2014-09-26 15 views
10

Tôi đã tìm kiếm trên Google và StackOverflow trong một vài giờ. Dường như có rất nhiều câu hỏi tương tự trên StackOverflow nhưng tất cả chúng đều khoảng 3-5 tuổi.Cách tốt nhất để tải siêu dữ liệu video từ tệp MP4 trong ASP.Net MVC bằng C# là gì?

Sử dụng FFMPEG vẫn là cách tốt nhất trong những ngày này để lấy siêu dữ liệu từ tệp video trong ứng dụng web .NET? Và nếu vậy, trình bao bọc C# tốt nhất là gì?

Tôi đã thử MediaToolkit, MediaFile.dll mà không gặp bất kỳ sự may mắn nào. Tôi thấy ffmpeg-csharpe nhưng có vẻ như nó đã không được xúc động trong một vài năm.

Tôi chưa tìm thấy bất kỳ dữ liệu hiện tại nào về chủ đề này. Có khả năng kéo siêu dữ liệu từ video được tích hợp vào phiên bản .NET mới nhất không?

Về cơ bản, tôi đang tìm bất kỳ hướng nào vào thời điểm này.

Tôi nên thêm rằng bất cứ điều gì tôi sử dụng có thể được gọi hàng ngàn lần mỗi giờ để nó sẽ cần phải có hiệu quả.

Trả lời

15

Có một cái nhìn tại MediaInfo dự án (http://mediaarea.net/en/MediaInfo)

nó được thông tin đầy đủ về hầu hết các loại phương tiện truyền thông, và thư viện được đi kèm với một C# helper lớp học mà là dễ sử dụng.

Bạn có thể tải thư viện và helper class cho các cửa sổ từ đây:

http://mediaarea.net/en/MediaInfo/Download/Windows(DLL mà không cần cài đặt)

Lớp helper tọa lạc tại Developers\Source\MediaInfoDLL\MediaInfoDLL.cs, chỉ cần thêm nó vào dự án của bạn và sao chép MediaInfo.dll vào thùng rác của bạn.

Cách sử dụng

bạn có thể có được thông tin bằng cách yêu cầu thông số cụ thể từ các thư viện, đây là một mẫu:

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

Bạn có thể dễ dàng có được tất cả các thông tin trong định dạng chuỗi bằng cách gọi Inform():

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

nếu bạn cần thêm thông tin về các tham số có sẵn, bạn có thể đơn giản y truy vấn tất cả trong số họ bằng cách gọi Options("Info_Parameters"):

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

Cảm ơn bạn. Với sự giúp đỡ của bạn, tôi đã có thể nhận MediaInfo làm việc trong giải pháp của tôi! – Maddhacker24

2

tôi đề nghị bạn sử dụng ffmpeg với Process.Start, mã trông giống như sau:

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; 
     } 
    } 

May nó giúp.

9

Nó có thể là hơi muộn ... Bạn có thể làm điều này với mã số tối thiểu bằng cách sử dụng gói NuGet của MediaToolKit

Để biết thêm thông được đi từ đây MediaToolKit

+0

Tôi gặp sự cố khi sử dụng MediaInfo và điều này dễ dàng hơn nhiều.Không chắc tại sao OP không muốn sử dụng nó. – spongessuck

+0

cách sử dụng? –

Các vấn đề liên quan