Calculate Encoding Time with PHP and FFMPEG

preview_player
Показать описание
This is a Math heavy lesson. You've been warned.
We use FFMPEG first to get the duration of the video, parse it with regex, calculate seconds, then use the bitrate equation to calculate bytes.

Then using our interval and ajax that repeatedly checks file size, we can pretty accurately tell our users how long a video will take to encode.
Рекомендации по теме
Комментарии
Автор

A really good video.

A more accurate way to do this is to actually parse the ffmpeg output and get the frames which have been encoded, and compare that to duration in seconds X frame rate.

This is what we do at encodi.com

I also noted that you probably are alrady taking the miliseconds into account, becasue if you look the last seperator is a '.' not a ':' so you seconds in your example is probably being stored at 24.44 which is correct.

But either way I love the video.

Jon

narcosi
Автор

@Stanislav2588 yea, the thing is the ffmpeg doc doesn't say anything about what the numbers in the timecode stand for. It doesn't even say if its standard timecode or not.

optikalefx
Автор

hey man, great tutorial! i'm really getting into this programming thing. I'm from a video/audio background and just to let you know those last two digits are not milliseconds, but frames. So you might have to do some extra formulas in the PHP code, because every video can have a different frame rate. Right? The video in your example was 29.97 frames per second, which is pretty standard. I'm very new to this, so this was very informative. Thanx very much.

Stanislav
Автор

@optikalefxx one way to double check would be to open the movie in QuickTime and do Cmd+I. I'm 100% sure that QT is going to show you the timecode. If the two numbers match exactly than it is timecode, if not most likely it is a percentage of a second, not milliseconds. That's interesting that you mention it, I'll do some research myself.

Stanislav
Автор

@dakasadaka it works in the background - but it only RETURNS when its done. Just call to the PHP file with ajax and when it comes back, its done. I normally send a 2nd ajax to check filesize constantly so i can see how progress is going.

optikalefx
Автор

@trikerr its only for firefox. Its called extension developers extension. in mozilla addons.

optikalefx
Автор

@Stanislav2588 Are 100% sure that for FFMPEG the last 2 are frames? Because I can pull the frame rate from ffmpeg and then calculate the last 2 digits if they are. I know normally the last 2 are frames but this is ffmpegs timecode.

optikalefx
Автор

The GUI is custom. I use it whenever i need video. To get 'stuff' out from a command line call make sure you have the 2>&1 in there

optikalefx
Автор

the language doesn't really matter, you can make system command line calls in each of those languages. Just look up how to make system calls for the one you want.

optikalefx
Автор

well 2 things. 1) why not use what i'm using? and 2) i'd look up if ffmpeg has a surpress prompts option

optikalefx
Автор

Thank you mate, i think i need more research and testing ...

orideconblingbling
Автор

@narcosi1 How accurate are you at the end? I'm usually sitting at 99% for a few seconds here an there depending on bitrate settings. I never thought of doing it that way but that seems like it would be way more accurate.

yea the final code im using I'm doing the calculations as milliseconds instead of frames. Good call though.
Nice site btw!

optikalefx
Автор

@optikalefxx did you try the QT trick??

Stanislav
Автор

Hi! Love your tutorials! Where can I find the extension RegExp evaluator for Safari? I google it but couldn't find it. thx

trikerr
Автор

@Stanislav2588 qt only shows hours minutes and seconds. when i do ffmpeg it shows me 00:27:58:41 but qt shows 00:27:58

:(

optikalefx
Автор

I just want to know when is it processed so I can update my database?
$output = shell_exec('ffmpeg ' . . ' ' .
this works in background and if it is done need to know so I can update database!

dakasadaka
Автор

@optikalefxx similar to be honest a few seconds sometimes but we do have some post processing code for making thumbnails etc which also delays the end - sounds like you have a similar line of code to us:

if(percent>99) percent=99 :-)

We use ours as a method of checking that it is still processing as much as anything else.

if you are using miliseconds remember: 44.9 is 44 seconds and 900 miliseconds not 9.

email me from the site - be good to know you.

narcosi
Автор

Hey optikalefxx, for the encoding GUI what did you use? Homebrew or available on the internet.

I am testing using sothink command line encoder and i can capture the output using php (proc_open), but when i use ffmpeg i cannot capture anything although the encoding process is completed.

Any suggestion?

orideconblingbling
Автор

@optikalefxx They are not frames they are simple a decimal of seconds. if it was a : they would be frames but it is a .

narcosi
Автор

Hello optikalefxx, i'm sorry for bothering you ... But i need to ask you again ...

I run ffmpeg conversion using proc_open and catch the output using $console_output = fgets($pipes[2], 10000);

Then php start displaying output like using CMD, but stuck at this line :

Press [q] to stop, [?] for help

After long waiting, the output continues normally until end but no encoding progress info like :

frame=2295 fps=71 q=14.0 size=14561kB time=00:01:16.58 bitrate=1557.5kbits/s

Any clue about this?

orideconblingbling
welcome to shbcf.ru