I searched alot for a way to get details of a video file, without the need of a third party component. Im developing a video library on my free time, and I wanted the user to get this data automatically after selecting the video. The class uses shell32.dll which is what Windows Explorer uses to get the video details, and is available in every Windows installation. I tested my app on Windows XP and Vista. So to use this dll just open the Add Reference dialog box, select the COM tab, select Microsoft Shell Controls And Automation and click OK.
using System.Windows.Forms; using System; using System.IO; ////// This class gets the movie file's details using the windows shell /// public class MovieFileDetails { private Shell32.Shell Sh = new Shell32.Shell(); private Shell32.Folder F; private Shell32.FolderItem FI; ////// The runtime (play time) of the movie /// public readonly string Runtime; ////// The screen resolution of movie (Format: Width X Height) /// public readonly string Resolution; ////// The file size in MB of the movie /// public readonly string FileSize; ////// The audio bitrate of the movie (Example: 127kbps) /// public readonly string Audio; ////// Frame rates per second and total bitrate /// public readonly string Video; public MovieFileDetails(string MovieFile) { //Got this using Windows Vista, didnt try it in XP or earlier releases //1 file size = 702 MB //27 length = 01:33:45 //28 Audio Bitrate= 127kbps //262 data rate= 3kbps //263 frame height = 416 //265 frame rate = 23 frames/second //265 frame width = 560 //266 total bitrate = 130kbps if (System.IO.File.Exists(MovieFile)) { try { F = Sh.NameSpace(System.IO.Path.GetDirectoryName(MovieFile)); FI = F.ParseName(System.IO.Path.GetFileName(MovieFile)); //set the fields Runtime = F.GetDetailsOf(FI, 27); Resolution = F.GetDetailsOf(FI, 265) + " X " + F.GetDetailsOf(FI, 263); //remove formatting, i.e. '701' instead of '701 MB' FileSize = F.GetDetailsOf(FI, 1).Substring(0,4).Trim(); Audio = F.GetDetailsOf(FI, 28); Video = F.GetDetailsOf(FI, 265) + ", Total BitRate: " + F.GetDetailsOf(FI,266); } catch (Exception ex) { const string MSG = "Could not get movie file information. "; MSG += "Please make sure the right codecs are installed"; MessageBox.Show(MSG, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("The file does not exist.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
An example that uses the class:
private void button1_Click(object sender, EventArgs e)
{
MovieFileDetails movie = new MovieFileDetails("C:\\Video1.wmv");
//e.g. 54kbps
label1.Text = "Audio: " + movie.Audio + "\n";
//e.g. 6.11
label1.Text += "File Size: " + movie.FileSize + "\n";
//e.g. 240 X 320
label1.Text += "Video Resolution: " + movie.Resolution + "\n";
//e.g. 00:05:30
label1.Text += "Runtime: " + movie.Runtime + "\n";
//e.g. 25 frames/second, Total BitRate: 350kbps
label1.Text += "Video: " + movie.Video + "\n";
}
Related posts:





Hi Amgad,
This code works fine if we have .wmv and .avi video file. How about .mpg file formats which does not have these values as file properties. How can we extract similar properties of video files with different formats (say .mpg, .mp4, .swf etc).
Regards,
Satya
Hello Satya,
Actually this depends on the video codecs installed, and how well they are programmed. For example if DivX is not installed and you try to access an .avi file that is DivX encoded the properties will not show up nor will the thumbnail preview. When I wrote this post I had a collection of codecs installed(I think it was called K-Lite or something close to it) which enabled me to see thumbnail previews and video properties from Windows Explorer of .mpg and .mp4 files, and this code worked fine with these files. But I think to get .swf details you will need a library from Adobe or a 3rd party.
Dude, thanks, I really needed that, although framerare is 265 and not 264!
Thank you Rafal for pointing that out. I will fixed it now.