2010-10-12 140 views
6

我想用这个脚本来创建视频的缩略图,使用ffmpeg。起初我使用phpinfo();,我发现ffmpeg安装在我的服务器上。ffmpeg-php创建视频缩略图

然后我复制的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 

我写了这段代码和trie d任何可能的路径分配$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>&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!'; 
?> 
+2

嗯...有什么问题吗? – deceze 2010-10-12 05:00:23

+0

问题是脚本不能正常工作。 – Wazzzy 2010-10-12 05:05:46

+2

然后请提供更多信息,兄弟。任何错误消息?命令是否从命令行工作? – deceze 2010-10-12 05:10:51

回答

7

最后我得到的代码感谢Anubhaw。您的联系帮lot.Try此代码。

  //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,$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

它有帮助...试试这个... – Wazzzy 2011-03-28 12:28:26

+0

你可以看看我的问题请http:// stackoverflow.com/questions/27189948/ffmpeg-is-not-creating-screenshot-from-video – Hitesh 2014-11-28 13:53:18

5

看了看代码,使它更加简单和可运行的,也许这可以帮助你们中的一些。

class VideoTile 
{ 
    public static function createMovieThumb($srcFile, $destFile = "test.jpg") 
    { 
     // Change the path according to your server. 
     $ffmpeg_path = 'D:\\ffmpeg\\bin\\'; 

     $output = array(); 

     $cmd = sprintf('%sffmpeg -i %s -an -ss 00:00:05 -r 1 -vframes 1 -y %s', 
      $ffmpeg_path, $srcFile, $destFile); 

     if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) 
      $cmd = str_replace('/', DIRECTORY_SEPARATOR, $cmd); 
     else 
      $cmd = str_replace('\\', DIRECTORY_SEPARATOR, $cmd); 

     exec($cmd, $output, $retval); 

     if ($retval) 
      return false; 

     return $destFile; 
    } 
} 

用法

$file = VideoTile::createMovieThumb("../video3.mp4");