使用C#从ASP.Net MVC中的MP4文件获取视频元数据的最佳方法是什么?

Mad*_*r24 11 c# asp.net ffmpeg

我一直在谷歌和StackOverflow上搜索好几个小时.在StackOverflow上似乎有很多类似的问题,但它们都是大约3-5岁.

如今使用FFMPEG仍然是从.NET Web应用程序中的视频文件中提取元数据的最佳方式吗?如果是这样,那里最好的C#包装器是什么?

我试过MediaToolkit,MediaFile.dll没有任何运气.我看到ffmpeg-csharpe,但看起来好几年没有被触及过.

我还没有找到关于这个主题的任何现有数据.是否能够从内置于最新版.NET的视频中提取元数据?

我现在基本上都在寻找任何方向.

我应该补充说,无论我使用什么,每小时都可以调用数千次,因此需要高效.

use*_*830 19

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

它获取了大多数媒体类型的大量信息,并且该库与ac#helper类捆绑在一起,易于使用.

您可以从此处下载Windows的库和帮助程序类:

http://mediaarea.net/en/MediaInfo/Download/Windows (没有安装程序的DLL)

帮助程序类位于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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以通过调用Inform()以下方式轻松获取字符串格式的所有信

        var mi = new MediaInfo();
        mi.Open(@"video path here");
        Console.WriteLine(mi.Inform());
        mi.Close();
Run Code Online (Sandbox Code Playgroud)

如果您需要有关可用参数的更多信息,可以通过调用Options("Info_Parameters")以下方法查询所有参数:

        var mi = new MediaInfo();
        Console.WriteLine(mi.Option("Info_Parameters"));
        mi.Close();
Run Code Online (Sandbox Code Playgroud)


khe*_*eya 11

这可能有点晚......你可以使用MediaToolKit的NuGet包以最少的代码完成这项工作

欲了解更多信息,请访问MediaToolKit


non*_*ast 5

我建议您将 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

愿它有帮助。