ffmpeg-php to create thumbnail of video

前端 未结 2 1721
一个人的身影
一个人的身影 2020-12-28 12:05

I am trying to use this script to create thumbnail of a video using ffmpeg. At first I used phpinfo(); and I found ffmpeg is installed on my se

2条回答
  •  Happy的楠姐
    2020-12-28 12:50

    At last I got the code thanks to Anubhaw.Your link helped lot.Try this code.

                //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 .= "

    Cmd line :
    " . nl2br(htmlspecialchars($cmd)) . "
    "; $ERROR .= "

    The ffmpeg program said:
    "; $ERROR .= nl2br(htmlspecialchars($output)); $ERROR .= "
    "; } @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; }

    Enjoy Coding

    with regards,

    Wasim

提交回复
热议问题