2017-08-09 107 views
13

获取什么是最佳方式?上传后获取视频的持续时间

我有一个字段,用户将上传视频。

当您点击上传按钮时,视频会上传。这工作完美。

我需要从物理路径视频的时间,我想:

using WMPLib; 
WindowsMediaPlayer wmp = new WindowsMediaPlayerClass(); 
IWMPMedia mediaInfo = wmp.newMedia(Server.MapPath("~/Uploads/test.mp4")); 
double duration = mediaInfo.duration; 

但其没有工作,我得到错误:

The type 'WMPLib.WindowsMediaPlayerClass' has no constructors defined Interop type 'WMPLib.WindowsMediaPlayerClass' cannot be embedded.

如何捕捉时间?

+2

的可能的复制[?什么是用C#从ASP.Net MVC一个MP4文件中获取视频元数据的最佳方式(https://stackoverflow.com/questions/26051273/whats - 最好的方式获取视频元数据 - 从一个MP4文件在ASP网络mvc - 使用C) – Clint

+2

请参阅https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/2d095670-9920-44f3-a386-3cfeceeb14c0/c-get-video-duration?forum = csharpgeneral for the solution。 –

+0

我尝试了他们,但他们不帮助我:( –

回答

3

使用DirectShow.Net包装库时,可以使用DirectShow API MediaDet对象。方法get_StreamLength是以秒为单位获取视频长度的方法,可以使用确定性算法将其转换为分钟/小时。要使用此API,请确保操作系统上已安装MPEG-4依赖项。

或者您可以选择Media Info库从视频文件获取信息。

请参阅使用DirectShow.NET包装库的实现:Getting length of video

2

我们得到了许多方面的录像持续时间

方法1:壳牌32 API

方法2:WMPLib(Windows媒体播放器库)

方法3:FFmpeg的包装下面

检查链接

方法1和方法2链接get-the-length-of-a-video-in-c#

方法3参考链接video file time duration in ffmpeg

在此方法用于获取视频性能和duraion所有这三个我被检查previously.it的文件做工精细一些自定义attriburtes属性不显示在前2个方法中。

方法3不能用于共享主机。它需要专用服务器。

9

您可以使用此NuGet包:

Install-Package Xabe.FFMpeg 

我试图让易用,跨平台FFmpeg的包装。

您可以在https://github.com/tomaszzmuda/Xabe.FFmpeg

IVideoInfo videoInfo = new VideoInfo("videofile.mkv"); 
var videoDuration = videoInfo.VideoProperties.VideoDuration; 

更多信息找到在获得视频文件的时间更多这方面的信息: https://github.com/tomaszzmuda/Xabe.FFmpeg/wiki/Getting-an-information-about-video

+0

我试过FFMpeg项目,代码看起来不错,但无法编译,如果你有时间可以提供一个GitHub指南来让它工作? –

+0

该项目是在.NET标准2.0中。也许这是问题所在。根据文档.NET Standard在4.6.1以上的.NET Framework中受支持([link](https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announceing-net-standard-2-0 /)),但不是每次都可以使用它。我会尽力解决这个问题。 @JeremyThompson如果可以的话,请在github上发布编译错误。 –

+0

@TomaszŻmuda'VideoInfo'正在等待废弃,正在被'MediaInfo'取代。 – maxp

2

你的代码看起来很好,但如果你可以检查媒体的attributes信息如:

using WMPLib; // this file is called Interop.WMPLib.dll 
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass(); 
string FilePath = "yourFilePath"; 
IWMPMedia mediaInfo = wmp.newMedia(FilePath); 

// write duration 
Console.WriteLine("Duration = " + mediaInfo.duration); 

// write named attributes 
for (int i=0; i < mediaInfo.attributeCount; i++) 
{ 
    Console.WriteLine(mediaInfo.getAttributeName(i) + " = " + mediaInfo.getItemInfo(mediaInfo.getAttributeName(i))); 
} 
0

以下代码片段可能会帮助您球员:

using WMPLib; 
// ...your code here... 

var player = new WindowsMediaPlayer(); 
var clip = player.newMedia(VideoFilePath); 
Console.WriteLine(TimeSpan.FromSeconds(clip.duration)); 

and don't forget to add the reference of wmp.dll which will be present in System32 folder.

相关问题