我试图使用FFMPEG和FFMPEG-PHP扩展从电影中的随机点生成缩略图。
我的剧本很好..。然而,只需20分钟就能生成5-10个缩略图!!
该脚本通过生成随机数来工作,这些随机数后来用作帧号。生成的所有数字都在电影帧计数之内。
你能弄明白为什么这个剧本要花20分钟才能完成吗?如果没有,有更好的解决办法吗?
<?php
//Dont' timeout
set_time_limit(0);
//Load the file (This can be any file - still takes ages)
$mov = new ffmpeg_movie('1486460.mp4');
//Get the total frames within the movie
$total_frames = $mov->getFrameCount();
//Loop 5-10 times to generate random frames 5-10 times
for ($i = 1; $i <= 5; ) {
// Generate a number within 200 and the total number of frames.
$frame = mt_rand(200,$total_frames);
$getframe = $mov->getFrame($frame);
// Check if the frame exists within the movie
// If it does, place the frame number inside an array and break the current loop
if($getframe){
$frames[$frame] = $getframe ;
$i++;
}
}
//For each frame found generate a thumbnail
foreach ($frames as $key => $getframe) {
$gd_image = $getframe->toGDImage();
imagejpeg($gd_image, "images/shot_".$key.'.jpeg');
imagedestroy($gd_image);
echo $key.'<br/>';
}
?>
脚本应该生成有效的帧号吗?起始端中的任何内容都应该是有效的帧号?但是这个循环需要很长时间!
发布于 2010-04-07 09:29:53
您可以从命令行调用ffmpeg,使用-ss
开关寻找一个额外的起始点(在时间上,而不是在帧数上),并通过-vframes 1
告诉它提取一个帧,例如:
ffmpeg -i 1486460.mp4 -ss 10 -vframes 1 images/shot_10.jpg
将从10秒内提取一个帧,并称之为images/shot_10.jpg
。
发布于 2010-05-31 22:07:53
这里的问题是单词随机。我成功地获得了视频的持续时间,然后尝试得到一个带有随机持续时间的帧。更多帧很容易修改:
$cmd = "ffmpeg -i {$src} 2>&1 |grep Duration";
$output = array ();
exec($cmd, $output);
if(count($output)) {
$duration = explode(':', trim(str_replace('Duration:', NULL, current(explode(',', current($output))))));
list($hour, $min, $sec) = $duration;
$sec = sprintf("%02d:%02d:%02d", rand(0, $hour), rand(0, $min), rand(0, $sec));
} else {
$sec = "00:00:12"; //12sec it's ok :)
}
$cmd = "ffmpeg -ss {$sec} -i {$src} -s {$w}x{$h} -f image2 -vframes 1 {$destination}";
$output = array ();
exec($cmd, $output);
发布于 2010-04-07 09:30:24
我对电影格式不太了解,但由于压缩可能依赖于带有参照系的单通道增量压缩,如果参照系的位置没有定义,这意味着系统必须播放完整的电影才能获得特定的偏移量。通过总是加载,例如,查找偏移量20和帧数除以某个值之间的帧,可以很容易地检查这一点。
假设是这样的话,您还有另一个问题,就是您设计算法的方式,您需要回退到开始,并向前搜索5个帧中的每一个--如果预先生成偏移量,然后对列表进行排序,ffmpeg可能能够在一次传递中获取帧。
HTH
结果表明,C.
https://stackoverflow.com/questions/2591206
复制相似问题