FFmpeg文件转换超过最大执行时间


FFmpeg file conversion exceeds maximum execution time

我有一个文件上传系统,检查文件格式等,并在必要时转换为mp4。只要视频文件的总长度小于30秒,就可以正常工作。我一直在测试这两个短剪辑,每个大约10秒,它工作得很好,但当我测试这个剪辑,这33秒,我得到错误:

Fatal error: Maximum execution time of 30 seconds exceeded inC:'xampp'htdocs'own_it_all'global.func'file_upload.php on line59

我可以增加php.ini文件的最大执行时间,但由于视频的最大长度是20分钟,让用户每个视频等待20分钟似乎不太友好。是否有一种方法转换视频立即或接近?

这是我的执行命令:

$cmd = "ffmpeg -i $input -vcodec h264 -acodec aac -strict -2 $o";

由于up-loader允许多次上传,因此这是在for循环中。

foreach($_FILES['file']['name'] as $key => $name){
        if($_FILES['file']['error'][$key] === 0){
            $temp = $_FILES['file']['tmp_name'][$key];
            $ext = explode('.',$name);
            $ext = strtolower(end($ext));
            $_file = md5($temp).time();
            $file = $_file.'.'.$ext;
            if(in_array($ext,$allowed) === true &&  move_uploaded_file($temp,"../uploads/{$file}") === true){
                $file_type = explode('/',$_FILES['file']['type'][$key]);
                if($file_type[0] === 'image'){
                    $succedeed[] = array('name' => $name,'file' => $file, 'type' => 'image');               
                }else{
                    $ffmpeg = 'ffmpeg';
                    $output = dirname(__DIR__).'/uploads/thumbs/'.$_file.'.jpg';
                    $input = dirname(__DIR__).'/uploads/'.$file;
                    $mov = new ffmpeg_movie($input);
                    $d =  $mov->getDuration();
                    $iscopy = $mov->getCopyright();
                    $h = $mov->getFrameHeight();
                    $w = $mov->getFrameWidth();
                    $pos = ceil((int)$d /3);
                    $size = $w.'x'.$h;
                    $i = explode('.',$input);
                    $o = $i[0].'.mp4';
                    if(ceil($d) < 1200){
                        if($ext != 'mp4'){
                            $cmd = "ffmpeg -i $input -vcodec h264 -acodec aac -strict -2 $o";
                            //$cmd = "ffmpeg -i $input -vcodec h264 -acodec aac -s $size $o";
                            shell_exec($cmd);
                            $toclear[] = array('file' => $file);
                        }
                        $cmd = "ffmpeg -ss $pos -i $o -an -s $size $output";
                        shell_exec($cmd);
                        $total_time += $pos;
                        $succedeed[] = array('name' => $name,'file' => 'thumbs/'.$_file.'.jpg', 'type' => 'mp4');                           
                    }else{
                        $failed[] = array('name' => $name, 'file' => $file, 'error' => 'Video length cannot exceed 20mins.');
                    }           
                }
            }else{
                $failed[] = array('name' => $name, 'file' => $file, 'error' => 'File type not allowed');
            }
        }
    }

检查您的php ini并设置最大执行时间。在我的例子中,5分钟的视频比30分钟的ffmpeg转换要多。您可以使用ajax调用您的"转换器" php脚本,并放置一个"加载器"与jscript。