【问题标题】:How do you calculate the duration/length of an mp4 video using php?您如何使用 php 计算 mp4 视频的持续时间/长度?
【发布时间】:2011-06-25 08:37:18
【问题描述】:

如何使用 php 获取/计算 mp4 视频的时长/长度?

I tried using this 但它仅适用于 f4v。 :

【问题讨论】:

标签: php mp4


【解决方案1】:

我无法让 tandu 的解决方案发挥作用。

以下是最终对我有用的方法:

$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";
}

希望这对某人有所帮助。

【讨论】:

  • 简单而完美。感谢您发布@Henno
  • 这可能会导致 0 秒的持续时间。因为 ffmpeg 也会返回毫秒。所以输出是这样的:00:00:05.66,上面的正则表达式将忽略十进制00:00:05。你需要的是:/.*Duration: ((\d+)(?:\:)(\d+)(?:\:)(\d+))*/i.
【解决方案2】:

我最近也想这样做。我对它做了很多研究,在php中没有原生的方式。一个建议是ffmpeg-php,但它似乎对我不起作用。另一种方法是使用命令行ffmpeg:

exec("ffmpeg -i \"{$videofile}\" 2>&1");
$search='/Duration: (.*?),/';
$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);

【讨论】:

【解决方案3】:

这可以通过使用php-reader 库在纯php 中完成。这个库是纯 php 中 ISO Base Media File Format, or ISO/IEC 14496-12 的完整实现。然后您可以在持续时间内信任元数据标头,也可以根据 stbl 框自行计算。

【讨论】:

    猜你喜欢
    • 2014-08-07
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2020-11-16
    • 2015-02-17
    相关资源
    最近更新 更多