How to Extract Video Still Frames with MPlayer
Join the DZone community and get the full member experience.
Join For FreeWhile 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:
- Capture video frames from within .NET code
- Capture images from all kinds of different video formats
- 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).
Putting It Together
You will need:
- 1 download of MPlayer Windows executable from http://www.mplayerhq.hu/design7/dload.html
- 1 download of the MPlayer Windows binary codecs package from http://www.mplayerhq.hu/design7/dload.html
- 1 MPlayer .NET wrapper provided in this article
Setup
- Extract the mPlayer executable (mplayer.exe)
- Extract the mPlayer codecs in a directory called "codecs" in the same directory as the mPlayer executable
- Add a reference to the mPlayerWrapper project or compiled DLL
Usage
- Capture with default arguments
in C#// Specify the path to the video filestring videoFilePath = @"drive letter:\path\to\myVideo.mpg";// Declare the mplayer instance with the mplayer executable residing in the// same directory as your executablemPlayerWrapper mPlayerInstance = new mPlayerWrapper();// Capture framesmPlayerInstance.captureFrames(videoFilePath);
in VB.NET' Specify the path to the video filedim videoFilePath As String = "drive letter:\path\to\myVideo.mpg"' Declare the mplayer instance with the mplayer executable residing in the' same directory as your executabledim mPlayerInstance As new mPlayerWrapper()' Capture framesmPlayerInstance.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 filestring videoFilePath = @"drive letter:\path\to\myVideo.mpg";// Specify the output directorystring outputPath = @"drive letter:\path\to\output directory";// Declare the mplayer instance with the mplayer executable residing in the// same directory as your executablemPlayerWrapper mPlayerInstance = new mPlayerWrapper();// Capture framesmPlayerInstance.captureFrames(videoFilePath, outputPath);
in VB.NET' Specify the path to the video filedim videoFilePath As String = "drive letter:\path\to\myVideo.mpg"' Specify the output directorydim outputPath As String = @"drive letter:\path\to\output directory"' Declare the mplayer instance with the mplayer executable residing in the' same directory as your executabledim mPlayerInstance As new mPlayerWrapper()' Capture framesmPlayerInstance.captureFrames(videoFilePath, outputDirectory)
- Using distinct capture methods:
- captureFramesWithInterval - by default captures a frame every 5 seconds with up to 12 frames
- 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
Downloads
mPlayerWrapper: http://blog.tech-cats.net/examples/dotnet/mPlayerWrapper.dll
mPlayerWrapper Source: http://blog.tech-cats.net/examples/dotnet/mplayerWrapper-v0.2.zip
Published at DZone with permission of Boyan Kostadinov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments