6

如何从输出中获取特定信息,例如“持续时间” ffmpeg -i /var/thismovie.avi

我需要帧高、帧宽、电影持续时间和帧速率。所有这些都在上述命令的输出中,但是我如何单独获取我需要的位并将它们放入 PHP 中的变量中?

我已经放弃了尝试安装ffmpeg-php在此之前我用来做同样工作的东西。

谢谢

ffmpeg -i /var/thismovie.avi产生这样的输出

ffmpeg version N-43171-ga763caf Copyright (c) 2000-2012 the FFmpeg developers built on Aug 3 2012 07:56:19 with gcc 4.1.2 (GCC) 20080704 (Red Hat 4.1.2-52) configuration: --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvpx --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-pic --enable-libx264 --enable-libxvid --disable-ffplay --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads --extra-cflags=-fPIC libavutil 51. 66.100 / 51. 66.100 libavcodec 54. 48.100 / 54. 48.100 libavformat 54. 22.100 / 54. 22.100 libavdevice 54. 2.100 / 54. 2.100 libavfilter 3. 5.102 / 3. 5.102 libswscale 2. 1.100 / 2. 1.100 libswresample 0. 15.100 / 0. 15.100 libpostproc 52. 0.100 / 52. 0.100 [avi @ 0x18e8e240] non-interleaved AVI [avi @ 0x18e8e240] max_analyze_duration 5000000 reached at 5000000 Input #0, avi, from '/var/www/vhosts/mysite.com/httpdocs/movie.avi': Duration: 00:00:10.76, start: 0.000000, bitrate: 5180 kb/s Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 25 tbn, 50 tbc Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, stereo, s16, 128 kb/s At least one output file must be specified` 
4

4 回答 4

15

基于ffprobe@blahdiblah 建议的解决方案并受到另一个问题答案的启发:https ://github.com/paulofreitas/php-ffprobe

注意:需要 ffmpeg 0.9+,支持所有 ffmpeg 支持的文件类型

使用类

// Example 1
$info = new ffprobe($filename);
var_dump($info->format->format_long_name); // e.g. string(10) "AVI format"
var_dump($info->streams[0]->duration);     // e.g. string(11) "5674.674675"

// Example 2 (prettified)
$info = new ffprobe($filename, true);
var_dump($info->format->format_long_name); // e.g. string(10) "AVI format"
var_dump($info->streams[0]->duration);     // e.g. string(14) "1:34:34.674675"

扩展类

class ffprobe_ext extends ffprobe
{
    public function __construct($filename)
    {
        parent::__construct($filename);
    }

    public function getVideoStream()
    {
        foreach ($this->streams as $stream) {
            if ($stream->codec_type == 'video') {
                return $stream;
            }
        }
    }

    public function getVideoInfo()
    {
        $stream = $this->getVideoStream();
        $info   = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
        $info->duration     = (float) $stream->duration;
        $info->frame_height = (int) $stream->height;
        $info->frame_width  = (int) $stream->width;
        eval("\$frame_rate = {$stream->r_frame_rate};");
        $info->frame_rate   = (float) $frame_rate;

        return $info;
    }
}

$ffprobe = new ffprobe_ext($filename);
$info = $ffprobe->getVideoInfo();
var_dump($info->duration); // e.g. float(5674.674675)

欢迎进一步改进!:-)

于 2012-08-04T08:55:13.647 回答
9

改为使用ffprobe

ffprobe是与 FFmpeg 一起打包的工具,完全符合您所追求的目的:提取视频信息。它输出各种容易解析的格式,并且会比解析FFmpeg输出的附带信息容易得多。

例如:

$ ffprobe -show_format -loglevel quiet mptestsrc.mp4 
[FORMAT]
filename=mptestsrc.mp4
nb_streams=1
format_name=mov,mp4,m4a,3gp,3g2,mj2
format_long_name=QuickTime/MPEG-4/Motion JPEG 2000 format
start_time=0.000000
duration=12.040000
size=237687
bit_rate=157931
TAG:major_brand=isom
TAG:minor_version=512
TAG:compatible_brands=isomiso2avc1mp41
TAG:encoder=Lavf54.20.100
[/FORMAT]
于 2012-08-04T02:24:48.960 回答
8

您可以使用具有输出 JSON 格式选项的 ffprobe。

看看这个例子:

ffprobe -v quiet -print_format json -show_format Ramp\ -\ Apathy.mp3

产生以下输出:

{
    "format": {
        "filename": "Ramp - Apathy.mp3",
        "nb_streams": 2,
        "format_name": "mp3",
        "format_long_name": "MP2/3 (MPEG audio layer 2/3)",
        "start_time": "0.000000",
        "duration": "203.638856",
        "size": "4072777",
        "bit_rate": "159999",
        "tags": {
            "title": "Apathy",
            "artist": "Ramp",
            "album": "Evolution Devolution Revolution",
            "date": "1999",
            "genre": "Metal"
        }
    }
}

我正在使用 ffprobe 版本 1.0.7。

于 2013-05-28T22:40:34.000 回答
3
$output = `ffmpeg -i /var/thismovie.avi`; //note that back quote is like exec
preg_match('/Duration: (.*?),.*?Video:.*?0x.*?([0-9]+)x([[0-9]+).*?([0-9]+) fps/i'
    ,$output , $result);

$result 数组的输出应该是这样的:

Array
(
    [0] => Duration: 00:00:10.76, start: 0.000000, bitrate: 5180 kb/s Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 25 fps
    [1] => 00:00:10.76
    [2] => 1280
    [3] => 720
    [4] => 25
)

更新

实际输出是多行,所以请将模式更新为

/Duration: (.*?),.*?([0-9]+)x([0-9]+) \[.*?([0-9]+) fps/is

于 2012-08-04T02:58:41.460 回答