1

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'");
Hashid Hameed
  • 878
  • 1
  • 8
  • 23

2 Answers2

3

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);
    }
}
Sal
  • 1,657
  • 12
  • 9
  • Almost exactly what I was going to post. You beat me to it by 30 seconds. :) – ghoti Feb 02 '12 at 06:04
  • Only catch is that the OP wants the output files to be named *.jpg. – ghoti Feb 02 '12 at 15:11
  • I have space in my video title. How should I deal with it? @ghoti – Vaibhav Kadam Oct 24 '17 at 09:01
  • @VaibhavKadam ... The command line being run by the `exec()` function in this answer fails to quote the filenames (`$in` and `$out`). Add quotes, and you should be good to go. Better yet, for more clarity, construct your command using `sprintf()` instead of duct-taping strings together with the dot operator. – ghoti Oct 24 '17 at 13:58
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 />';
    }
maxjackie
  • 22,386
  • 6
  • 29
  • 37