How to get video duration from mp4,m3u8 or any other file

雨燕双飞 提交于 2019-12-10 12:19:04

问题


I found the NReco.VideoInfo library to be the best option and simpler

1] Find the NReco.VideoInfo library in NuGet Package Manager and import it in your project

2] After that import the namespace "using NReco.VideoInfo"

3] Add the below line of code into the method.

var ffProbe = new FFProbe();
var videoInfo = ffProbe.GetMediaInfo(blob.Uri.AbsoluteUri);
return videoInfo.Duration.TotalMilliseconds;

here you can see some other option


回答1:


Why don't you use the windows media player to solve this problem?

using WMPLib;

public Double getDuration(String path)
{
    WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
    IWMPMedia mediaInfo = wmp.newMedia(file);
    return mediaInfo.duration;
}



回答2:


If you mean from a local file, the following will show the duration without loading the entire video file (test on mp4):

var inputBox = document.getElementById("videoInput")

inputBox.onchange = function(files)  {
  alert("starting")
  var video = document.createElement('video');
  alert("video created")
  video.preload = 'metadata';
  video.onloadedmetadata = function() {
    alert("metadata loaded")
    var duration = video.duration;
    alert("duration is " + duration)
  }
  var selectedVID = document.getElementById("videoInput").files[0];
  video.src = URL.createObjectURL(selectedVID);
}
<div id="input-upload-file">
  <p>
  Video Upload Duration test
  </p>
  <input id="videoInput" type="file" class="upload" name="fileUpload">
</div>

The same approach will work on streamed videos also.



来源:https://stackoverflow.com/questions/46968928/how-to-get-video-duration-from-mp4-m3u8-or-any-other-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!