什么';这是使用C#从ASP.NETMVC中的视频文件中获取视频元数据的最佳方式
本文关键字:获取 文件 的视频 视频 元数据 方式 最佳 NETMVC ASP 什么 | 更新日期: 2023-09-27 18:24:40
我在Google和StackOverflow上搜索了好几个小时。StackOverflow上似乎有很多类似的问题,但它们都是3-5年前的问题。
使用FFMPEG仍然是目前从.NET web应用程序中的视频文件中提取元数据的最佳方式吗?如果是这样的话,最好的C#包装器是什么?
我试过MediaToolkit,MediaFile.dll,但没有任何运气。我看过ffmpeg csharpe,但看起来它已经好几年没人碰过了。
我没有找到关于这个主题的任何最新数据。从视频中提取元数据的功能现在已经内置到最新版本的.NET中了吗?
在这一点上,我基本上在寻找任何方向。
我应该补充一点,无论我使用什么,每小时都可以调用数千次,所以它需要高效。
看看MediaInfo
项目(http://mediaarea.net/en/MediaInfo)
它获得了关于大多数媒体类型的大量信息,并且该库与一个易于使用的c#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一起使用。首先,代码如下:
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]...