我正在使用这个脚本,但我无法创建图像。
我的文件在这里。
它不起作用,因为您从不执行$cmd. 要实际执行,$cmd您需要使用popen(),proc_open()或exec().
试试这个功能。只要 ffmpeg 可以访问,它就应该生成图像。要使其可访问,请将其添加到$pathlinux 中,将其放入windows/system32Windows 中的文件夹中。或者将其添加到 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 崩溃),将显示错误。
您的 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`;
首先安装 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.'">';
}
}
?>