FFMPEG 检索持续时间不适用于 MOV 文件

FFMPEG retrieve time duration not working for MOV files

所以我在 php 运行 对文件执行 ffprobe 命令并抓取输出(以获取持续时间、时间等)

exec("usr/bin/ffprobe -v quiet-print_format json -show_format -show_streams $location", $output, $exitCode); 

其中 $location 是文件位置 + 文件名。现在这个命令 100% 完美地适用于 .mp4 / .avi / 我试过的所有其他东西。除了 .mov,当使用 .mov 时,我的 "output" 变量是空的。有什么办法可以解决这个问题/改变这个问题吗?

您不会使用 ffprobe 获得有关 mov 文件的太多信息。使用 exiftool:

在您的服务器上安装该工具: http://owl.phy.queensu.ca/~phil/exiftool/install.html

如果你这样做了:

exiftool IMG_0014.MOV > a.txt

输出是

ExifTool Version Number         : 8.60
File Name                       : IMG_0014.MOV
Directory                       : .
File Size                       : 19 MB
File Modification Date/Time     : 2013:07:19 12:03:22-10:00
File Permissions                : rw-r--r--
File Type                       : MOV
MIME Type                       : video/quicktime
Major Brand                     : Apple QuickTime (.MOV/QT)
Minor Version                   : 0.0.0
Compatible Brands               : qt
Movie Data Size                 : 19979709
Movie Header Version            : 0
Modify Date                     : 2013:07:19 22:03:21
Time Scale                      : 600
Duration                        : 7.27 s
Preferred Rate                  : 1
Preferred Volume                : 100.00%
Preview Time                    : 0 s
Preview Duration                : 0 s
Poster Time                     : 0 s
Selection Time                  : 0 s
Selection Duration              : 0 s
Current Time                    : 0 s
Next Track ID                   : 3
Track Header Version            : 0
Track Create Date               : 2013:07:19 22:03:13
Track Modify Date               : 2013:07:19 22:03:21
Track ID                        : 1
Track Duration                  : 7.27 s
Track Layer                     : 0
Track Volume                    : 0.00%
Image Width                     : 1920
Image Height                    : 1080
Graphics Mode                   : ditherCopy
Op Color                        : 32768 32768 32768
Compressor ID                   : avc1
Source Image Width              : 1920
Source Image Height             : 1080
X Resolution                    : 72
Y Resolution                    : 72
Compressor Name                 : H.264
Bit Depth                       : 24
Video Frame Rate                : 27.011
Camera Identifier               : Back
Frame Readout Time              : 28512 microseconds
Matrix Structure                : 1 0 0 0 1 0 0 0 1
Media Header Version            : 0
Media Create Date               : 2013:07:19 22:03:13
Media Modify Date               : 2013:07:19 22:03:21
Media Time Scale                : 44100
Media Duration                  : 7.31 s
Media Language Code             : und
Balance                         : 0
Handler Class                   : Data Handler
Handler Vendor ID               : Apple
Handler Description             : Core Media Data Handler
Audio Channels                  : 1
Audio Bits Per Sample           : 16
Audio Sample Rate               : 44100
Audio Format                    : chan
Model                           : iPhone 4S
Software Version                : 6.1.3
Create Date                     : 2013:07:20 08:03:13+10:00
Make                            : Apple
Handler Type                    : Metadata Tags
Make (und-AU)                   : Apple
Creation Date (und-AU)          : 2013:07:20 08:03:13+10:00
Software (und-AU)               : 6.1.3
Model (und-AU)                  : iPhone 4S
Avg Bitrate                     : 22 Mbps
Image Size                      : 1920x1080
Rotation                        : 90

如果您只需要视频文件中的信息,您可以使用 ffmpeg 和所有其他 类 并在线使用 ffmpeg 查询视频文件。

$ffmpeg_path = 'ffmpeg'; //or: /usr/bin/ffmpeg - depends on your installation
$vid = 'PATH/TO/VIDEO'; //Replace here!

if (file_exists($vid)) {

    $video_attributes = _get_video_attributes($vid, $ffmpeg_path);

    print_r('Video codec: ' . $video_attributes['codec'] . ' - width: '  . $video_attributes['width'] 
            . ' - height: ' .  $video_attributes['height'] . ' <br/>');

    print_r('Video duration: ' . $video_attributes['hours'] . ':' . $video_attributes['mins'] . ':'
           . $video_attributes['secs'] . '.'. $video_attributes['ms']);
} else { echo 'File does not exist.'; }

function _get_video_attributes($video, $ffmpeg) {

    $command = $ffmpeg . ' -i ' . $video . ' -vstats 2>&1';  
    $output = shell_exec($command);  

    $regex_sizes = "/Video: ([^,]*), ([^,]*), ([0-9]{1,4})x([0-9]{1,4})/";
    if (preg_match($regex_sizes, $output, $regs)) {
        $codec = $regs [1] ? $regs [1] : null;
        $width = $regs [3] ? $regs [3] : null;
        $height = $regs [4] ? $regs [4] : null;
     }

    $regex_duration = "/Duration: ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}).([0-9]{1,2})/";
    if (preg_match($regex_duration, $output, $regs)) {
        $hours = $regs [1] ? $regs [1] : null;
        $mins = $regs [2] ? $regs [2] : null;
        $secs = $regs [3] ? $regs [3] : null;
        $ms = $regs [4] ? $regs [4] : null;
    }

    return array ('codec' => $codec,
            'width' => $width,
            'height' => $height,
            'hours' => $hours,
            'mins' => $mins,
            'secs' => $secs,
            'ms' => $ms
    );

}

我最终通过仅从视频文件中转换音频解决了这个问题,这在 ffmpeg 中速度非常快,您可以将质量设置得非常低并从中获得一致的持续时间。即使没有音频,它仍会抓取与电影长度相同的音频文件。只需不到一秒钟,即可为您提供可靠的数据。