johnDoe220's avatar

How to get file time(audio and video) with laravel

I am implementing a podcast system on my site My question is how can I get file information including time and other information, and finally how can I have podcasts in different qualities And finally, when I upload that file, I transfer it to Job so that I do not have to wait for the file to be uploaded to leave the upload page. Please give detailed instructions

0 likes
24 replies
Jacques_EJ's avatar

@Sinnbeck Hi, how are you? I read your reply to @johndoe220 regarding working for free. I have some questions about a project I am working on. Can I give you my email or contact info? I am willing to pay for your service and time. Thanks.

johnDoe220's avatar

I have been working on each component of this project in a completely professional way for months, and my only final concern was this issue. Unfortunately, you judge based on my level in laracasts. i using php-ffmpeg package..., anyway thank's

Sinnbeck's avatar

@johnDoe220 We dont judge you and dont care about your level. :) But

Please give detailed instructions

This sounds to use like you are asking us to write a guide for you. If you instead showed some code and asked "I am doing this, but x isnt working" we might actually be able to help you out.

1 like
johnDoe220's avatar

@Sinnbeck In a way you are right, of course I did not mean you because I was aware that you gave me a complete answer whenever you could, this is part of my code that I enter the time podcast manually, my only problem is This is how I can access the php-ffmpeg package in time

Podcast::create([
            'user_id' => auth()->user()->id,
            'category_id' => $request->category_id,
            'title' => $request->title,
            'slug' => PodcastsController::slug($request->title),
            'small_text' => $request->small_text,
            'description' => $request->description,
            'image' => $request->image->store('articles'),
            'voice' => $request->voice->store('articles'),
            'time' => $request->time,
        ]);
        session()->flash('success', __('trans.create podcast successfully'));
        return redirect(route('podcasts.index'));
Sinnbeck's avatar

@johnDoe220 Ok so you save the uploaded file, and save the podcast.

I would then write a command to use for testing this out. This can then later be run either in a queued job or a scheduled job.

So lets say that you have a path for the voice. In the command you can then extract that and try doing stuff with it

$podcast = Podcast::find(1);
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open(storage_path($podcast->voice));
//do the following for each size you want
$video->resize(new FFMpeg\Coordinate\Dimension(320, 240)); 
$video
    ->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
johnDoe220's avatar

@Sinnbeck Everything I did gave this error

Unable to probe http://localhost:8000/podcasts/VS6xqUre1o1I7HVAJ9Y3vNV851ZV9rchr3ox0X2F.mp3
Sinnbeck's avatar

@johnDoe220 Which line of the code is throwing that error? Seems like you are calling an url instead of a local path

johnDoe220's avatar

@Sinnbeck if using this code

$video = $ffmpeg->open(asset($podcast->voice));
// give me this error => Unable to probe http://localhost:8000/podcasts/VS6xqUre1o1I7HVAJ9Y3vNV851ZV9rchr3ox0X2F.mp3

and use this code

$video = $ffmpeg->open(storage_path($podcast->voice));
// give me this error => Unable to probe C:\Users\User\Desktop\podcast-v2\podcast\storage\podcasts/Zi5L268bSV6DJfktVnEoHnKRA2kJgEzlGWHsvDoU.mp3
Sinnbeck's avatar

@johnDoe220 Second one is the right one. But you need to give it the correct path, which does not seem to be the case here. Check you computer for where the files are actually at, and what path is stored in the database. If you show both, I am sure I can help you with the correct call

johnDoe220's avatar

@Sinnbeck i linked storage directory to public dir with command

php artisan storage:link

and i can open files with url browser,so file dir is true. and i checked file name and is true

Sinnbeck's avatar

@johnDoe220 Yes. But the path i assumed might not be. Can you post the url you can get the file on as well as the result of dd($podcast->voice);

johnDoe220's avatar

@Sinnbeck this name saved on database

podcasts/7gkXNUva84eaqS7Gaap2H6Ld6kNzT7ABDtbG9oPH.mp3

url for get files

http://localhost:8000/storage/podcasts/7gkXNUva84eaqS7Gaap2H6Ld6kNzT7ABDtbG9oPH.mp3

It should be noted that all the files I upload are stored in the public folder

Sinnbeck's avatar

@johnDoe220 Ok all good. Try this then :)

$video = $ffmpeg->open(Storage::disk('public')->path($podcast->voice));
johnDoe220's avatar

@Sinnbeck

Call to undefined method FFMpeg\Media\Video::resize()
// this line is error =>  $video->resize(new FFMpeg\Coordinate\Dimension(320, 240));
Sinnbeck's avatar

@johnDoe220 Maybe you need to call the ->filters() method first. I am litterally just taking code from the examples on the github page :)

But be aware that this was an example of resizing a video. Is yours pure audio or?

$video = $ffmpeg->open(Storage::disk('public')->path($podcast->voice));
$video->filters()->resize(new FFMpeg\Coordinate\Dimension(320, 240)); 

If it is audio, use the examples there: https://github.com/PHP-FFMpeg/PHP-FFMpeg#audio

johnDoe220's avatar

@Sinnbeck my file format mp3, i give filters now give me this error

Encoding failed

this status my code

 $podcast = Podcast::find($podcast);
        $ffmpeg = FFMpeg::create([
            'ffmpeg.binaries' => 'C:/FFmpeg/bin/ffmpeg.exe',
            'ffprobe.binaries' => 'C:/FFmpeg/bin/ffprobe.exe'
        ]);
        $video = $ffmpeg->open(\Storage::disk('public')->path($podcast->voice));
        $video->filters()->resize(new Dimension(320, 240));
        $video
            ->save(new X264(), 'export-x264.mp4');
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@johnDoe220 If it is audio, you cannot resize it like that as sound does not have a width and height. Check the link in my last comment.

1 like
rodrigo.pedra's avatar

@johnDoe220 Great you got it working from @sinnbeck 's help, I was out for a while and wanted to address just one point:

Unfortunately, you judge based on my level in laracasts.

By affirming this you are being the only one who is making any judgment. I made no value judgement on my reply, and even from the scarce information you provided about your issue I gave you two leads on the solution.

The first link to a library you said later, not on the opening post, you were already using, so we both agree it was a good suggestion that I could never guess you were already using it. And the second one to a free series on Laravel as one of few details you shared on your opening post you ask about how to transfer a task to a job to be executed on background, which is a topic covered by this series.

If you took it as having any malicious meaning, the only one to be blamed is your interpretation from what I wrote.

You can look on my profile and see I make no distinction on either the OP is a veteran or not. And also that I am usually very glad to help. Of course I am no guru nor the best coder, and never claimed to be such things, but I've always try me best to help people out, when there is at least some clarity on the subject.

If you can take a suggestion, maybe next time you could reconsider how you phrase your help request and make what is your issue clearer in the first place. Share what you have already tried, where do you think any bottleneck lies, and preferably share any relevant code.

And as a lesson learned on my side, I will try to refrain on engaging on posts with abstract information and that are phrased in a manner it seems to want all the work to be done from responders.

Good luck =)

1 like
johnDoe220's avatar

this code is work Of course I put the video to test

public function store(createRequest $request)
    {
        $podcast = Podcast::create([
            'user_id' => auth()->user()->id,
            'category_id' => $request->category_id,
            'title' => $request->title,
            'slug' => PodcastsController::slug($request->title),
            'smal_text' => $request->smal_text,
            'description' => $request->description,
            'image' => $request->image->store('podcasts'),
            'voice' => $request->voice->store('podcasts'),
            'time' => $request->time
        ]);
        $this->getResize($podcast->id);
        session()->flash('success', __('trans.podcast created successfully'));
        return redirect(route('podcasts.index'));
    }

this is function convert other quality

public function getResize($podcast)
    {
        $podcast = Podcast::find($podcast);
        $ffmpeg = FFMpeg::create([
            'ffmpeg.binaries' => 'C:/FFmpeg/bin/ffmpeg.exe',
            'ffprobe.binaries' => 'C:/FFmpeg/bin/ffprobe.exe'
        ]);
        $video = $ffmpeg->open(\Storage::disk('public')->path($podcast->voice));
        $video
            ->filters()
            ->resize(new Dimension(320, 240))
            ->synchronize();
        $video
            ->frame(TimeCode::fromSeconds(10))
            ->save('frame.jpg');
        $video
            ->save(new X264(), 'export-x264.mp4');
    }

but first install ffmpeg on your pc,i use this toturial

https://ganje.host/blog/how-to-install-ffmpeg-on-cpanel/

thanks, to @sinnbeck

Please or to participate in this conversation.