【问题标题】:How to create an image using ffmpeg and PHP?如何使用 ffmpeg 和 PHP 创建图像?
【发布时间】:2010-11-18 05:21:26
【问题描述】:

我正在使用this script,但我无法创建图像。

我的文件是here

【问题讨论】:

  • 您提供的那个文件实际上是无用的,因为它与您的特定 PHP 安装有内在联系。不如告诉我们您看到的是哪种错误或结果?
  • 这个脚本没有生成图片

标签: php image video ffmpeg


【解决方案1】:

您的 PHP 程序基本上调用了两次/usr/bin/ffmpeg。先在命令行上试试!你可以

echo "<pre>$cmd</pre>"

找出你的 PHP 脚本到底在做什么,然后在命令行上尝试那个确切的命令。

第一个命令应该类似于

/usr/bin/ffmpeg -i /var/www/beta/clock.avi 2>&1

这是您放置echos 的地方:

// get the duration and a random place within that
$cmd = "$ffmpeg -i $video 2>&1";
echo "<pre>$cmd</pre>"

if (preg_match('/Duration: ((\d+):(\d+):(\d+))/s', `$cmd`, $time)) {
    $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
    $second = rand(1, ($total - 1));
}

// get the screenshot
$cmd = "$ffmpeg -i $video -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
echo "<pre>$cmd</pre>"
$return = `$cmd`;

【讨论】:

    【解决方案2】:

    它不起作用,因为您从未执行过$cmd。要实际执行$cmd,您需要使用popen()proc_open()exec()

    试试这个功能。只要 ffmpeg 可以访问,它就应该生成图像。要使其可访问,请将其添加到 linux 中的 $path,将其放入 Windows 中的 windows/system32 文件夹中。或者将其添加到Windows控制面板中的环境变量中。

    /** * ExtractThumb,从视频中提取缩略图 * * 此函数加载视频并从第 4 帧中提取图像 * 进入剪辑的秒数 * @param $in string 正在处理的视频的输入路径 * @param $out string 输出图片的保存路径 */ 函数 ExtractThumb($in, $out) { $thumb_stdout; $错误; $retval = 0; // 如果文件已经存在则删除 if (file_exists($out)) { unlink($out); } // 使用 ffmpeg 从电影中生成缩略图 $cmd = "ffmpeg -itsoffset -4 -i $in -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 $out 2>&1"; 执行($cmd,$thumb_stdout,$retval); // 将错误排队等待处理 if ($retval != 0) { $errors[] = "FFMPEG 缩略图生成失败"; } if (!empty($thumb_stdout)) { foreach ($thumb_stdout 作为 $line) { 回声 $line 。 "
    \n"; } } if (!empty($errors)) { foreach ($errors 为 $error) { 回声$错误。 "
    \n"; } } }

    $thumb_stdout - 显示与在 CLI 中相同的输出。这对于查看 ffmpeg 正在执行的操作的详细信息以及查看它在不工作时崩溃的位置很有用。

    $errors - 如果 CLI 以错误代码退出(IE,如果 ffmpeg 崩溃)将显示错误。

    【讨论】:

    • 也可以使用system(),以及其他几个选项
    【解决方案3】:

    先安装ffmpeg-php(http://ffmpeg-php.sourceforge.net/)

    然后你就可以使用这个简单的代码了:

    <?php
    $frame = 10;
    $movie = 'test.mp4';
    $thumbnail = 'thumbnail.png';
    
    $mov = new ffmpeg_movie($movie);
    $frame = $mov->getFrame($frame);
    if ($frame) {
        $gd_image = $frame->toGDImage();
        if ($gd_image) {
            imagepng($gd_image, $thumbnail);
            imagedestroy($gd_image);
            echo '<img src="'.$thumbnail.'">';
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2012-07-12
      • 2013-07-22
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多