首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ASP.Net MVC中使用C#从视频文件中获取视频元数据的最佳方法是什么?

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

Stack Overflow用户
提问于 2014-09-26 02:46:27
回答 4查看 27.8K关注 0票数 16

我已经在谷歌和StackOverflow上搜索了几个小时了。在StackOverflow上似乎有很多类似的问题,但它们都在3-5岁左右。

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

我试过MediaToolkit,MediaFile.dll,没有任何运气。我看到了,但它看起来几年没碰过了。

我还没有找到关于这个问题的任何最新数据。现在,从.NET最新版本中内置的视频中提取元数据的能力了吗?

在这一点上我基本上是在寻找方向。

我要补充的是,无论我使用什么,都可以每小时调用数千次,因此它必须是高效的。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-09-26 05:11:21

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

它获得关于大多数媒体类型的广泛信息,库与易于使用的c#助手类捆绑在一起。

您可以从这里下载windows的库和助手类:

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

helper类位于Developers\Source\MediaInfoDLL\MediaInfoDLL.cs,只需将其添加到项目中并将MediaInfo.dll复制到bin中即可。

使用

您可以通过从库中请求特定参数来获取信息,下面是一个示例:

代码语言:javascript
复制
[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()轻松地获得字符串格式的所有信息。

代码语言:javascript
复制
        var mi = new MediaInfo();
        mi.Open(@"video path here");
        Console.WriteLine(mi.Inform());
        mi.Close();

如果需要有关可用参数的更多信息,只需调用Options("Info_Parameters")查询所有参数即可。

代码语言:javascript
复制
        var mi = new MediaInfo();
        Console.WriteLine(mi.Option("Info_Parameters"));
        mi.Close();
票数 22
EN

Stack Overflow用户

发布于 2015-12-24 07:58:56

可能有点晚了..。您可以使用NuGet包MediaToolKit使用最少的代码来完成这一任务。

要获得更多信息,请从这里MediaToolKit

票数 11
EN

Stack Overflow用户

发布于 2014-09-26 03:03:29

我建议您对Process.Start使用ffmpeg,代码如下所示:

代码语言:javascript
复制
    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;
        }
    }

希望它能帮上忙。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26051273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档