Preben has asked for the wisdom of the Perl Monks concerning the following question:

I tend to really try things out before I seek the wisdome of the perlmonks. But once again, my brain nor google will aid me into solving my problem.

Senario: Making a media library for my own use, to store all the video info like size, personal rating etc. in mysql. Presenting it through CGI. Peechy!

But how can I generate screenshot(s) from a video file? Is perl even capable of such a thing?

Any help, urls guidance in this would be greatly appreciated.

//Preben

Replies are listed 'Best First'.
Re: Generate screenshot from a video file?
by plobsing (Friar) on Dec 27, 2007 at 06:09 UTC
    Working audio/video is best left to C. This means that you probably want to look for some C video libraries with an XS interface.
    Luckily, there's FFmpeg (available at a CPAN near you).
    In fact, it even has an example of capturing a frame from a stream in the SYNOPSIS portion of the POD.

      I found FFmpeg too tedious to build and simply used the ffmpeg command line tool (as a prebuilt binary) from Perl - that's not hard either and saved me the hassle of collecting, configuring and compiling all the various libraries that ffmpeg needs to work. And luckily, ffmpeg can take the video stream from STDIN and can output the results to STDOUT.

Re: Generate screenshot from a video file?
by ohcamacj (Beadle) on Dec 27, 2007 at 13:17 UTC
    As always, TMTOWTDI.
    I would probably use mplayer, as well. Something like
    $mplayer_path = "/path/to/mplayer"; $video_file = "test_file.avi"; #create a temporary directory. my $temppath = "/tmp/screenshots." . int(rand(1e7)); if( ! mkdir($temppath)){ print("Error: $!\n"); exit(1); } if( ! chdir($temppath)){ print("Error: $!\n"); exit(1); } # Pick a mode: # Every single frame # system($mplayer_path, "-vo", "png:z=3", "-ao", "null", $video_file); # Every 20th frame system($mplayer_path, "-vo", "png:z=3", "-vf", "framestep=20", "-ao", +"null", $video_file); # Every keyframe # system($mplayer_path, "-vo", "png:z=3", "-vf", "framestep=I", "-ao", + "null", $video_file); #mplayer creates many files of the form "^\d{8}\.png$" in the current +directory. my $dirh; if( ! opendir($dirh, $temppath)) { print("Error: $!\n"); exit(1); } my @frames = readdir($dirh); closedir($dirh); foreach(@frames){ #do something useful, or delete the file. }
      Awesome! Thanks guys!!
Re: Generate screenshot from a video file?
by zentara (Cardinal) on Dec 27, 2007 at 12:34 UTC
    Mplayer does that, and you can run it thru system so you can do batch processing. See z-charcoal-video-converter for how to run mplayer from Perl, and check out Lives

    Also googling for something like "mplayer screenshot" should give good results.

    #> I am looking for Linux tools for extracting the individual frames o +f #> an AVI movie file, and saving them to files in bitmap formats such +as #> JPEG. Any suggestions? mplayer -vo jpeg z-men.mpg #(generates 1 JPEG per frame, plays all frames) # mplayer -vo jpeg -ss 1:23:45 -frames 20 file.avi #(-sseeks to 1 hour 23 minutes 45 seconds in file.avi, generates 20 JP +EGs #from the subsequent frames, quits) #"man mplayer" for more stuff, like skipping to the start of a DVD #chapter.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum