Propably the simplest way to use ffmpeg with Imagick is to configure the ffmpeg as a delegate for ImageMagick. I tested this quickly yesterday and I was able to get images out of an Xvid video.

ImageMagick relies on a number of delegate programs to support certain image formats. This means that when an image with a specific format is opened ImageMagick spawns the delegate program to handle the conversion from the original format. I my case the program was ffmpeg.

First start by adding a new delegate into ImageMagick’s delegates.xml, inside the tag:

<delegate decode=”ffmpeg” command=’”ffmpeg” -i “%i” -y -vcodec png -ss %s -vframes 1 -an -f rawvideo “%o”‘ />

After this you should be able to use the following piece of code:

  1. <?php
  2.  
  3. $im = new imagick();
  4. $im->newPseudoImage( 200, 200, "ffmpeg:test1.mpg[40]" );
  5. $im->setImageFormat( "png" );
  6.  
  7. header( "Content-Type: image/png" );
  8. echo $im;
  9.  
  10. ?>

The number inside [] is seconds from the begin of the video. Someone more proficient with ffmpeg can propably change that to framenumber but I was unable to find a way to get image output by frame.