在文件夹中为一组mp4视频生成缩略图。

5
我看过这篇文章,但并没有太大帮助。
我有一个名为videos的文件夹和另一个名为thumbnails的文件夹。视频文件夹中有许多mp4视频文件,我想使用ffmpeg和php在第四秒捕获缩略图并将其保存到缩略图文件夹中。
  1. I am using Wamp server 2.2 on windows whit php 5.3.8 and Apache 2.2.21
  2. I downloaded ffmpeg from FFmpeg Windows Builds section of ffmpeg download page and the static 32 build from this link.
  3. I extracted the 7z file to my website root
    here is my php code:

    $ffmpeg = "includes/ffmpeg/bin/ffmpeg";
    foreach(glob('files/videos/*.mp4') as $pathname){
      $filename = substr($pathname,13,strripos($pathname,'.mp4')-13);
      $thumbnail = 'files/thumbnails/'.$filename.'.jpg';
      exec("ffmpeg -i $pathname -an -y -f mjpeg -ss 00:00:04 -vframes 1 $thumbnail");
    }
    

但是什么都没有发生,缩略图文件夹总是空的!
- 我如何确定我的服务器上是否安装了ffmpeg?
- 我如何让我的脚本工作起来?
请帮忙。


你能检查权限和路径是否正确定义了吗? - जलजनक
@DoSparKot 什么权限?我正在使用wamp服务器,我有Windows文件夹。我已经给视频和缩略图设置了完全访问权限,但是没有任何变化。 - user1778985
3个回答

4

尝试这个:

$ffmpeg = "c:/wamp/www/includes/ffmpeg/bin/ffmpeg";
$videos = "c:/wamp/www/files/videos/*.mp4";
$ouput_path = "c:/wamp/www/files/thumbnails/";
foreach(glob($videos) as $v_file){
    $fname = basename($v_file, ".mp4");
    $thmb = $ouput_path.$fname.'_tn.jpg';
    $cmd = "$ffmpeg -i $v_file -an -y -f mjpeg -ss 00:00:04 -vframes 1 $thmb";
    $stat = system ($cmd);
}

1
尝试在命令中使用绝对路径,而不是依赖于PATH环境变量: exec()system()都可以工作。解决路径定义问题。
/* Using Absolute paths */
$ffmpeg = "c:/wamp/www/includes/ffmpeg/bin/ffmpeg";
$videos = "c:/wamp/www/files/videos/*.mp4";
$ouput_path = "c:/wamp/www/files/thumbnails/";

foreach(glob($videos) as $video_file){

    $filename = basename($video_file, ".mp4");
    $thumbnail = $ouput_path.$filename.'_tn.jpg';
    $command = "$ffmpeg -i $video_file -an -y -f mjpeg -ss 00:00:04 -vframes 1 $thumbnail";

    $status = system ($command);
/*or
    $status = exec($command);

    if ($status === false) {
        var_dump("ERROR: Conversion Failed!!!!");
    } else
        var_dump($status);
*/
}

有趣,你能把单个文件放在 C:\temp\ 并尝试使用修改后的路径吗? - जलजनक
它取决于内部常量DIRECTORY_SEPARATOR,代码中为/,在Windows平台上输出\\ - जलजनक
@PedarJepeto 无论如何,感谢你,现在我知道如何动态生成视频缩略图了。 :-) - जलजनक
它真的对你有效吗?我完全复制粘贴了代码,但无法使其工作!! :( - user1778985

0

我不能添加评论,所以在这里(作为帖子)是我在处理此问题时发现的内容: 文件名中的空格会阻止ffmpeg找到正确的文件。因此,一种方法是更改文件名。


ffmpeg 命令行工具可以通过正确的引用或转义处理空格:ffmpeg -i input "output 1.mp4" 'output 2.mp4' output\ 3.mp4 - llogan
谢谢,我会尝试的。 - user2217108
我不是 PHP 用户(问题标记为 [tag:php]),所以我的三个示例可能适用于 PHP,也可能不适用。 - llogan
哦,它运行得非常好,我在字符串中使用了“output 1.mp4”来转义引号,像这样: -"output 1.mp4"-,并且万岁!!! :) 感谢提示,我相信这就是为什么它对Pedar Jepeto没用的原因。 - user2217108

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接