How do you calculate the duration/length of an mp4 video using php?
How do you get/calculate the duration/length of an mp4 video with php?
I tried using this but it only works for f4v. :< Can you help me with 开发者_开发知识库the mp4 case ?
I could not get tandu's solution working.
Here's what eventually worked for me:
$ffmpeg_output = shell_exec("ffmpeg -i \"$file\" 2>&1");
if( preg_match('/.*Duration: ([0-9:]+).*/', $ffmpeg_output, $matches) ) {
echo $matches[1];
} else {
echo "$file failed\n";
}
Hope this helps someone.
I've also wanted to do this recently. I did a lot of research on it, and there is no native way in php. One suggestion is ffmpeg-php, but it did not appear to work for me. Another method is to use the command line ffmpeg:
exec("ffmpeg -i \"{$videofile}\" 2>&1");
$search='/Duration: (.*?),/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
This can be done in pure php by using the php-reader library. This library is a full implementation of the ISO Base Media File Format, or ISO/IEC 14496-12
in pure php. Then you can either trust the metadata header for the duration, or calculate it yourself based on the stbl boxes.
精彩评论