问题描述
我的脚本可以正常工作20分钟只是为了生成5-10个缩略图!!
该脚本的工作原理是生成随机数,后来用作帧号。生成的所有数字都在电影帧数之内。你可以弄清楚为什么这个脚本需要20分钟的时间才能完成?
如果没有,更好的解决方案?
<?php
//不要'timeout
set_time_limit(0);
//加载文件(这可以是任何文件 - 仍然需要年龄)
$ mov = new ffmpeg_movie('1486460.mp4');
//获取电影中的总帧
$ total_frames = $ mov-> getFrameCount();
//循环5-10次生成随机帧5-10次
($ i = 1; $ i //生成200以内的数字和总帧数。
$ frame = mt_rand(200,$ total_frames);
$ getframe = $ mov-> getFrame($ frame);
//检查电影中是否存在该框架
//如果是,将框架号放在数组中并打破当前循环
if($ getframe){
$ frames [$ frame] = $ getframe;
$ i ++;
}
}
//对于每个找到的帧生成一个缩略图
foreach($ frames as $ key => $ getframe){
$ gd_image = $ getframe-> toGDImage();
imagejpeg($ gd_image,images / shot _。$ key。'。jpeg');
imagedestroy($ gd_image);
echo $ key。'< br />';
}
?>
脚本应该生成有效的帧编号? START-END中的任何内容都应该是有效的帧号?
您可以调用,使用 -ss
开关寻找一个适当的起点,而不是帧数)和 -vframes 1
来告诉它提取一个帧,例如:
ffmpeg -i 1486460.mp4 -ss 10 -vframes 1 images / shot_10.jpg
将从10秒内提取一个框架,并调用它 images / shot_10.jpg
I'm trying to generate thumbnails from random points in a movie using FFMPEG and FFMPEG-PHP extension.
My script works OK.. however takes 20 minutes just to generate 5-10 thumbnails!!
The script works by generating random numbers which are used as frame numbers later. All numbers generated are within the movies frame count.
Can you work out why this script is taking 20 mins to finish?If not, a better solution?
<?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/>';
}
?>
The script SHOULD be generating frame numbers which are valid? Anything within START - END should be valid frame numbers? Yet the loop takes ages!
You could invoke ffmpeg from the commandline, using the -ss
switch to seek to an approrpriate start-point (in time, not in number of frames) and -vframes 1
to tell it to extract exactly one frame, e.g.:
ffmpeg -i 1486460.mp4 -ss 10 -vframes 1 images/shot_10.jpg
Will extract a frame from 10 seconds in and call it images/shot_10.jpg
这篇关于使用PHP + FFMPEG生成随机缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!