开发者

How to extract frame size and length from ffmpeg, in php?

I usually use midentify, which spits out a nicely formatted string, that is easy to preg_match

It however, fails sometimes, so I wanna do a fall-back method via ffmpeg. ffmpeg -i hello.avi spits this out:

    Input #0, avi, from 'hello.avi':
  Metadata:
    en开发者_JAVA技巧coder         : Nandub v1.0rc2
  Duration: 01:11:16.56, start: 0.000000, bitrate: 1202 kb/s
    Stream #0.0: Video: mpeg4, yuv420p, 640x336 [PAR 1:1 DAR 40:21], 25 tbr, 25 tbn, 25 tbc
    Stream #0.1: Audio: mp3, 48000 Hz, 2 channels, s16, 117 kb/s
At least one output file must be specified

I need the width and height of the actual frame size, as well as the duration.

What would be the best way to extract this from here? Im not that familiar with regex.


Duration is easy:

preg_match('/Duration: (\d{2}:\d{2}:\d{2}\.\d{2})/', $source, $matches);
$matches[1] == '01:11:16.56';

The size is harder. I'm going to guess that any set of numbers separated by nothing except an x character will be the dimensions:

preg_match('/(\d+)x(\d+)/', $source, $matches);
$matches[1] == '640';
$matches[2] == '336';


I would use the ffmpeg extension to PHP. Download and install from http://ffmpeg-php.sourceforge.net/ then:

extension_loaded('ffmpeg') or die('Error in loading ffmpeg');

$ffmpegInstance = new ffmpeg_movie('hello.avi');

echo "getDuration: " . $ffmpegInstance->getDuration() . "getFrameCount: " . $ffmpegInstance->getFrameCount() . "getFrameRate: " . $ffmpegInstance->getFrameRate() . "getFilename: " . $ffmpegInstance->getFilename() . "getComment: " . $ffmpegInstance->getComment() . "getTitle: " . $ffmpegInstance->getTitle() . "getAuthor: " . $ffmpegInstance->getAuthor() . "getCopyright: " . $ffmpegInstance->getCopyright() . "getArtist: " . $ffmpegInstance->getArtist() . "getGenre: " . $ffmpegInstance->getGenre() . "getTrackNumber: " . $ffmpegInstance->getTrackNumber() . "getYear: " . $ffmpegInstance->getYear() . "getFrameHeight: " . $ffmpegInstance->getFrameHeight() . "getFrameWidth: " . $ffmpegInstance->getFrameWidth() . "getPixelFormat: " . $ffmpegInstance->getPixelFormat() . "getBitRate: " . $ffmpegInstance->getBitRate() . "getVideoBitRate: " . $ffmpegInstance->getVideoBitRate() . "getAudioBitRate: " . $ffmpegInstance->getAudioBitRate() . "getAudioSampleRate: " . $ffmpegInstance->getAudioSampleRate() . "getVideoCodec: " . $ffmpegInstance->getVideoCodec() . "getAudioCodec: " . $ffmpegInstance->getAudioCodec() . "getAudioChannels: " . $ffmpegInstance->getAudioChannels() . "hasAudio: " . $ffmpegInstance->hasAudio();


I just want to add slightly modified version of the @lonesomeday's answer:

this part is correct but if you have string like this one:

Duration: 00:05:40.11, start: 0.000000, bitrate: 60847 kb/s  
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), 
yuv420p(tv, bt709), 1920x1080, 60846 kb/s, 29.97 fps, 29.97 tbr, 2997 tbn, 5994 
tbc (default)

this

preg_match('/(\d+)x(\d+)/', $source, $matches);

will return

$matches[1] == '0';
$matches[2] == '31637661';

So I modified it a little:

preg_match('/(\d{2,4})x(\d{2,4})/', $source, $matches);

This way it wont match one digit or more than 4 digits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜