C# Get video file duration from metadata

2020-02-19 07:54发布

I am trying to read metadata from a file. I only need the Video -> Length property, however I am unable to find a simple way of reading this information.

I figured this would be fairly easy since it is visible by default in Explorer, however this looks to be way more complicated than I anticipated. The closest I came was using:

Microsoft.DirectX.AudioVideoPlayback.Video video = new Microsoft.DirectX.AudioVideoPlayback.Video(str);
double duration = video.Duration;

However this throws a LoaderLock exception, and I don't know how to deal with it.

Any ideas?

标签: c# metadata
11条回答
Melony?
2楼-- · 2020-02-19 08:26

use MCI

its easy to use and works even on NT:

  using System.Runtime.InteropServices;

  [DllImport("winmm.dll")]
  public static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, int hwndCallback);
  [DllImport("winmm.dll")]
  private static extern int mciGetErrorString(int l1, StringBuilder s1, int l2);

  string cmd = "open " + strFile + " alias video";
  StringBuilder mssg = new StringBuilder(255);

  int h = mciSendString(cmd, null, 0, 0); // open video at mci
  int i = mciSendString("set video time format ms", null, 0, 0); // set time format, you can see other formats at link above
  int j = mciSendString("status video length", mssg, mssg.Capacity, 0); //get video length into mssg 
  Console.WriteLine(mssg.ToString());
  int m = mciSendString("close video", null, 0, 0); //close video
查看更多
Fickle 薄情
3楼-- · 2020-02-19 08:29

Getting duration of video file in Win Rt App or Metro C#:

StorageFile videoFile;
videoFile = await StorageFile.GetFileFromPathAsync(VIDEO_PATH_HERE);
Windows.Storage.FileProperties.VideoProperties x = await videoFile.Properties.GetVideoPropertiesAsync();
var videoDuration = x.Duration;
查看更多
在下西门庆
4楼-- · 2020-02-19 08:31

using DirectShowLib (http://directshownet.sourceforge.net/)

   /// <summary>
    /// Gets the length of the video.
    /// </summary>
    /// <param name="fileName">Name of the file.</param>
    /// <param name="length">The length.</param>
    /// <returns></returns>
    static public bool GetVideoLength(string fileName, out double length)
    {
        DirectShowLib.FilterGraph graphFilter = new DirectShowLib.FilterGraph();
        DirectShowLib.IGraphBuilder graphBuilder;
        DirectShowLib.IMediaPosition mediaPos;
        length = 0.0;

        try
        {
            graphBuilder = (DirectShowLib.IGraphBuilder)graphFilter;
            graphBuilder.RenderFile(fileName, null);
            mediaPos = (DirectShowLib.IMediaPosition)graphBuilder;
            mediaPos.get_Duration(out length);
            return true;
        }
        catch
        {
            return false;
        }
        finally
        {
            mediaPos = null;
            graphBuilder = null;
            graphFilter = null;
        }
    }
查看更多
孤傲高冷的网名
5楼-- · 2020-02-19 08:35

You can use our 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);

//Video length
var duration = analyzeResult.VideoInfo.Format.Duration;
查看更多
Root(大扎)
6楼-- · 2020-02-19 08:37

Take a look at this SO question - Solid FFmpeg wrapper for C#/.NET which links to several ffmpeg .Net implementations. ffmpeg works with most video formats/codecs. That way you don't need to worry about the codec being installed on the machine.

Or look at https://mediaarea.net/en/MediaInfo.

查看更多
不美不萌又怎样
7楼-- · 2020-02-19 08:39

I had the same issue with a small video preview app.

The issue is Managed Debugging Assisstants. This is an issue when using The Managed DirectX 1.1 libraries in VS2005 or 2008. Microsoft has moved on to focus on MDX2 and then XNA rather than Managed DirectX 1 so don't hope too much for a patch.

The easy workaround is to disable the LoaderLock Exception handling while debugging that solution. This should have no real effect on the program anyways since this error only shows up in a debug environment.

To disable go to Debug -> Exceptions -> Managed Debugging Assistants and uncheck LoaderLock.

More info here:http://vivekthangaswamy.blogspot.com/2006/11/loaderlock-was-detected-error-when.html

查看更多
登录 后发表回答