---
author: einar
categories:
- General
- Linux
comments: true
date: "2007-10-16T20:44:46Z"
slug: script-to-make-captures-of-movie-files
tags:
- bash
- Linux
- python
- script
title: Script to make captures of movie files
omit_header_text: true
disable_share: true
wordpress_id: 312
---

The other day I was thinking about how to make screencaps for the anime I watch. Windows users often use [Media Player Classic](http://sourceforge.net/projects/guliverkli/), which can create a video contact sheet (i.e., a series of captures) out of a movie file. I had two problems with this:



	
  * The biggest is that it runs on Windows, and I don't use Windows;

	
  * The frames needed to be manually cropped every time, which was slow.


Therefore, inspired by [a video contact sheet script for Linux](http://freshmeat.net/projects/video-contact-sheet/), I decided to write a small piece of code to make captures. It works rather easy, by taking snapshots every X minutes, where X is an integer number.

The code is here: it requires bash, python (just for checks) and [mplayer](http://mplayerhq.hu) to work correctly. It should work with every format mplayer suppports. It's hackish, but if you find it useful, let me know.

**EDIT**: I changed a line (thanks, greg) because this syntax highlighter messes up some formatting.

[code lang='c']

#!/bin/bash
# (C) 2007 Luca Beltrame - licensed under the terms of the GPL v2

# Simple script to output video frames with MPlayer. It takes the file
# and a step argument to indicate how  many minutes a capture should
# be taken. The step must be an integer!

if [ $# -ne "2" ]
then
echo "Usage: $0  "
exit
fi

file=$1
step=$2
i=1

# Requires python 2.5
verification=`python -c "value = 0 if isinstance($step,float) else 1;print value"`
echo $verification

if [ $verification -eq "0" ]
then
echo "Step must be an integer!"
fi

length=`mplayer -benchmark -ao null -vo null -identify -frames 0 -quiet $file 2>/dev/null | grep ID_LENGTH | cut -f2 -d'='`

end=`echo $length/60| bc`

while [ $i -lt $end ]
do
minutes="00:"$i":00"
name="capture_"$i"min.png"
# We take two captures as the first will be always black - mplayer bug?
mplayer -sws 9 -ao null -quiet -benchmark -vo "png:z=0" -frames 2 -ss $minutes "$1" &> /dev/null
mv 00000002.png $name
rm -f 00000001.png
i=$[$i+$step]
done

echo "Screenshots saved."

[/code]
