Create Video Thumbnail of All Files in a Directory via FFmpeg and PHP

自古美人都是妖i 提交于 2019-12-05 08:06:09

问题


I have searched all over the Google and StackOverFlow, but still did not find a solution for this.

I want to generate video thumbnail of all mp4 video files in a directory and name the thumbnails as "filename.mp4".jpg

I have ffmpeg and ffmpeg-php installed on my server. I also succeeded in creating thumbnails of one file at a time.

So this is the situation, I have a directory named uploads which has lots of mp4 videos. Now, when I run the script, thumbnail of size 100x100 shoud be created automatically and placed in another folder "skrin". Eg: xxx.mp4 should have xxx.mp4.jpg has the thumb name.

IMPORTANT: My filenames have spaces, single quotes, brackets etc in their file names. So the script should be able to handle this.

Could some one help me ? I use the following shell command in php using exec to generate thumb of an individual video.

exec("/usr/local/bin/ffmpeg -itsoffset -105 -i 'xxx haha.mp4' -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 'xxx haha.mp4.jpg'");

回答1:


It's just a quick one:

$videos_dir = 'path/to/videos';
$videos_dir = opendir($videos_dir);
$output_dir = 'path/to/output/dir/';
while (false !== ($file = readdir($videos_dir))) {
    if ($file != '.' && $file != '..'){
        $in = $videos_dir.'/'.$file;
        $out = $output_dir.$file.'.jpg';
        exec("/usr/local/bin/ffmpeg -itsoffset -105 -i ".$in." -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 ".$out);
    }
}



回答2:


try this

try
    {
        $directory = 'your directory name';
        $dir = new RecursiveDirectoryIterator($directory);
        $it = new RecursiveIteratorIterator($dir);
        while($it->valid()) {

            if (!$it->isDot()) {
                //echo 'SubPathName: ' . $it->getSubPathName() . "\n";
                //echo 'SubPath:     ' . $it->getSubPath() . "\n";
                //echo 'Key:         ' . $it->key() . "\n\n";
                echo $name = $it->key(),"\n";
                exec("/usr/local/bin/ffmpeg -itsoffset -105 -i $name -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 $name.'.jpg'");    
            }

            $it->next();
        }
    }
    catch(Exception $e)
    {
        echo 'No files Found!<br />';
    }


来源:https://stackoverflow.com/questions/9107710/create-video-thumbnail-of-all-files-in-a-directory-via-ffmpeg-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!