Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 old.

Is using FFMPEG still the best way these days to pull metadata from a video file in a .NET web application? And if so, what's the best C# wrapper out there?

I've tried MediaToolkit, MediaFile.dll without any luck. I saw ffmpeg-csharpe but that looks like it hasn't been touched in a few years.

I haven't found any current data on this subject. Is the ability to pull metadata from a video built into the latest version of .NET now?

I'm basically looking for any direction at this point.

I should add that whatever I use could be invoked thousands of times per hour so it will need to be efficient.

like image 774
Maddhacker24 Avatar asked Sep 26 '14 02:09

Maddhacker24


People also ask

How dynamically upload and play video file in asp net MVC 5?

Right-click on project Add select New Folder, name it VideoFileUpload to upload all the audio files in that folder. Right-click on Models folder, select Add, then select Class. A window will appear. Choose Class, give it the name VideoFiles, then click on Add.

How do I get the length of a video in C#?

Use a Shell object (found in shell32. dll in the system32 directory) to get the video length from the file metadata. Instantiate a WindowsMediaPlayer object (found in wmp. dll if WMP is installed on your machine), load the file into it and then get the length from the corresponding object property.

What is metadata in C# with example?

October 28, 2021. Metadata refers to binary information saved in memory or a language runtime portable executable file. When you compile code from a portable executable file, data is added to another file section. The code is converted to MSIL (Microsoft Intermediate Language) before moving to another file partition.


2 Answers

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();
like image 137
user3473830 Avatar answered Sep 21 '22 17:09

user3473830


It may be little late... You can do this with minimal code using the NuGet package of MediaToolKit

For more info get going from here MediaToolKit

like image 42
kheya Avatar answered Sep 24 '22 17:09

kheya