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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

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

    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.
    Code:
    [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]

    #2
    PHP Code:
    <?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

    Comment


      #3
      PHP Code:
      <?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 Code:
      <?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

      Comment


        #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.

        Code:
        <?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);
            }
        }
        ?>
        Last edited by hashid; 04.02.12, 18:51.

        Comment


          #5
          Originally posted by hashid View Post
          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.

          Code:
          <?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?

          Comment


            #6
            Ofcourse not, you need to have FFmpeg installed.

            Comment

            Working...
            X