## Thursday, March 06, 2008

### How to Extract Video Still Frames with MPlayer

While working on a video content management system, I was in need of capturing frames from video files so they can be used as a preview for video. The system already had some code in place but it only worked for video encoding in Windows Media format (.wmv extension). That would not do, I thought, not in this day and age when we have so many file formats and video codecs. So I need to able to:

1. Capture video frames from within .NET code
2. Capture images from all kinds of different video formats
3. Not reinvent the wheel while satisfying #1 and #2

### Enter MPlayer

Stolen directly from the Information page of MPlayer, "MPlayer is a movie player which runs on many systems (see the documentation). It plays most MPEG/VOB, AVI, Ogg/OGM, VIVO, ASF/WMA/WMV, QT/MOV/MP4, RealMedia, Matroska, NUT, NuppelVideo, FLI, YUV4MPEG, FILM, RoQ, PVA files, supported by many native, XAnim, and Win32 DLL codecs. You can watch VideoCD, SVCD, DVD, 3ivx, DivX 3/4/5 and even WMV movies..". To add my own description, MPlayer is an open source command line video player.

### So What

Metallica has an old song called "So What", great tune for the metal heads in all of us. The answer is simple, MPlayer can do a lot more than play video. It can capture images, stream video over http and even transcode video from one format to another (but the last feature is grounds for another article).

You will need:

### Setup

1. Extract the mPlayer executable (mplayer.exe)
2. Extract the mPlayer codecs in a directory called "codecs" in the same directory as the mPlayer executable
3. Add a reference to the mPlayerWrapper project or compiled DLL

### Usage

• Capture with default arguments

in C#
// Specify the path to the video file
string videoFilePath = @"drive letter:\path\to\myVideo.mpg";
// Declare the mplayer instance with the mplayer executable residing in the
// same directory as your executable
mPlayerWrapper mPlayerInstance = new mPlayerWrapper();
// Capture frames
mPlayerInstance.captureFrames(videoFilePath);


in VB.NET
' Specify the path to the video file
dim videoFilePath As String = "drive letter:\path\to\myVideo.mpg"
' Declare the mplayer instance with the mplayer executable residing in the
' same directory as your executable
dim mPlayerInstance As new mPlayerWrapper()
' Capture frames
mPlayerInstance.captureFrames(videoFilePath)


This will capture 12 frames with 5 second interval between each frame and put them in the same directory as your video file. The filename of each frame will be "myVideo_thumb01.jpg" to "myVideo_thumb12.jpg". Each frame will be scaled to 270x200.
• Capture arguments
• mPlayerPath - sets the path where the mPlayer executable (mplayer.exe) is located. The default is in the same directory as your code is executing.
• currentFilePath - sets the file path to the video file you are using
• cleanOutputDirectory - deletes all the "jpg" images in the capture output directory before capturing
• captureInterval - sets the interval at which frames will be captured. Only applicable if using a time interval capture method (as outlined below)
• numberOfFramesToCapture - the number of frames to be captured
• captureExactNumberOfFrames - tells the wrapper to attempt to capture the exact number of frames as specified by the "numberOfFramesToCapture" property. This can be used if a file has too few frames but you still want to capture an exact number
• useTimeSeekToCapture - used to set the wrapper method of capture to seeking through the file instead of capturing a frame at an interval
• thumbnailPrefix - the prefix to be used when creating the filenames for captured frames. The default is the name of the video with "_thumb" append to it as in "myVideo_thumb01.jpg"
• capturedFrameWidthHeight - the width:height that each frame will be scaled to. The default is 270:200
• scaleCapturedFrames - used in conjunction with the "capturedFrameWidthHeight" property to scale down the captured frames. Set to true by default
• Capture in a different output directory
in C#
// Specify the path to the video file
string videoFilePath = @"drive letter:\path\to\myVideo.mpg";
// Specify the output directory
string outputPath = @"drive letter:\path\to\output directory";
// Declare the mplayer instance with the mplayer executable residing in the
// same directory as your executable
mPlayerWrapper mPlayerInstance = new mPlayerWrapper();
// Capture frames
mPlayerInstance.captureFrames(videoFilePath, outputPath);


in VB.NET
' Specify the path to the video file
dim videoFilePath As String = "drive letter:\path\to\myVideo.mpg"
' Specify the output directory
dim outputPath As String = @"drive letter:\path\to\output directory"
' Declare the mplayer instance with the mplayer executable residing in the
' same directory as your executable
dim mPlayerInstance As new mPlayerWrapper()
' Capture frames
mPlayerInstance.captureFrames(videoFilePath, outputDirectory)

• Using distinct capture methods:
1. captureFramesWithInterval - by default captures a frame every 5 seconds with up to 12 frames
2. captureFramesWithTimeSeek - by default captures 1 frame each second with up to 12 frames by seeking through the file
• Bonus
• getFileProperties - returns a SortedList of video and audio properties for the file
• getAudioProperties - returns a SortedList of audio properties for the file
• getVideoProperties - returns a SortedList of video properties for the file