Advanced search  

News:

cpg1.5.30 maintenance release - upgrade recommended
The Coppermine development team is releasing an update for Coppermine in order to fix several minor issues. All fixes are not security critical, so if your gallery is running fine with cpg1.5.28 you don't need to upgrade. If you are running an older version than cpg1.5.28, you must update to this latest version as soon as possible because of the security impact!
[more]

Pages: [1] 2 3   Go Down

Author Topic: Automatic video thumbnail creation (Requires ffmpeg)  (Read 98750 times)

0 Members and 1 Guest are viewing this topic.

Abbas Ali

  • Administrator
  • Coppermine addict
  • *****
  • Country: in
  • Offline Offline
  • Gender: Male
  • Posts: 2158
  • Spread the PHP Web
    • Ranium Systems
Automatic video thumbnail creation (Requires ffmpeg)
« on: November 24, 2006, 12:56:30 PM »

This mod requires ffmpeg pre-installed and working on your server. Please DO NOT ask for support on installing or configuring ffmpeg. If you don't have ffmpeg or can't install ffmpeg on your server then this mod is not meant for you.

What will this mod do?
This mod will create thumbnail from the uploaded video file automatically on the fly. Thumbnails will be placed in the directory where the videos are uploaded/batch added. Though ffmpeg supports many video formats but i am unsure about wmv. File formats which i tested were avi, mpg, asf, flv.

Edit include/picmgmt.inc.php

Add

Code: [Select]
/**
 * Function to create video thumbnail using ffmpeg
 */
function create_movie_thumb($src_file)
{
    global $CONFIG, $ERROR;

    $CONFIG['ffmpeg_path'] = '/usr/local/bin/'; // Change the path according to your server.

    $src_file = $src_file;
    $dest_file = $CONFIG['fullpath']."edit/".md5(uniqid(time()))."%d.jpg";

    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 .= "<br /><br /><div align=\"left\">Cmd line : <br /><span style=\"font-size:120%\">" . nl2br(htmlspecialchars($cmd)) . "</span></div>";
            $ERROR .= "<br /><br /><div align=\"left\">The ffmpeg program said:<br /><span style=\"font-size:120%\">";
            $ERROR .= nl2br(htmlspecialchars($output));
            $ERROR .= "</span></div>";
        }
        @unlink($dest_file);
        return false;
    }

    $return = str_replace("%d", "1", $dest_file);
    @chmod($return, octdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled
    return $return;
}

just before

Code: [Select]
// Add a picture to an album
function add_picture($aid, $filepath, $filename, $position = 0, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '', $iwidth = 0, $iheight = 0)

In above code change $CONFIG['ffmpeg_path'] value to where ffmpeg is installed on your server.

Add

Code: [Select]
    } elseif (is_movie($image)) {
      preg_match("/(.+)\.(.*?)\Z/", $filename, $matches);
      $thumb = $CONFIG['fullpath'] . $filepath . $CONFIG['thumb_pfx'] . $matches[1].".jpg";
      $videoThumb = create_movie_thumb($image);
      if ($videoThumb) {
        if (!resize_image($videoThumb, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use'])) {
          return false;
        }
        @unlink($videoThumb);
        $imagesize = getimagesize($thumb);
      } else {
        return false;
      }

just before

Code: [Select]
    } else {
        $imagesize[0] = $iwidth;
        $imagesize[1] = $iheight;
    }

That's it, only one file to be modified.


Few Notes
  • The mod works best on linux servers
  • If you have Dreamhost as your webhost then this might help you
  • The dreamhost installation way might work on other shared servers too but not sure. Give it a try.
  • If you have a dedicated server then ffmpeg will not be that difficult for your webhost to install
« Last Edit: November 24, 2006, 02:10:42 PM by GauGau »
Logged
Chief Geek at Ranium Systems

jape

  • Coppermine newbie
  • Offline Offline
  • Posts: 8
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #1 on: January 09, 2007, 02:13:05 PM »

Why not working .flv videos???
Thumnail name is problems...

I trying upload video.FLV

but...

Thumb location is:
/gallery/images/thumb_FLV.jpg


Not Found

The requested URL /galleria/images/thumb_FLV.jpg was not found on this server.

Logged

jape

  • Coppermine newbie
  • Offline Offline
  • Posts: 8
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #2 on: January 09, 2007, 02:26:28 PM »

Its working  ;D
But not why...  ;D ;D
Logged

jape

  • Coppermine newbie
  • Offline Offline
  • Posts: 8
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #3 on: January 09, 2007, 03:26:40 PM »

ahaa....  :D
This donīt working thumbnails: video.FLV
This working: video.flv
Logged

Abbas Ali

  • Administrator
  • Coppermine addict
  • *****
  • Country: in
  • Offline Offline
  • Gender: Male
  • Posts: 2158
  • Spread the PHP Web
    • Ranium Systems
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #4 on: January 10, 2007, 07:57:07 AM »

Not sure why that's happening but ffmpeg might not be able to create thumbs with capitalized extension. Better always use lower case extensions.
Logged
Chief Geek at Ranium Systems

adrianbj

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Posts: 34
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #5 on: January 10, 2007, 08:10:21 AM »

Hey Abbas,

Fantastic Hack - thanks.

I did notice one thing though. If you want the dimensions of the movie to be properly collected, you need to change

Code: [Select]
    } elseif (is_movie($image)) {
      preg_match("/(.+)\.(.*?)\Z/", $filename, $matches);
      $thumb = $CONFIG['fullpath'] . $filepath . $CONFIG['thumb_pfx'] . $matches[1].".jpg";
      $videoThumb = create_movie_thumb($image);
      if ($videoThumb) {
        if (!resize_image($videoThumb, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use'])) {
          return false;
        }
        @unlink($videoThumb);
        $imagesize = getimagesize($thumb);
      } else {
        return false;
      }

to:

Code: [Select]
    } elseif (is_movie($image)) {
      preg_match("/(.+)\.(.*?)\Z/", $filename, $matches);
      $thumb = $CONFIG['fullpath'] . $filepath . $CONFIG['thumb_pfx'] . $matches[1].".jpg";
      $videoThumb = create_movie_thumb($image);
      $imagesize = getimagesize($videoThumb);
      if ($videoThumb) {
        if (!resize_image($videoThumb, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use'])) {
          return false;
        }
        @unlink($videoThumb);
        } else {
        return false;
      }

Note that I have removed your getimagesize line and replaced it further up in the code and changed the variable from $thumb to $videoThumb

The way you had it, it always grabbed the dimensions of the thumbnail, rather than the actual fullsize video.

I also commented out these lines in upload.php:

Code: [Select]
        //Add width and height boxes to the form.
        //$form_array[] = array($lang_admin_php['th_wd'],'movie_wd', 0, 4, 1); //no longer needed since dimensions of movies are grabbed automatically
        //$form_array[] = array($lang_admin_php['th_ht'],'movie_ht', 0, 4, 1); //no longer needed since dimensions of movies are grabbed automatically

because it is no longer necessary for the user to enter the movie dimensions manually.

Thanks again,
Adrian
Logged

adrianbj

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Posts: 34
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #6 on: January 10, 2007, 08:11:46 AM »

I noticed the capitalized extension problem too!
Logged

Abbas Ali

  • Administrator
  • Coppermine addict
  • *****
  • Country: in
  • Offline Offline
  • Gender: Male
  • Posts: 2158
  • Spread the PHP Web
    • Ranium Systems
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #7 on: January 10, 2007, 09:23:57 AM »

Thanks adrianbj for enhancing the mod.
Logged
Chief Geek at Ranium Systems

adrianbj

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Posts: 34
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #8 on: January 11, 2007, 09:31:27 AM »

No problem,

Here is another addition to ensure that the thumbnail is deleted along with movie if a delete takes place.

In the file delete.php

Add this:

Code: [Select]
//Added by AJ to delete thumb and preview versions for movies
$file_normal = $CONFIG['normal_pfx'] . $file;
$file_thumb = $CONFIG['thumb_pfx'] . $file;

if(is_movie($file)){
$path_parts = pathinfo($file);
$file_ext = '.' . $path_parts['extension'];
$file_normal = str_replace($file_ext, ".jpg", $file_normal);
$file_thumb = str_replace($file_ext, ".jpg", $file_thumb);
}
//

directly after these lines:

Code: [Select]
    $aid = $pic['aid'];
    $dir = $CONFIG['fullpath'] . $pic['filepath'];
    $file = $pic['filename'];


Change this:

Code: [Select]
$files = array($dir . $file, $dir . $CONFIG['normal_pfx'] . $file, $dir . $CONFIG['thumb_pfx'] . $file);

to this:

Code: [Select]
$files = array($dir . $file, $dir . $file_normal, $dir . $file_thumb); // Modified by AJ - changed links to normal and thumb version to grab corrected links for movies and special images etc
That's it!
« Last Edit: January 16, 2007, 11:53:17 PM by adrianbj »
Logged

adrianbj

  • Contributor
  • Coppermine novice
  • ***
  • Offline Offline
  • Posts: 34
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #9 on: January 17, 2007, 12:01:15 AM »

Here's another addition so you get to see the preview thumbnail during the upload process - really useful if you've uploaded several files and you're not sure which is which when you're entering the info about the movie.

In upload.php, add

Code: [Select]
    // Added by AJ for handling preview thumb for movie images
   } elseif(is_movie($file_set[1])) {
   
   
       $CONFIG['ffmpeg_path'] = '/usr/bin/'; // Change the path according to your server.

        // Create preview image file name.

        do {

            // Create a random seed by taking the first 8 characters of an MD5 hash of a concatenation of the current UNIX epoch time and the current server process ID.
            $seed = substr(md5(uniqid("")), 0, 8);

            // Assemble the file path.
$extension="jpg"; //change temp thumbsized preview file to a JPG
            $path_to_preview = './'.$CONFIG['fullpath'].'edit/preview_' . $seed . '%d.' . $extension;

        } while (file_exists($path_to_preview));

        // Create secure preview path.
        $s_preview_path = 'preview_' . $seed . '.' . $extension;

    $output = array();

        // The file is an image, we must resize it for a preview image.
        $cmd = "{$CONFIG['ffmpeg_path']}ffmpeg -i $path_to_image -an -ss 00:00:05 -r 1 -vframes 1 -s sqcif -y $path_to_preview";
        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 .= "<br /><br /><div align=\"left\">Cmd line : <br /><span style=\"font-size:120%\">" . nl2br(htmlspecialchars($cmd)) . "</span></div>";
            $ERROR .= "<br /><br /><div align=\"left\">The ffmpeg program said:<br /><span style=\"font-size:120%\">";
            $ERROR .= nl2br(htmlspecialchars($output));
            $ERROR .= "</span></div>";
        }
        @unlink($path_to_preview);
        //return false;
    }
    // Set mode of uploaded picture
$path_to_preview = str_replace("%d", "1", $path_to_preview);
    @chmod($path_to_preview, octdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled
    // We check that the image is valid
    $imginfo = getimagesize($path_to_preview);
    if ($imginfo == null) {
        $ERROR = $lang_errors['resize_failed'];
        @unlink($path_to_preview);
        //return false;
    } else {
        //return true;
    }

just before:

Code: [Select]
} else {

        // The file is not an image, so we will use the non-image thumbs
        // for preview images.

        // We create the path to the preview image.
        $path_to_preview = "images/thumb_{$extension}.jpg";

    }


The -s sqcif does the resizing. Unfortunately I can't work out how to resize proportionally using ffmpeg, so if you have a bunch of videos that are not in the standard aspect ratio, you might want to have ffmpeg create the screenshot at fullsize and then use an imagemagick resize command on it and do -resize 150x which will ensure the aspect ratio is not changed.
Logged

oscar_rocha

  • Coppermine newbie
  • Offline Offline
  • Gender: Male
  • Posts: 14
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #10 on: February 19, 2007, 01:25:51 PM »

You can also overlap a "play this movie" image (jpeg,gif or png) to the videothumb (like in youtube) just adding in picmgmt.inc.php:

After:
Code: [Select]
$return = str_replace("%d", "1", $dest_file);
Add:

Code: [Select]
   
/**************************************/
  $background = imagecreatefromjpeg($return);
  $insert = imagecreatefromgif($image_path);
  imagecolortransparent($insert,imagecolorat($insert,0,0));
  $insert_x = imagesx($insert);
  $insert_y = imagesy($insert);
  imagecopymerge($background,$insert,45,35,0,0,$insert_x,$insert_y,100);
  imagejpeg($background, $return, 100);
    /***************************************/

Where $image_path is the path to the image you want to overlap.
Logged

gldickens3

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 39
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #11 on: August 04, 2007, 12:49:00 AM »

This is a great thread!

I downloaded ffmpg and got everything listed in this thread working perfectly except for this:

Quote from: oscar_rocha
You can also overlap a "play this movie" image (jpeg,gif or png) to the videothumb (like in youtube) just adding in picmgmt.inc.php:

After:
Code:

$return = str_replace("%d", "1", $dest_file);


Add:

Code:

   
/**************************************/
  $background = imagecreatefromjpeg($return);
  $insert = imagecreatefromgif($image_path);
  imagecolortransparent($insert,imagecolorat($insert,0,0));
  $insert_x = imagesx($insert);
  $insert_y = imagesy($insert);
  imagecopymerge($background,$insert,45,35,0,0,$insert_x,$insert_y,100);
  imagejpeg($background, $return, 100);
    /***************************************/


Where $image_path is the path to the image you want to overlap.

When I put the code into picmgmt.inc.php then I get the following error message when I try to "Batch add files":

Fatal error: Call to undefined function imagecreatefromjpeg() in /home/gajets/public_html/gallery/include/picmgmt.inc.php on line 78

How do I fix this problem so I don't get this undefined function error?  Is there other code that I need to be including?

Thanks,

Gordon Dickens
Logged

Nibbler

  • Guest
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #12 on: August 04, 2007, 06:26:35 AM »

You need GD with JPEG support enabled. Check your phpinfo page.
Logged

gldickens3

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 39
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #13 on: August 04, 2007, 02:51:34 PM »

Hi Nibbler,

Duh....

You hit the nail right on the head!  I had GD on my system but it wasn't included in my Apache compile so I recompiled Apache including GD which fixed the problem.  Thanks!

Now, the thumbnails are being generated, however, the overlay "play button" is not being included. So, my question is about:

Quote from: oscar_rocha
Where $image_path is the path to the image you want to overlap.

Should the file name path be relative to the root directory of the website or relative to the root directory of the coppermine installation?  That is, I put my overlay image in my themes directory.  So, which of the following paths would be correct for the code line: $insert = imagecreatefromgif($image_path);?

$image_path = gallery/themes/gordon_grey/playbutton.gif
or;
$image_path = themes/gordon_grey/playbutton.gif

Thanks,

Gordon
Logged

Abbas Ali

  • Administrator
  • Coppermine addict
  • *****
  • Country: in
  • Offline Offline
  • Gender: Male
  • Posts: 2158
  • Spread the PHP Web
    • Ranium Systems
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #14 on: August 04, 2007, 05:26:24 PM »

$image_path = gallery/themes/gordon_grey/playbutton.gif
or;
$image_path = themes/gordon_grey/playbutton.gif

Play safe and put the absolute path i.e. something like /home/username/public_html/gallery/themes/.... (if you are on linux server).
Btw: relative path from gallery folder should work
Logged
Chief Geek at Ranium Systems

gldickens3

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 39
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #15 on: August 04, 2007, 11:28:33 PM »

Ali and Nibbler,

I've got it working now. I've just got to figure out how to properly center the overlay on my thumbnails.

Thanks for all your help.

Gordon
Logged

lifter

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 37
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #16 on: September 06, 2007, 11:21:04 PM »

When I try to implement this mod, video uploads seize to work.

When I upload the file it says:

Successful Uploads
1 uploads were successful.

Please click 'Continue' to add the files to albums.

But after continuing and selecting an album and pressing continue again it says

"The previous file could not be placed.

You have successfully placed all the files."
Logged

Abbas Ali

  • Administrator
  • Coppermine addict
  • *****
  • Country: in
  • Offline Offline
  • Gender: Male
  • Posts: 2158
  • Spread the PHP Web
    • Ranium Systems
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #17 on: September 07, 2007, 06:47:09 AM »

First check whether normal video uploading (without the mod) is working or not. If that is working then it means ffmpeg is not working on your server.

Also turn on the debug mode to see the errors (if any).
Logged
Chief Geek at Ranium Systems

lifter

  • Coppermine novice
  • *
  • Offline Offline
  • Posts: 37
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #18 on: September 07, 2007, 06:56:40 AM »

Video uploading works without the mod - it may be that ffmpegshow isnt installed - going to have to check with my host
Logged

Abbas Ali

  • Administrator
  • Coppermine addict
  • *****
  • Country: in
  • Offline Offline
  • Gender: Male
  • Posts: 2158
  • Spread the PHP Web
    • Ranium Systems
Re: Automatic video thumbnail creation (Requires ffmpeg)
« Reply #19 on: September 07, 2007, 07:30:32 AM »

it may be that ffmpegshow isnt installed - going to have to check with my host

Thats the first thing to check for as my first post describes.
Logged
Chief Geek at Ranium Systems
Pages: [1] 2 3   Go Up
 

Page created in 0.094 seconds with 20 queries.