How to Get Video Thumbnail and Duration Using Mplayer and PHP

Mplayer is a free and open source media player that available for all major operating systems including Windows, Unix and Mac OS X. Basically Mplayer is a command line application but it also has GUI front end that differs to its supported operating system.

In PHP, we can execute Mplayer’s command line to get thumbnail and duration of a video file.

The basic command line to get thumbnail is:

[lorenz@localhost] mplayer -ss <position> videofile – frames <numofthumb> -nosound -vo jpeg<:outdir=path/to/outdir>.

where:

position, is seek position in seconds or in hh:mm:ss when the thumbnail is taken
videofile, is the video file or path to video file
numbofthumb,  is the number of thumbnail created
:outdir=path/to/outdir, is the output directory. If the outdir is not specified, the default current working directory will be the output directory, make sure the output directory is writetable.

Example

[lorenz@localhost] mplayer -ss 10 video.flv -frames 1 -nosound -vo jpeg:outdir=/home/lorenz/video

The command above will create one thumbnail from video.flv at 10th second position, the output directory is /home/lorenz/video. The resulted thumbnail filename will be 00000001.jpg and will increase in accordance with number of thumbnail created, -frames 10 will result ten thumbnails with filename starts from 00000001.jpg to 00000010.jpg.

To execute in PHP, use exec command

exec("mplayer -ss 10 video.flv -frames 1 -nosound -vo jpeg:outdir=/home/lorenz");

The basic command line to get duration is:

[lorenz@localhost] mplayer -vo null -ao null -frames 0 -identify video.flv

The command above will output a lot of video information, the video duration is on ID_LENGTH=duration line.
To get the duration in PHP, we have to extract ID_LENGTH line from the output string.
The PHP code :

exec('mplayer -vo null -ao null -frames 0 -identify video.flv', $output);

while (list($key, $value) = each($output)) {
      if ($length = strstr($v, "ID_LENGTH=")) break;
}

$data       = explode('=', $length);
$duration   = $data [1];
facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Comments

  1. says

    I am doing a live streaming project where I have found similar scenario. I got a script in which the code was written but due to some problem the code is not working. The code something like this:

    $cmd = $config['mplayer']. ' -quiet -nolirc -vo null -ao null -frames 0 -identify "' .$video_path. '"';

    exec($cmd, $output);

    Where, $config['mplayer'] = ‘/usr/bin/mplayer’; and $video_path is path to video. Exec command doest give any output.

Leave a Reply

Your email address will not be published. Required fields are marked *