How to create thumbnails for all videos in a directory (ffmpeg & php) EATING MY HEAD!

Started on the old forum in February 2012 · view the original in the archive · old links redirect here

#ffmpeg #ffmpeg-php #php #thumbnail #video-thumbnail

5 replies · 3,928 views · started 14 years ago · 1 min read

3 participants
hashid Junior · 21 posts #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.

[h=2][SIZE=3]exec("/usr/local/bin/ffmpeg -itsoffset -105 -i 'xxx haha.mp4'  -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 'xxx  haha.mp4.jpg'");[/SIZE][/h]
softwarefreak Senior · 349 posts #2
<?php

$file=htmlspecialchars(@$_GET['file']);

if (!class_exists(ffmpeg_movie))
{
die('ffmpeg is not supported!');
}

//creating an object (a test file test.3gp)

if (file_exists($file))
{
$mov = new ffmpeg_movie($file);
}
else
{
die('Error !! Can not find file !!');
}

//frame number
$frame = 10;

//width
$w = $mov->GetFrameWidth();

//height
$h = $mov->GetFrameHeight();

//extract frame
$ff_frame = $mov->getFrame($frame);

if ($ff_frame)
{
//format GD
$gd_image = $ff_frame->toGDImage();

if ($gd_image)
{
//output (for example, in the gif)
header('Content-type: image/gif');
imagegif($gd_image);

/*You can make a change in size, causing copyrights and watermarks, in general all that apply for GD*/
}
else
{
die('cannot be converted to GD');
}
}
else
{
die('can not extract frame');
}

/*
descriptions of all methods ffmpeg_movie look here
http://ffmpeg-php.sourceforge.net/doc/api/ffmpeg_movie.php
*/
?> 

You may try the above script for creating thumbnails on the fly

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

GumSlone Administrator Principal · 1,690 posts #3
<?php
$dir = "./path/videos/";
$thumb_dir = "./path/videos/scrin/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            $file = rawurlencode($file); // not sure about this but i think it should work for files with spaces
            $ext = strtolower(end(explode('.', $file)));
            if($ext=='mp4')
                @exec("/usr/local/bin/ffmpeg -i '".$dir.$file."' -f image2 -ss 4.00 -vframes 1 -pix_fmt jpeg '".$thumb_dir.$file.".jpg'");
        }
        closedir($dh);
    }
}
?>

or with your command:

<?php
$dir = "./path/videos/";
$thumb_dir = "./path/videos/scrin/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            $file = rawurlencode($file); // not sure about this but i think it should work for files with spaces
            $ext = strtolower(end(explode('.', $file)));
            if($ext=='mp4')
               exec("/usr/local/bin/ffmpeg -itsoffset -105 -i '".$dir.$file."'  -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 '".$thumb_dir.$file.".jpg'");
        }
        closedir($dh);
    }
}
?>

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

hashid Junior · 21 posts #4

Thanks alot softwarefreak and GumSlone. Unfortunately GumSlone's code is not working for filenames with spaces. And code by softwarefreak generates thumbnail on the fly. I think it eats up CPU load as ffmpeg gets executed for each user. What I asked was, to generate thumbnails of all videos in folder specified and save the thumbnail files as "filename.ext.jpg". Let me give a try again. 🙂 Thanks again.

Added after 16 minutes:

Ok, I got it working finally! I just escaped spaces, hyphens and brackets with backslash using str_replace.

<?php
$dir = "/full path to directory/";
$thumb_dir = "/full path to directory for thumbnails/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
$file=str_replace(" ", "\ ", $file);
$file=str_replace("-", "\-", $file);
$file=str_replace("(", "\(", $file);
$file=str_replace(")", "\)", $file);
echo $file."<br/>";
//$file=str_replace("'", "\ ", $file);
            $ext = strtolower(end(explode('.', $file)));
            if($ext=='mp4')
               exec("/usr/local/bin/ffmpeg -itsoffset -105 -i ".$dir.$file."  -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 ".$thumb_dir.$file.".jpg");
        }
        closedir($dh);
    }
}
?> 
Guest #5

hashid wrote: Thanks alot softwarefreak and GumSlone. Unfortunately GumSlone's code is not working for filenames with spaces. And code by softwarefreak generates thumbnail on the fly. I think it eats up CPU load as ffmpeg gets executed for each user. What I asked was, to generate thumbnails of all videos in folder specified and save the thumbnail files as "filename.ext.jpg". Let me give a try again. 🙂 Thanks again.

Added after 16 minutes:

Ok, I got it working finally! I just escaped spaces, hyphens and brackets with backslash using str_replace.

<?php
$dir = "/home/hashid/public_html/Mp4_Song_Videos/uploads/IndiPop/";
$thumb_dir = "/home/hashid/public_html/Mp4_Song_Videos/uploads/IndiPop/";
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
$file=str_replace(" ", "\ ", $file);
$file=str_replace("-", "\-", $file);
$file=str_replace("(", "\(", $file);
$file=str_replace(")", "\)", $file);
echo $file."<br/>";
//$file=str_replace("'", "\ ", $file);
            $ext = strtolower(end(explode('.', $file)));
            if($ext=='mp4')
               exec("/usr/local/bin/ffmpeg -itsoffset -105 -i ".$dir.$file."  -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 ".$thumb_dir.$file.".jpg");
        }
        closedir($dh);
    }
}
?> 

how do this script work? i try to save this as test.php and execute it nothing happen. its work with server that dont support ffmpeg?

hashid Junior · 21 posts #6

Ofcourse not, you need to have FFmpeg installed.

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close