PHP视频上传和FFmpeg无法创建缩略图

4

我正在为我的项目制作视频上传功能。但是我在ffmpeg部分遇到了问题。 ffmpeg已经安装在我的服务器上。但是我无法获得任何缩略图。我尝试使用以下代码创建缩略图:

$videoa = exec("/usr/bin/ffmpeg  -i $videoUrlp.flv -f flv -s 650x390 $videoUrlp.mp4 2>&1");
$videob = exec("/usr/bin/ffmpeg  -i $videoUrlp.mp4 -vcodec png -ss 00:00:5 -s 650x390 -vframes 1 -an -f rawvideo $videoUrlp.png");

当我对$videoa和$videob使用var_dump();时,会出现以下信息:

string(74) "https://mywebsite.com/uploads/video/ey1kXNew_video.flv:Input/output error" string(0) ""

此外,如果我像这样使用shell_exec
$videoa = shell_exec("/usr/bin/ffmpeg  -i $videoUrlp.flv -f flv -s 650x390 $videoUrlp.mp4");
$videob = shell_exec("/usr/bin/ffmpeg  -i $videoUrlp.mp4 -vcodec png -ss 00:00:5 -s 650x390 -vframes 1 -an -f rawvideo $videoUrlp.png");

当我使用var_dumps()时,结果显示为NULLNULL

请帮我找出错误所在。

以下是我的完整视频上传代码:

$valid_formats = array("mp4","MP4","flv");
            if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
               $name = $_FILES['uploading']['name'];
               $size = $_FILES['uploading']['size'];
               if(strlen($name)) {
                   $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION)); 
                   $name = alphaID(microtime(true) * 10000).'_video';
                   if(in_array($ext,$valid_formats)) {
                   if($size<(50024*50024)) {
                      $GetVideoName = $name;
                      $video_ext=$ext;
                       $tmp = $_FILES['uploading']['tmp_name'];
                       if(move_uploaded_file($tmp, $videoPath.$GetVideoName.'.'.$video_ext)) {
                           $videoUrlp = $base_url.'uploads/video/'.$GetVideoName;
                           $videoa = exec("/usr/bin/ffmpeg  -i $videoUrlp.flv -f flv -s 650x390 $videoUrlp.mp4 2>&1");
                           $videob = exec("/usr/bin/ffmpeg  -i $videoUrlp.mp4 -vcodec png -ss 00:00:5 -s 650x390 -vframes 1 -an -f rawvideo $videoUrlp.png");
                           echo var_dump($videoa);
                           echo var_dump($videob);

                        } else {
                            echo "Fail upload folder with read access.";
                        }
                     } else
                        echo "Image file size max 1 MB";                    
                     } else
                        echo "invalidvieo"; 
                 } else
                    echo "Please select image..!";
                 exit;
              }

我也试过这种方法:

echo exec("/usr/bin/ffmpeg -i $videoUrlp.flv -ar 22050 -ab 32 -f flv -s 780x400 $videoUrlp.$video_ext");
echo exec("/usr/bin/ffmpeg -i $videoUrlp.$video_ext -deinterlace -an -ss 1 -t 00:00:05 -r 1 -y -vcodec mjpeg -f mjpeg $videoUrlp.png 2>&1");

我看到了以下信息:

视频: 71kB 音频: 0kB 字幕: 0kB 其他流: 0kB 全局头文件: 0kB 复用开销: 0.000000%

再试一次后,我收到了此提示:https://mywebsite.com/uploads/video/1547892356_video.flv: 连接被拒绝

echo exec("/usr/bin/ffmpeg -i $videoUrlp.flv -ar 22050 -ab 32 -f flv -s 780x400 $videoUrlp.$video_ext");
echo exec("/usr/bin/ffmpeg -i $videoUrlp.$video_ext -deinterlace -an -ss 1 -t 00:00:05 -r 1 -y -vcodec mjpeg -f mjpeg $videoUrlp.png 2>&1");

以下是上一次的shell_exec输出:

 $local = 'https://website.com/uploads/video/'.$GetVideoName.'.'.$video_ext;
 $localTumb = 'https://website.com/uploads/video/'.$GetVideoName.'.png';
 echo shell_exec("/usr/bin/ffmpeg -i $local -deinterlace -an -ss 1 -t 00:00:05 -r 1 -y -vcodec mjpeg -f mjpeg $localTumb 2>&1");    

ffmpeg版本2.8.15版权所有(c)2000-2018年FFmpeg开发人员,使用gcc 4.8.5(GCC)20150623(Red Hat 4.8.5-36)构建。配置为:--prefix=/usr --bindir=/usr/bin
--datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --extra-ldflags='-Wl,-z,relro ' --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc --enable-version3 --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect。libavutil 54. 31.100 / 54. 31.100 libavcodec 56. 60.100 / 56. 60.100 libavformat 56. 40.101 / 56. 40.101 libavdevice 56. 4.100 / 56. 4.100 libavfilter 5. 40.101 / 5. 40.101 libavresample 2. 1. 0 / 2. 1. 0 libswscale 3. 1.101 / 3. 1.101 libswresample 1. 2.101 / 1. 2.101 libpostproc 53. 3.100 / 53. 3.100 输入#0,mov,mp4,m4a,3gp,3g2,mj2,来自'https://website.com/uploads/video/e0J6HwtK_video.mp4':元数据: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 creation_time : 2018-01-01 12:09:49 持续时间:00:00:41.49,开始: 0.000000,比特率:230 kb/s 流#0:0(und):视频:h264(受限基线)(avc1 / 0x31637661),yuv420p,240x360 [SAR 1:1 DAR 2:3],158 kb/s,9.73 fps,9.73 tbr,19462 tbn,19.46 tbc(默认)元数据: handler_name : VideoHandler 流#0:1(und):音频:aac(LC)(mp4a / 0x6134706D),44100 Hz,单声道,fltp,71 kb/s(默认值)元数据: creation_time : 2018-01-01 12:09:49 handler_name : IsoMedia File Produced by Google,5-11-2011 [swscaler @ 0xc8c580]使用了过时的像素格式,请确保正确设置范围 输出#0,mjpeg, 到'https://website.com/uploads/video/e0J6HwtK_video.png':元数据: major_brand : mp42 minor

1
如果您在shell中运行FFMPEG命令会发生什么?此外,要创建一个MVC示例,您可以首先尝试在新的PHP中运行以下命令:var_dump(exec('/usr/bin/ffmpeg -version')); - Raptor
@Raptor 打印输出如下:string(38) "libpostproc 53. 3.100 / 53. 3.100" - AlwaysStudent
@Raptor 在 var_dump(); 中使用 shell_exec 函数返回 NULL NULL - AlwaysStudent
1
如果这个一行的示例能够给出有效的输出(就像你刚刚测试的那样),问题只在于FFMPEG命令本身。尝试直接在shell中运行它,而不是通过PHP。 - Raptor
1
在您的计算机/服务器上运行。在基于Linux的机器上使用终端/Shell控制台。这是Mac还是Linux? - Raptor
显示剩余10条评论
4个回答

6

5

参数必须进行转义,不能将变量直接放入参数中。URL包含一些特殊字符,例如&,在shell中具有特殊含义。例如:

exec("/usr/bin/ffmpeg -i $videoUrlp.flv -f flv -s 650x390 $videoUrlp.mp4 2>&1");

应更改为:

exec("/usr/bin/ffmpeg -i ".escapeshellarg($videoUrlp.flv)." -f flv -s 650x390 ".escapeshellarg($videoUrlp.mp4)." 2>&1");

但是...

请问是否有人能帮我?

这里有两个函数用于获取缩略图,getThumbPng2()会使用curl下载整个视频并将其保存到临时文件中,然后使用ffmpeg提取URL(如果ffmpeg没有构建http / https / whatever支持或者ffmpeg被防火墙拦截等,则这样做更安全,但缺点是潜在的速度较慢)

第一个函数从ffmpeg中获取缩略图而不是先获取视频(这可能要快得多,因为ffmpeg只需读取所需部分而不是下载整个视频,但由于上述原因可能无法正常工作)。

function getThumbPng2(string $uri, string &$imageBinary=NULL, string &$stderr=NULL):bool{
    $ch=curl_init($uri);
    if(!$ch){
        $imageBinary=$stderr="curl_init() failed";
        return false;
    }
    $tmph=tmpfile();
    curl_setopt_array($ch,array(
        CURLOPT_SSL_VERIFYPEER=>false,
        CURLOPT_SSL_VERIFYHOST=>false,
        CURLOPT_FILE=>$tmph
    ));
    if(!curl_exec($ch)){
        $imageBinary=$stderr="curl error: ".curl_errno($ch).": ".curl_error($ch);
        curl_close($ch);
        fclose($tmph);
        return false;
    }
    curl_close($ch);
    $ret=getThumbPng(stream_get_meta_data($tmph)['uri'],$imageBinary,$stderr);
    fclose($tmph);
    return $ret;
}
function getThumbPng(string $uri,string &$imageBinary=NULL, string &$stderr=NULL):bool{
    $imageBinary="";
    $stderr="";
    $descriptorspec = array(
        0 => array("pipe", "rb"),  // stdin
        1 => array("pipe", "wb"),  // stdout
        2 => array("pipe","wb") // stderr 
    );
    $cmd="ffmpeg -i ".escapeshellarg($uri)." -ss 00:00:3 -s 650x390 -vframes 1 -c:v png -f image2pipe -";
    // var_dump($cmd);
    $proc = proc_open($cmd, $descriptorspec, $pipes);
    if(!$proc){
        $stderr="failed to start ffmpeg, proc_open(\"ffmpeg\",...) failed. (why? don't know, and PHP has no way of telling us either, afaik.)";
        return false;
    }
    fclose($pipes[0]);// by default stdin is *inherited*, which is not what we want,
    // so to avoid the default behaviour, we explicitly create a stdin pipe and close it.
    $fetch=function()use(&$pipes,&$imageBinary,&$stderr){
        $tmp=stream_get_contents($pipes[1]);
        if(is_string($tmp)){
            $imageBinary.=$tmp;
        }
        $tmp=stream_get_contents($pipes[2]);
        if(is_string($tmp)){
            $stderr.=$tmp;
        }   
    };
    while(($status=proc_get_status($proc))['running']){
        usleep(10*1000);
        $fetch();
    }
    $fetch();
    fclose($pipes[1]);
    fclose($pipes[2]);
    $ret=$status['exitcode'];
    proc_close($proc);
    //var_dump($ret);
    return ($ret===0);
}

示例用法:

<?php
if(getThumbPng("http://files.4x4norway.no/2018/vik2.webm",$imageBinary,$stderr)){
    echo "got a thumbnail! (the png binary data is in the \$imageBinary variable.)";
}else{
    var_dump($imageBinary);
    echo "failed to create thumbnail! error: {$stderr}";
}

3
使用以下代码: ` //缩略图路径应该添加在下面的代码中 //测试缩略图

      $dir_img='uploads/';
      $mediapath='123.jpg';

      $file_thumb=create_movie_thumb($dir_img.$mediapath,$mediapath,$mediaid);

        $name_file=explode(".",$mediapath);
        $imgname="thumb_".$name_file[0].".jpg";     



        /*
          Function to create video thumbnail using ffmpeg
        */
        function create_movie_thumb($src_file,$mediapath,$mediaid)
        {
            global $CONFIG, $ERROR;

            $CONFIG['ffmpeg_path'] = '/usr/local/bin/'; // Change the path according to your server.
            $dir_img='uploads/';
            $CONFIG['fullpath'] = $dir_img."thumbs/";

            $src_file = $src_file;
            $name_file=explode(".",$mediapath);
            $imgname="thumb_".$name_file[0].".jpg";
            $dest_file = $CONFIG['fullpath'].$imgname;

            if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
                // get the basedir, remove '/include'
                $cur_dir = substr(dirname(__FILE__), 0, -8);
                $src_file = '"' . $cur_dir . '\\' . strtr($src_file, '/', '\\') . '"';
                $ff_dest_file = '"' . $cur_dir . '\\' . strtr($dest_file, '/', '\\') . '"';
            } else {
                $src_file = escapeshellarg($src_file);
                $ff_dest_file = escapeshellarg($dest_file);
            }

            $output = array();

            if (eregi("win",$_ENV['OS'])) {
                // Command to create video thumb
                $cmd = "\"".str_replace("\\","/", $CONFIG['ffmpeg_path'])."ffmpeg\" -i ".str_replace("\\","/" ,$src_file )." -an -ss 00:00:05 -r 1 -vframes 1 -y ".str_replace("\\","/" ,$ff_dest_file);
                exec ("\"$cmd\"", $output, $retval);

            } else {
                // Command to create video thumb
                $cmd = "{$CONFIG['ffmpeg_path']}ffmpeg -i $src_file -an -ss 00:00:05 -r 1 -vframes 1 -y $ff_dest_file";
                exec ($cmd, $output, $retval);

            }


            if ($retval) {
                $ERROR = "Error executing FFmpeg - Return value: $retval";
                if ($CONFIG['debug_mode']) {
                    // Re-execute the command with the backtick operator in order to get all outputs
                    // will not work if safe mode is enabled
                    $output = `$cmd 2>&1`;
                    $ERROR .= "<br /><br /><div align=\"left\">Cmd line : <br /><span style=\"font-size:120%\">" . nl2br(htmlspecialchars($cmd)) . "</span></div>";
                    $ERROR .= "<br /><br /><div align=\"left\">The ffmpeg program said:<br /><span style=\"font-size:120%\">";
                    $ERROR .= nl2br(htmlspecialchars($output));
                    $ERROR .= "</span></div>";
                }
                @unlink($dest_file);
                return false;
            }

            $return = $dest_file;
            //@chmod($return, octdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled
            return $return;
        }`

0
我认为你可以在服务器上检查是否安装了ffmpeg文件。
$ffmpeg = trim(shell_exec('whereis ffmpeg')); // This will show you which path FFMPEG 

if (empty($ffmpeg))
{
    die('ffmpeg not available');
}else{
    echo $ffmpeg;
}

那么试试这个:

$cmd = shell_exec("$ffmpeg -i $videoUrlp.mp4 -ss 00:00:02.000 -vframes 1 $videoUrlp.png");


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接