2014-09-26 9 views
10

Szukam od Google i StackOverflow przez dobre kilka godzin. Na StackOverflow pojawia się wiele podobnych pytań, ale wszystkie mają około 3-5 lat.Jaki jest najlepszy sposób na uzyskanie metadanych wideo z pliku MP4 w ASP.Net MVC przy użyciu C#?

Czy korzystanie z FFMPEG jest najlepszym sposobem, aby pobrać metadane z pliku wideo w aplikacji internetowej .NET? A jeśli tak, to jakie jest najlepsze opakowanie C#?

Próbowałem już MediaToolkit, MediaFile.dll bez powodzenia. Widziałem ffmpeg-csharpe, ale wygląda na to, że nie została dotknięta w ciągu kilku lat.

Nie znalazłem żadnych aktualnych danych na ten temat. Czy teraz można pobierać metadane z wideo wbudowanego w najnowszą wersję .NET?

Po prostu szukam jakiegokolwiek kierunku w tym momencie.

Powinienem dodać, że cokolwiek używam, może być wywoływane tysiące razy na godzinę, więc będzie musiało być wydajne.

Odpowiedz

15

Wystarczy popatrzeć na MediaInfo projektu (http://mediaarea.net/en/MediaInfo)

robi obszerne informacje na temat większości typów mediów, a biblioteka jest w zestawie z C# klasy pomocnika, który jest łatwy w użyciu.

Można pobrać Class Library i pomocnika do okien stąd:

http://mediaarea.net/en/MediaInfo/Download/Windows(DLL bez instalatora)

Klasa pomocnik znajduje się w Developers\Source\MediaInfoDLL\MediaInfoDLL.cs, wystarczy dodać go do swojego projektu i skopiuj MediaInfo.dll do swojego kosza.

Wykorzystanie

można uzyskać informacje poprzez żądanie konkretnego parametru z biblioteki, Oto przykład:

[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")); 
    } 
} 

można łatwo uzyskać wszystkie informacje w formacie ciągu wywołując Inform():

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

jeśli potrzebujesz więcej informacji o dostępnych parametrach, możesz to zrobić y kwerendy wszystkie z nich nazywając Options("Info_Parameters"):

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

Dziękuję. Z twoją pomocą udało mi się uzyskać pracę MediaInfo w moim rozwiązaniu! – Maddhacker24

2

Proponuję użyć ffmpeg z Process.Start, kod wygląda następująco:

private string GetVideoDuration(string ffmpegfile, string sourceFile) { 
     using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process()) { 
      String duration; // soon will hold our video's duration in the form "HH:MM:SS.UU" 
      String result; // temp variable holding a string representation of our video's duration 
      StreamReader errorreader; // StringWriter to hold output from ffmpeg 

      // we want to execute the process without opening a shell 
      ffmpeg.StartInfo.UseShellExecute = false; 
      //ffmpeg.StartInfo.ErrorDialog = false; 
      ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
      // redirect StandardError so we can parse it 
      // for some reason the output comes through over StandardError 
      ffmpeg.StartInfo.RedirectStandardError = true; 

      // set the file name of our process, including the full path 
      // (as well as quotes, as if you were calling it from the command-line) 
      ffmpeg.StartInfo.FileName = ffmpegfile; 

      // set the command-line arguments of our process, including full paths of any files 
      // (as well as quotes, as if you were passing these arguments on the command-line) 
      ffmpeg.StartInfo.Arguments = "-i " + sourceFile; 

      // start the process 
      ffmpeg.Start(); 

      // now that the process is started, we can redirect output to the StreamReader we defined 
      errorreader = ffmpeg.StandardError; 

      // wait until ffmpeg comes back 
      ffmpeg.WaitForExit(); 

      // read the output from ffmpeg, which for some reason is found in Process.StandardError 
      result = errorreader.ReadToEnd(); 

      // a little convoluded, this string manipulation... 
      // working from the inside out, it: 
      // takes a substring of result, starting from the end of the "Duration: " label contained within, 
      // (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there) 
      // and going the full length of the timestamp 

      duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length); 
      return duration; 
     } 
    } 

Maj to pomaga.

9

To może być trochę za późno ... Można to zrobić przy minimalnym kodu przy użyciu pakietu Nuget z MediaToolKit

Aby uzyskać więcej informacji chodźmy stąd MediaToolKit

+0

Miałem problemy z korzystaniem z MediaInfo i było to znacznie łatwiejsze.Nie wiem, dlaczego OP nie chciał z niego korzystać. – spongessuck

+0

jak tego użyć? –

Powiązane problemy