Question

I am creating a program to access information on different types of files, I have so far been successful mostly with MP3 files and am now working on the video MPG etc types.

So far I have been able to obtain Video Title, Year, Duration, Genre, video height and width with little effort and now I am attempting to access the slightly more difficult aspects from the VideoHeader section of TagLib.

This is the code I have managed to obtain from finding something about AudioHeaders on here but it didnt work:

TagLib.File f = TagLib.Mpeg.File.Create(GetMPG.FileName);

foreach(ICodec codec in f.Properties.Codecs){
  TagLib.Mpeg.VideoHeader G = (TagLib.Mpeg.VideoHeader) codec;
  MPGbps.Text = G.VideoFrameRate.ToString();
}

Where am I going wrong?

My new current code:

TagLib.File f = TagLib.File.Create(GetMPG.FileName);
foreach(ICodec codec in f.Properties.Codecs){
  TagLib.Mpeg.VideoHeader G = (TagLib.Mpeg.VideoHeader) codec;
  if (G != null)
    {
      MPGbps.Text = G.VideoFrameRate.ToString();
    }
}

This has ended in the error:

Error 2 Operator '!=' cannot be applied to operands of type 'TagLib.Mpeg.VideoHeader' and '< null >'

note: added spaces in final null as it didn't show up in the post otherwise

Was it helpful?

Solution

Video files have multiple codecs, an audio and a video one. What's happening with your code is that in one iteration of the loop, codec is not a VideoHeader meaning G is not set correctly. I'm not sure if this triggers an exception or if G is set to an empty VideoHeader.

The below code should work:

TagLib.File f = TagLib.File.Create(GetMPG.FileName);

foreach(ICodec codec in f.Properties.Codecs){
  if(codec is TagLib.Mpeg.VideoHeader) {
    TagLib.Mpeg.VideoHeader G = (TagLib.Mpeg.VideoHeader) codec;
    MPGbps.Text = G.VideoFrameRate.ToString();
  }
}

Also, you should be using TagLib.File.Create instead. It is a static factory method.

Update

Beyond the above initial issue where codec was being cast as the wrong type, there was the issue that the file was not a MPEG and did not actually contain a TagLib.Mpeg.VideoHeader and had a TagLib.Riff.BitmapInfoHeader instead. Beyond the basic properties guaranteed by TagLib.IVideoCodec, the details provided by individual file formats varies widely on a case by case basis. It is important to know what file types are in scope, what features you want to detect, and extract video details as available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top