3

I'm reading from a folder the .mp4 files there Currently I'm using FileInfo to extract the name FileInfo is limited to a few of the details a movie contains. I also need to extract other information like Title Subtitle Comments Genre Directors Producers

DirectoryInfo dirInfo = new DirectoryInfo(@"..\bin\Debug\Folder");
FileInfo[] fileNames = dirInfo.GetFiles("*.mp4");
foreach (FileInfo fi in fileNames)
{
    string movieName = fi.Name.Split('.')[0]; // returns the file name
    VideoFile newVideo = new VideoFile(movieName); // insert name in object
            director.ListVid.Add(newVideo); // add object to a director object - aka another list
}
 listVideoDirector.Add(director); //add director object to list

My videoFile object has more attributes. I need to extract them from the actual file

Alexandru Nutu
  • 431
  • 2
  • 5
  • 11
  • See posting : http://stackoverflow.com/questions/58649/how-to-get-the-exif-data-from-a-file-using-c-sharp – jdweng May 10 '16 at 09:54
  • Hmm... I should narrow the question. There is a shell extension. It contains things like System.Media.SubTitle. But I cannot find them – Alexandru Nutu May 10 '16 at 10:04
  • An image may not contain all the exif data. It depends on the application (and person) which extensions are written to the image. For example a camera would put in the number of pixels automatically and an automatic number title (which a person can manually change to describe the picture) but not always a subtitle. – jdweng May 10 '16 at 10:14

2 Answers2

10

I've used the library FFProbe

Download Library : https://www.nrecosite.com/downloads/video_info_free.zip

for example :

string path = "Video path";
NReco.VideoInfo.FFProbe ffProbe = new NReco.VideoInfo.FFProbe();
MediaInfo videoInfo = ffProbe.GetMediaInfo(path ); 

TimeSpan videoDuration = videoInfo.Duration;

if (videoInfo.Streams[index].CodecType.ToLower() == "video")
{
 int iWidth = videoInfo.Streams[index].Width;
 int iHeight = videoInfo.Streams[index].Height;
 string sVideoFrameRate = videoInfo.Streams[index].FrameRate.ToString();
 string sVideoCodecName = videoInfo.Streams[index].VideoCodecName;
  //...
}
else if(videoInfo.Streams[index].CodecType.ToLower() == "audio")
{
   string sAudioCodecName = videoInfo.Streams[index].CodecName;
  //...
}
Masoud Siahkali
  • 5,100
  • 1
  • 29
  • 18
  • 1
    hey it's get error like ffprobe.exe not found? how to solve it. – Brijesh Mavani Aug 02 '18 at 06:43
  • sounds like you need to download ffprobe which this probably calls. edit: I just typed "ffprobe" at the command prompt and got output which means it must be part of ffmpeg's standard package. Try this: https://www.ffmpeg.org/ – Brent Rittenhouse Feb 05 '19 at 09:29
2

You may use ffmpeg (ffmpeg.exe or ffprobe.exe) for extracting metadata from video or audio files (it supports almost all known formats). FFMpeg can be executed from C# code with System.Diagnostics.Process and video file metadata should be parsed from the console output (you can redirect stdout and read it as string).

As alternative to writing custom code that executes ffprobe you can use one of the existing .NET wrappers that will return a result with one line of code (like NReco VideoInfo -- I'm an author of this library).

Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
  • I tried it, but failed :) I have the NReco in my references. – Alexandru Nutu May 10 '16 at 10:10
  • var ffProbe = new FFProbe(); var videoInfo = ffProbe.GetMediaInfo(fi.FullName); string info1 = null; string info2 = null; foreach (var tag in videoInfo.FormatTags) { info1 = tag.Key; info2 = tag.Value; } //string info4 = info1.Split(' ')[0]; // genre string genre = info2.Split(' ')[0]; – Alexandru Nutu May 10 '16 at 10:10
  • Uhm, that does not lool good. The thing is the way I try to get the info is wrong I think. If I say split[0] it returns only the genre, if I say split[1]. I get a null reference exception – Alexandru Nutu May 10 '16 at 10:11
  • Some information may be missed in the concrete video file - you need to check tags availability at the runtime. Also you can inspect MediaInfo.Result property (XPathDocument) to get all information returned by ffprobe. – Vitaliy Fedorchenko May 10 '16 at 11:36