1

How can we get the duration of uploaded video in mvc controller ? When I have uploaded video it only give content-type,content-length etc kind of details but not the duration of the video.

So how can we get the duration of the video in mvc ?

Thanks for the help !

XamDev
  • 3,377
  • 12
  • 58
  • 97
  • Possible duplicate of [How to get video duration from mp4, wmv, flv, mov videos](http://stackoverflow.com/questions/10190906/how-to-get-video-duration-from-mp4-wmv-flv-mov-videos) – sab669 Dec 08 '16 at 13:27
  • For the most part, "meta information" of different file types is natively not available through .NET and you'll often have to rely on a third-party library. I tried to recently write something to re-name a ton of music files of mine using the meta data for aritst, album, track etc. but there is no .NET support for that either. See the "Possible Duplicate" link I tagged this as. – sab669 Dec 08 '16 at 13:29

5 Answers5

2

You can use this nuget package:

Install-Package Xabe.FFMpeg

I'm trying to make easy to use, cross-platform FFmpeg wrapper.

You can find more information about this at Xabe.FFmpeg

IMediaInfo mediaInfo = await MediaInfo.Get("videofile.mkv");
var videoDuration = mediaInfo.VideoStreams.First().Duration;

More info about getting duration of video file in documentation

Tomasz Żmuda
  • 629
  • 6
  • 9
1

Yuo can use FFMPEG Wrapper for .NET Core to extract any information about uploaded video but be careful because it's still on beta (https://github.com/lecode-official/ffmpeg-dotnet/blob/master/Source/FFmpegDotNet.Interop/Formats/AVDurationEstimationMethod.cs)

J. Doe
  • 2,651
  • 1
  • 13
  • 31
0

just for those who doesn't know how to do it

first add package

<PackageReference Include="Xabe.FFMpeg" Version="3.1.0" />

then

 public static void Main(string[] args)
    {
        Load().Wait();

        BuildWebHost(args).Run();
    }


 public static async Task Load()
    {
        //Set directory where app should look for FFmpeg 
        FFmpeg.ExecutablesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FFmpeg");
        //Get latest version of FFmpeg. It's great idea if you don't know if you had installed FFmpeg.
        await FFmpeg.GetLatestVersion();
    }

and usage

   IMediaInfo mediaInfo = await MediaInfo.Get(@"C:\Users\username\source\repos\MyWebApp\uploads\videos\file1.mp4");
        var videoDuration = mediaInfo.VideoStreams.First().Duration;
unos baghaii
  • 2,539
  • 4
  • 25
  • 42
0

First, I try FFMPEG Wrapper but it gets an error after found an easy solution.

You can use this nugget package:

 Install-Package Microsoft.WindowsAPICodePack-Shell -Version 1.1.0

In your project add two namespaces.

using Microsoft.WindowsAPICodePack.Shell; using.Microsoft.WindowsAPICodePack.Shell.PropertySystem;

ShellFile so = ShellFile.FromFilePath(your file path);
double nanoseconds;
double.TryParse(so.Properties.System.Media.Duration.Value.ToString(),
out nanoseconds);

if (nanoseconds > 0)
{
     double seconds = Convert100NanosecondsToMilliseconds(nanoseconds) / 1000;
     int ttl_seconds = Convert.ToInt32(seconds);
     TimeSpan time = TimeSpan.FromSeconds(ttl_seconds);
} 

public static double Convert100NanosecondsToMilliseconds(double nanoseconds)
{           
        return nanoseconds * 0.0001;
}

Here i store seconds in TimeSpan so its directly give hours:minutes:seconds.

Brijesh Mavani
  • 1,070
  • 1
  • 15
  • 23
0

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.

Rish
  • 1,303
  • 9
  • 22