📜 ⬆️ ⬇️

Getting screenshots and information from the video

In this post, I tell you how to make a screenshot of the video, as well as how to get information about the video file.

Of course, many already know how to take a screenshot from the video. But for someone, as they say, hands do not reach. It was for such people that I wrote this short note. I hope it will be useful.


')
So, proceed:

Many have probably heard about ffmpeg - a set of libraries for working with video. But, it turns out, such a trivial thing, as pulling a screenshot from a video file, this thing does very slowly! The further from the beginning of the movie you need to take a screenshot, the more time you have to wait, as ffmpeg first “scrolls” the video to the desired mark. You can get a screenshot of ffmpeg in the following way:

ffmpeg -i /home/username/movie.avi -an -ss 00:20:14 -r 1 -vframes 24 -s 320×240 -y -f mjpeg /home/username/screenshot-pct.jpg


But it is slow, so I do not recommend it. Naturally, such a slow work does not suit anyone (why in general did the developers make this feature in ffmpeg?).
Fortunately, there is an alternative - this is Mplayer . It happens both with a graphic shell, and without (depending on how to assemble it) and the same comrades who make ffmpeg make it. We do not need a graphical shell nafig, we're not going to watch movies! So boldly assemble Mplayer without GUI.
We use:

mplayer /home/username/movie.avi -ss 00:20:14 -frames 24 -vo jpeg:outdir=/home/username


This command means: “Take me 24 screenshots from the 20th minute and 14th second of the movie /home/username/movie.avi and put them in the directory / home / username”.

Why exactly 24 screenshots. The fact is that usually a movie contains from ~ 24 to ~ 31 frames, so I take 24 frames. More, I think, hardly needed. It is clear that within a second the scene can change dramatically (especially when a helicopter explodes =)), which is why we make 24 frames and then we can choose the most beautiful frame from these frames.
Now about how to get information from the video.
Personally, I use the same MPlayer to get the meta-information about the video file:

mplayer -identify /home/username/movie.avi -ao null -vo null -frames 0 2>/dev/null | grep ^ID_


Or, you can (php, without grep):

<?
/**
* - -.
* mplayer.
*/
class VideoInfoComponent {
public $mplayer = '/usr/local/bin/mplayer';
/**
* - -
*
* @param string $filename
* @return array -
*/
public function info($filename) {
$result = array();
$params = array();
exec("{$this->mplayer} -vo null -ao null -frames 0 -identify '{$filename}'", $result);
foreach ($result as $i=>$value) {
if (!preg_match('/^ID_/', $value)) unset($result[$i]);
else {
list ($param, $data) = explode('=', $value);
$params[ $param ] = strtolower($data);
}
}
$params['ID_SIZE'] = filesize($filename);
return $params;
}
}
?>

Source: https://habr.com/ru/post/22481/


All Articles