Daniel Guerrero Thoughts

Monday, November 28, 2005

Amazing ffmpeg

So, there is a boom for video, you can see on youtube.com the next major on the media, flash streaming as "universal" format for web playing; I have to say that is a really, really good idea.

On the other side, there is a lot of 'funny videos' and 'jackass kind' of videos on a lot of sites, one of these sites I like too much is: ebaumsworld.com, but ok let's go the facts =).

To process info about a video, you need an api to access this; in this case there is a GPL library: ffmpeg (Mplayer use it). I have a mac, so I wanted to test on my mac instead on my linux box, so I download the ffmpeg sources (from cvs since the stable version didn't work) to compile I change it to 3.3 version:

phobos:~/Desktop/downloads/ffmpeg danguer$ sudo gcc_select 3.3
Password:
You are already using gcc version 3.3 as the default compiler.
phobos:~/Desktop/downloads/ffmpeg danguer$ ./configure --prefix=/sw --enable-shared


I use the /sw since I have fink working on my mac, then I compiled and installed: make && sudo make install

Then I downloaded the ffmpeg-php extension to use ffmpeg into my php scripts.
For me only worked if you hack a bit the configure script, follow the steps (phpize if you want as extension to load), and before you make ./configure change the configure file from:

echo "$as_me:$LINENO: checking for ffmpeg libavcodec.so" >&5
echo $ECHO_N "checking for ffmpeg libavcodec.so... $ECHO_C" >&6
for i in $PHP_FFMPEG /usr/local /usr ; do
if test -f $i/lib/libavcodec.so; then
FFMPEG_LIBDIR=$i/lib
fi
done

to:

echo "$as_me:$LINENO: checking for ffmpeg libavcodec.so" >&5
echo $ECHO_N "checking for ffmpeg libavcodec.so... $ECHO_C" >&6
for i in $PHP_FFMPEG /usr/local /usr ; do
if test -f $i/lib/libavcodec.dylib; then
FFMPEG_LIBDIR=$i/lib
fi
done


And of course, the script will only look into /usr/lib & /usr/include ; as my libraries were into /sw I put links to the lib & include dirs.

From there, the configure works fine, and also the compile; you need to create (or copy from /etc/php.ini-default to /usr/lib/php.ini and edit to add the proper extension.

Next, download a mpg & avi file, and put this code to test =):

<?php
print "<h1>Testing mpg</h1>";
$movie = new ffmpeg_movie("tmp.mpg", false);

print "Movie Duration: " . $movie->getDuration() . "<br/>\n";
print "Movie GetFrameCount: " . $movie->getFrameCount() . "<br/>\n";
print "Movie GetFrameWidth: " . $movie->getFrameWidth() . "<br/>\n";
print "Movie GetFrameHeight: " . $movie->getFrameHeight() . "<br/>\n";
$frame = $movie->getFrame(15);
$im = $frame->toGDImage();
imagejpeg($im, "im_mpg.jpg");
print '<img src="im_mpg.jpg"/><br/>';

print "<h1>Testing avi</h1>";
$movie = new ffmpeg_movie("tmp.avi", false);

print "Movie Duration: " . $movie->getDuration() . "<br/>\n";
print "Movie GetFrameCount: " . $movie->getFrameCount() . "<br/>\n";
print "Movie GetFrameWidth: " . $movie->getFrameWidth() . "<br/>\n";
print "Movie GetFrameHeight: " . $movie->getFrameHeight() . "<br/>\n";
$frame = $movie->getFrame(15);
$im = $frame->toGDImage();
imagejpeg($im, "im_avi.jpg");
print '<img src="im_avi.jpg"/><br/>';
?>


The first time I didn't get any; but from another files, I really could get the frames and info about the movie... I can't wait to test if wmv and mov files can be readed and created short clips =).

Regards,

0 Comments:

Post a Comment

<< Home