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
<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:
- <?php
- $im = new imagick();
- $im->newPseudoImage( 200, 200, "ffmpeg:test1.mpg[40]" );
- $im->setImageFormat( "png" );
- header( "Content-Type: image/png" );
- echo $im;
- ?>
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.
#1 by Lars van den Noort on October 16, 2007 - 6:58 pm
Quote
Hi Mikko,
this isn’t working for me just like the PDF version of this example. FFMPEG is available from any location on my PC. What about the location of the mpg, where should it be? I tried including a path to the mpg but it fails with ‘Fatal error: Uncaught exception ‘ImagickException’ with message ‘UnableToOpenBlob `H:/Temp/magick-mgGe_XSB’: No such file or directory’. I do see a file being added to the Temp directory but is doesn’t have the same name as in the error message.
#2 by Mikko Koppanen on October 16, 2007 - 9:29 pm
Quote
Does it work from ImageMagick commandline tools like convert?
#3 by Lars van den Noort on October 17, 2007 - 2:21 am
Quote
It turns out that I had several FFMPEG.EXEs on my machine and of course the first one in the path was the oldest one. I updated it and it is now working. I find it to be much slower compared to entering the same commands via the command line. It is especially noticeable on higher resolution material like 1080p, the command line takes several seconds to output 1 frame, php/imagick takes 10 seconds more. About outputting a certain frame: FFMPEG is capable of selecting a certain frame; you need to multiply the frame number by 40(milliseconds) when the source file is 25fps. So if you want to have the frame at 10 seconds and 5 frames you should type 10.200 at the -ss value. Imagick doesn’t seem to pass on this float between the brackets to FFMPEG correctly.
#4 by RF on January 22, 2008 - 2:46 pm
Quote
The correct way to extract a specified frame is the following:
add “-r 1″. With this you force the framerate to be 1 FPS.
The -ss offset is then the frame number.
#5 by Mike on July 10, 2010 - 1:47 pm
Quote
Hi, i can’t seem to make it work.. it always says this:
‘no decode delegate for this image format’ when i try to suck image from avi or mpeg video file..
Im using latest imagick & imagemagick with php5
#6 by gregg on May 19, 2011 - 10:23 pm
Quote
hey wussup im loven this page but im haveing problrms puting this part in:”header( “Content-Type: image/png” );
echo $im;”
What am i doing wrong?
#7 by Mikko Koppanen on August 1, 2011 - 10:35 pm
Quote
Hi,
remove the header and the echo at the end and you should see if exception gets thrown.
#8 by Everson on September 13, 2011 - 7:26 pm
Quote
Hi,
How can I use delegates in Java?
I need to convert jpg to pcl.
Thanks
#9 by Daniel on October 25, 2011 - 5:22 pm
Quote
Hey, great site!
I’m trying to get to run the whole way around. I want to DPX files (10bit YCbCr Rec709), which I convert into an avi with ffmpeg. For this I have added the following line in the file delegate.xml:
Command line:
convert D:\EBU_IRT_Sequenzen\Source_720p50_.bmp “avi:D:\EBU_IRT_Sequenzen\Source_720p50.avi”
Unfortunately, now I get the following error message:
Magick: no encode delegate for this image format `avi:D:\EBU_IRT_Sequenzen\Source_720p50.avi’ @error/constitute.c/WriteImage/1193.
Have you any idea what’s wrong?
I work with ImageMagick 6.7.3-Q16
Many thanks in advance
Daniel
#10 by Dale Kashmir on October 26, 2011 - 8:41 am
Quote
Say thanks a lot for your efforts to have decided to put these things together on this blog. Say thanks a lot for your efforts to have decided to put these things together on this blog. Keep it up. Thank you.