ffmpeg-php创建视频缩略图

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了ffmpeg-php创建视频缩略图脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用此脚本使用ffmPEg创建视频缩略图.起初我使用了PHPinfo();我发现ffmpeg安装在我的服务器上.

然后我将ffmpeg-PHP复制到我的服务器上并运行测试文件

知道很多功能正在我的服务器上运行

我有这样的输出

输出: –

Functions available in /usr/lib/PHP/extensions/no-debug-non-zts-20060613/ffmpeg.so extension:

Methods available in class ffmpeg_movie:
__construct
getduration
getframecount
getframerate
getfilename
getcomment
gettITle
getauthor
getartist
getcopyright
getalbum
getgenre
getyear
gettracknumber
getframewidth
getframeheight
getframenumber
getpixelformat
getbitrate
hasaudio
hasvideo
getnextkeyframe
getframe
getvideocodec
getaudiocodec
getvideostreamid
getaudiostreamid
getaudiochannels
getaudiosamplerate
getaudiobitrate
getvideobitrate
getpixelaspectratio
getpixelaspectratio
getvideobitrate
getaudiobitrate
getaudiosamplerate
getaudiochannels
getaudiostreamid
getvideostreamid
getaudiocodec
getvideocodec
getframe
getnextkeyframe
hasvideo
hasaudio
getbitrate
getpixelformat
getframenumber
getframeheight
getframewidth
gettracknumber
getyear
getgenre
getalbum
getcopyright
getartist
getauthor
gettitle
getcomment
getfilename
getframerate
getframecount
getduration
__construct

我编写了这段代码并尝试了任何可能的路径来分配$ffmpeg;

<?PHP

    $thumb_stdout;
    $retval=0;
    $ffmpeg = '/home/lib/ffmpeg';

// change "demo.mpg" to your mpg file name!
$video  = dirname(__FILE__) . 'demo.mpg';

// change "demo.jpg" to whichever name you like or don't
// for this example,the name of the output jpg file does not matter
$image  = dirname(__FILE__) . 'demo.jpg';

$second = 1;

$cmd = "$ffmpeg -i $video 2>&amp;1";
if (PReg_match('/Duration: ((\d+):(\d+):(\d+))/s','$cmd',$time)) {
    $total = ($time[2] * 3600) + ($time[3] * 60) + $time[4];
    $second = rand(1,($total - 1));
}

//$cmd = "$ffmpeg -i $video -deinterlace -an -ss $second -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg $image 2>&1";
$cmd = "$ffmpeg -i $video -r 1 -ss 00:00:05 -t 00:00:01 -s 250x250 -f image2 $image";
echo $cmd;
exec($cmd);
//$return = '$cmd';

echo '<br>done!';
?>
最后我得到了代码感谢Anubhaw.Your链接帮助了很多.试试这段代码.
//thumb path should be added in the below code
            //test for thumb
            $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,$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__),-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,$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;
            }

享受编码

带着敬意,

瓦西姆

脚本宝典总结

以上是脚本宝典为你收集整理的ffmpeg-php创建视频缩略图全部内容,希望文章能够帮你解决ffmpeg-php创建视频缩略图所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。