You are not logged in.

#1 2010-12-15 02:58:39

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Using PHP to Create a Poster Frame from a Video

I have a PHP script which I am using to grab a poster frame from an arbitrary video ($video). Currently it breaks the video into all its frames, counts the files, and grabs the middle one...

system("mplayer -vo png $video");
system("mv " . sprintf("%08d.png", floor(system("ls -1 | wc -l") / 2)) . " /home/.");

The issue with the code above is that, until completion, it fills up a large portion of the hard drive on the server. I know I can figure out the number of the middle frame with the code below, but I do not know of a way to pull out the middle frame without pulling out all the other frames too...

$fps = system("x=$(mplayer -msglevel all=0 -identify -vo null -frames 0 $video 2>&1 | grep ID_VIDEO_FPS) ; echo ${x#*=}");
$length = system("x=$(mplayer -msglevel all=0 -identify -vo null -frames 0 $video 2>&1 | grep ID_LENGTH) ; echo ${x#*=}");
$midframe = floor($fps * $length / 2);

Does anyone have any ideas? At the very least, is there a way I can read each frame but only save the middle one?

Offline

#2 2010-12-15 03:47:02

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Using PHP to Create a Poster Frame from a Video

mplayer -nosound -vo png -ss 20 -frames 1 $video

seeks to 20 seconds and saves one frame.

Edit: http://www.londatiga.net/it/programming … r-and-php/

Last edited by karol (2010-12-15 03:51:35)

Offline

#3 2010-12-15 06:02:50

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Using PHP to Create a Poster Frame from a Video

Yeah; I may end up going with a solution like that. I had hoped to be able to seek to a specific frame. Really I'd like the script to be very robust - for example, if I pass it a 30-fps video that is one second long, I'd like to get the #15 frame...

Offline

#4 2010-12-15 06:14:32

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Using PHP to Create a Poster Frame from a Video

tony5429 wrote:

Yeah; I may end up going with a solution like that. I had hoped to be able to seek to a specific frame. Really I'd like the script to be very robust - for example, if I pass it a 30-fps video that is one second long, I'd like to get the #15 frame...

Google told me mplayer can't seek to a specific frame, but does 1/100th of a second seeks. I'm not sure how precise the duration estimates are.

Offline

#5 2010-12-18 13:55:46

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Using PHP to Create a Poster Frame from a Video

Thanks, Karol - I ended up implementing your idea. For anyone else who is interested, here is the PHP I'm using,

$halfwaytime = substr(system("mplayer -msglevel all=0 -identify -vo null -frames 0 $video 2>&1 | grep ID_LENGTH"), 10) / 2;
system("mplayer -nosound -vo png -ss $halfwaytime -frames 1 $video");
system("mv 00000001.png /home/poster_frame.png");

Offline

Board footer

Powered by FluxBB