How to get video duration from mp4, wmv, flv, mov videos

后端 未结 10 664
深忆病人
深忆病人 2020-11-30 07:51

Alright. Actually i need mostly the mp4 format. But if it is possible to get for other types as well that would be nice. I just need to read the duration of the file. How ca

相关标签:
10条回答
  • 2020-11-30 08:06

    FFMPEG project has a tool, called ffprobe which can provide you the information you need about your multimedia files and ouput the information in a nicely formated JSON.

    Take a look at this answer for an example.

    0 讨论(0)
  • 2020-11-30 08:08

    Using Windows Media Player Component also, we can get the duration of the video.
    Following code snippet may help you guys :

    using WMPLib;
    // ...
    var player = new WindowsMediaPlayer();
    var clip = player.newMedia(filePath);
    Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
    

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

    0 讨论(0)
  • 2020-11-30 08:09

    I had the same problem and we built a wrapper for ffprobe Alturos.VideoInfo. You can use it simply by installing the nuget package. Also the ffprobe binary is required.

    PM> install-package Alturos.VideoInfo
    

    Example

    var videoFilePath = "myVideo.mp4";
    
    var videoAnalyer = new VideoAnalyzer("ffprobe.exe");
    var analyzeResult = videoAnalyer.GetVideoInfo(videoFilePath);
    
    var duration = analyzeResult.VideoInfo.Format.Duration;
    
    0 讨论(0)
  • 2020-11-30 08:10

    IMHO you could use MediaInfo which gives you a lot of information about media files.
    There is a CLI for it so you can use it from your code and get info you need.
    You can take a look at this link.

    0 讨论(0)
  • 2020-11-30 08:11
    StreamReader errorreader;
    string InterviewID = txtToolsInterviewID.Text;
    
    Process ffmpeg = new Process();
    
    ffmpeg.StartInfo.UseShellExecute = false;
    ffmpeg.StartInfo.ErrorDialog = false;
    ffmpeg.StartInfo.RedirectStandardError = true;
    
    ffmpeg.StartInfo.FileName = Server.MapPath("ffmpeg.exe");
    ffmpeg.StartInfo.Arguments = "-i " + Server.MapPath("videos") + "\\226.flv";
    
    
    ffmpeg.Start();
    
    errorreader = ffmpeg.StandardError;
    
    ffmpeg.WaitForExit();
    
    string result = errorreader.ReadToEnd();
    
    string duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00.00").Length);
    
    0 讨论(0)
  • You can use DirectShow API MediaDet object, through DirectShow.NET wrapper library. See Getting length of video for code sample, get_StreamLength gets you the duration in seconds. This assumes Windows has MPEG-4 demultiplexer installed (requires third party components with Windows prior to 7, I believe the same applies to another answer by cezor, there are free to redistribute components though).

    0 讨论(0)
提交回复
热议问题