You can use FFmpeg tool from within your C# application to obtain metadata information from a video file including the width and height information. Not knowing your application workflow, I cannot suggest when exactly you should read the metadata information. But one possible scenario is this.
- Users select a bunch of video files from disk to tell you application to load them in to a playlist for playback.
- While showing a loading animation, you run each video through the FFmpeg tool to get metadata information which you either store in memory or in a database along with the file name.
- When you have completed step 2, the user is able to see the videos in the playlist.
- When user selects a video to play, you load the corresponding width and height information and set the properties of the media player control in your application.
- When user moves to a different video you repeat step 4.
Now there are many C# libraries for FFmpeg. Some paid and some free. But I've been using some code that I obtained from a co-worker a while back. It does more than just get the metadata. It also performs conversion of the video to FLV format. I'm uploading that to github here. The actual method that extracts the metadata will look like this.
public void GetVideoInfo(VideoFile input)
{
//set up the parameters for video info
string Params = string.Format("-i {0}", input.Path);
string output = RunProcess(Params);
input.RawInfo = output;
//get duration
Regex re = new Regex("[D|d]uration:.((\\d|:|\\.)*)");
Match m = re.Match(input.RawInfo);
if (m.Success)
{
string duration = m.Groups[1].Value;
string[] timepieces = duration.Split(new char[] { ':', '.' });
if (timepieces.Length == 4)
{
input.Duration = new TimeSpan(0, Convert.ToInt16(timepieces[0]), Convert.ToInt16(timepieces[1]), Convert.ToInt16(timepieces[2]), Convert.ToInt16(timepieces[3]));
}
}
//get audio bit rate
re = new Regex("[B|b]itrate:.((\\d|:)*)");
m = re.Match(input.RawInfo);
double kb = 0.0;
if (m.Success)
{
Double.TryParse(m.Groups[1].Value, out kb);
}
input.BitRate = kb;
//get the audio format
re = new Regex("[A|a]udio:.*");
m = re.Match(input.RawInfo);
if (m.Success)
{
input.AudioFormat = m.Value;
}
//get the video format
re = new Regex("[V|v]ideo:.*");
m = re.Match(input.RawInfo);
if (m.Success)
{
input.VideoFormat = m.Value;
}
//get the video format
re = new Regex("(\\d{2,3})x(\\d{2,3})");
m = re.Match(input.RawInfo);
if (m.Success)
{
int width = 0; int height = 0;
int.TryParse(m.Groups[1].Value, out width);
int.TryParse(m.Groups[2].Value, out height);
input.Width = width;
input.Height = height;
}
input.infoGathered = true;
}
The code could probably use some optimization including the implementation of IDisposable and the dispose pattern. However it should be a good starting point for you. You also need to download FFmpeg executable from here and update your App.config file with the path to the FFmpeg executable.