What's the best way to get video metadata from a video file in ASP.Net MVC using C#?

后端 未结 4 1448
囚心锁ツ
囚心锁ツ 2020-12-05 05:36

I\'ve been searching on Google and StackOverflow for a good couple of hours. There seems to be a lot of similar questions on StackOverflow but they are all about 3-5 years o

4条回答
  •  伪装坚强ぢ
    2020-12-05 06:30

    Have a look at MediaInfo project (http://mediaarea.net/en/MediaInfo)

    it gets extensive information about most media types, and the library is bundled with a c# helper class which is easy to use.

    You can download the library and helper class for windows from here:

    http://mediaarea.net/en/MediaInfo/Download/Windows (DLL without installer)

    The helper class is located at Developers\Source\MediaInfoDLL\MediaInfoDLL.cs, simply add it to your project and copy the MediaInfo.dll to your bin.

    Usage

    you can obtain information by requesting specific parameter from the library, Here is a sample:

    [STAThread]
    static void Main(string[] Args)
    {
        var mi = new MediaInfo();
        mi.Open(@"video path here");
    
        var videoInfo = new VideoInfo(mi);
        var audioInfo = new AudioInfo(mi);
         mi.Close();
    }
    
    public class VideoInfo 
    {
        public string Codec { get; private set; }
        public int Width { get; private set; }
        public int Heigth { get; private set; }
        public double FrameRate { get; private set; }
        public string FrameRateMode { get; private set; }
        public string ScanType { get; private set; }
        public TimeSpan Duration { get; private set; }
        public int Bitrate { get; private set; }
        public string AspectRatioMode { get; private set; }
        public double AspectRatio { get; private set; }
    
        public VideoInfo(MediaInfo mi)
        {
            Codec=mi.Get(StreamKind.Video, 0, "Format");
            Width = int.Parse(mi.Get(StreamKind.Video, 0, "Width"));
            Heigth = int.Parse(mi.Get(StreamKind.Video, 0, "Height"));
            Duration = TimeSpan.FromMilliseconds(int.Parse(mi.Get(StreamKind.Video, 0, "Duration")));
            Bitrate = int.Parse(mi.Get(StreamKind.Video, 0, "BitRate"));
            AspectRatioMode = mi.Get(StreamKind.Video, 0, "AspectRatio/String"); //as formatted string
            AspectRatio =double.Parse(mi.Get(StreamKind.Video, 0, "AspectRatio"));
            FrameRate = double.Parse(mi.Get(StreamKind.Video, 0, "FrameRate"));
            FrameRateMode = mi.Get(StreamKind.Video, 0, "FrameRate_Mode");
            ScanType = mi.Get(StreamKind.Video, 0, "ScanType");
        }
    }
    
    public class AudioInfo
    {
        public string Codec { get; private set; }
        public string CompressionMode { get; private set; }
        public string ChannelPositions { get; private set; }
        public TimeSpan Duration { get; private set; }
        public int Bitrate { get; private set; }
        public string BitrateMode { get; private set; }
        public int SamplingRate { get; private set; }
    
        public AudioInfo(MediaInfo mi)
        {
            Codec = mi.Get(StreamKind.Audio, 0, "Format");
            Duration = TimeSpan.FromMilliseconds(int.Parse(mi.Get(StreamKind.Audio, 0, "Duration")));
            Bitrate = int.Parse(mi.Get(StreamKind.Audio, 0, "BitRate"));
            BitrateMode = mi.Get(StreamKind.Audio, 0, "BitRate_Mode");
            CompressionMode = mi.Get(StreamKind.Audio, 0, "Compression_Mode");
            ChannelPositions = mi.Get(StreamKind.Audio, 0, "ChannelPositions");
            SamplingRate = int.Parse(mi.Get(StreamKind.Audio, 0, "SamplingRate"));
        }
    }
    

    You can easily obtain all information in string format by callingInform():

            var mi = new MediaInfo();
            mi.Open(@"video path here");
            Console.WriteLine(mi.Inform());
            mi.Close();
    

    if you need more information about available parameters, you can simply query all of them by calling Options("Info_Parameters"):

            var mi = new MediaInfo();
            Console.WriteLine(mi.Option("Info_Parameters"));
            mi.Close();
    

提交回复
热议问题