Getting length of video

后端 未结 7 2002
忘掉有多难
忘掉有多难 2020-12-01 12:36

I am having trouble finding a simple example of how to get the video length of a file programmatically. Many people say, oh use this library/wrapper or whatever, but do not

7条回答
  •  独厮守ぢ
    2020-12-01 13:36

    The easist and flawless solution I found is to use MediaToolkit nuget package.

    using MediaToolkit;
    
    // a method to get Width, Height, and Duration in Ticks for video.
    public static Tuple GetVideoInfo(string fileName)
    {
        var inputFile = new MediaToolkit.Model.MediaFile { Filename = fileName };
        using (var engine = new Engine())
        {
            engine.GetMetadata(inputFile);
        }
    
        // FrameSize is returned as '1280x768' string.
        var size = inputFile.Metadata.VideoData.FrameSize.Split(new[] { 'x' }).Select(o => int.Parse(o)).ToArray();
    
        return new Tuple(size[0], size[1], inputFile.Metadata.Duration.Ticks);
    }
    

提交回复
热议问题