簡體   English   中英

使用 C# 從 ASP.Net MVC 中的視頻文件中獲取視頻元數據的最佳方法是什么?

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

我已經在 Google 和 StackOverflow 上搜索了好幾個小時。 StackOverflow 上似乎有很多類似的問題,但它們都大約有 3-5 年的歷史。

如今,使用 FFMPEG 仍然是從 .NET Web 應用程序中的視頻文件中提取元數據的最佳方式嗎? 如果是這樣,最好的 C# 包裝器是什么?

我試過 MediaToolkit, MediaFile.dll 沒有任何運氣。 我看到了 ffmpeg-csharpe,但看起來它已經好幾年沒有被觸及了。

我還沒有找到關於這個主題的任何當前數據。 現在是否能夠從內置於最新版本 .NET 的視頻中提取元數據?

我現在基本上正在尋找任何方向。

我應該補充一點,我使用的任何東西每小時都可能被調用數千次,因此它需要高效。

看看MediaInfo項目 ( http://mediaarea.net/en/MediaInfo )

它獲取有關大多數媒體類型的大量信息,並且該庫與易於使用的 ac# helper 類捆綁在一起。

您可以從此處下載適用於 Windows 的庫和幫助程序類:

http://mediaarea.net/en/MediaInfo/Download/Windows (不帶安裝程序的 DLL)

helper 類位於Developers\\Source\\MediaInfoDLL\\MediaInfoDLL.cs ,只需將其添加到您的項目並將MediaInfo.dll復制到您的 bin。

用法

您可以通過從庫中請求特定參數來獲取信息,這是一個示例:

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

您可以通過調用Inform()輕松獲取字符串格式的所有信息:

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

如果您需要有關可用參數的更多信息,只需調用Options("Info_Parameters")即可查詢所有參數:

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

可能有點晚了...您可以使用 MediaToolKit 的 NuGet 包以最少的代碼完成此操作

有關更多信息,請從這里開始MediaToolKit

我建議你使用 ffmpeg 和 Process.Start,代碼如下:

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

可能會有所幫助。

ffmpeg 還提供了一個特殊的應用程序ffprobe來讀取元數據。 我們為 .net 編寫了一個包裝器並提供了一個nuget 包,您可以在這里找到它: Alturos.VideoInfo

PM> Install-Package Alturos.VideoInfo

例子:

var videoFilePath = "myVideo.mp4";

var videoAnalyer = new VideoAnalyzer();
var analyzeResult = videoAnalyer.GetVideoInfo(videoFilePath);
var videoInfo = analyzeResult.VideoInfo;
//videoInfo.Format.Filename = "myVideo.mp4"
//videoInfo.Format.NbStreams = 1
//videoInfo.Format.NbPrograms = 0
//videoInfo.Format.FormatName = "mov,mp4,m4a,3gp,3g2,mj2"
//videoInfo.Format.FormatLongName = "QuickTime / MOV"
//videoInfo.Format.StartTime = 0
//videoInfo.Format.Duration = 120 //seconds
//videoInfo.Format.Size = 2088470 //bytes
//videoInfo.Format.BitRate = 139231
//videoInfo.Format.ProbeScore = 100
//videoInfo.Format.Tags["encoder"] = Lavf57.76.100
//videoInfo.Streams[0].CodecType = "video" //Video, Audio
//videoInfo.Streams[0]...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM