15

What is best way to get?

I have a field where user will upload video.

When you click the upload button, Video get uploaded. This work perfectly.

I needed the duration of the video from physical path, I tried:

using WMPLib;
WindowsMediaPlayer wmp = new WindowsMediaPlayerClass();
IWMPMedia mediaInfo = wmp.newMedia(Server.MapPath("~/Uploads/test.mp4"));
double duration = mediaInfo.duration;

But its not working, I get error:

The type 'WMPLib.WindowsMediaPlayerClass' has no constructors defined Interop type 'WMPLib.WindowsMediaPlayerClass' cannot be embedded.

How to catch the duration?

  • 2
    Possible duplicate of [What's the best way to get video metadata from a MP4 file in ASP.Net MVC using C#?](https://stackoverflow.com/questions/26051273/whats-the-best-way-to-get-video-metadata-from-a-mp4-file-in-asp-net-mvc-using-c) – Clint Aug 09 '17 at 11:40
  • 2
    Refer https://social.msdn.microsoft.com/Forums/vstudio/en-US/2d095670-9920-44f3-a386-3cfeceeb14c0/c-get-video-duration?forum=csharpgeneral for the solution. – Alsamil Mehboob Aug 11 '17 at 05:52
  • i tried them but they are not helping me :( –  Sep 06 '17 at 05:15
  • @irshadjm did you tried to add the `WindowsMediaPlayer` Embedded explicitly by going to properties of that `WMPLib` and setting `Embeded = True` – Krsna Kishore Sep 06 '17 at 12:44
  • You will have to ask Code that is designed to display/process the video for that detail. The filelenght could never be a indicator. And there can be so many alternate tracks of video/audio it might not even be constant for one specific clip. I think it is customary to accept just about "any kind of video", then do conversion to a default format serverside. While doing that you can figure the lenght as a side effect. Having it in one form will also help detecting duplicates (ideally use some checksums for that). – Christopher Sep 11 '17 at 15:08
  • @irshadjm will the videos uploaded always be `.mp4`? – VC.One Sep 11 '17 at 20:23
  • @irshadjm nevermind. If always MP4 I would have just told you which part of MP4's **bytes** holds the **duration** (eg: where other programs also check to know duration). – VC.One Sep 13 '17 at 08:17

5 Answers5

9

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 FFmpeg.GetMediaInfo("videofile.mkv");
var videoDuration = mediaInfo.VideoStreams.First().Duration;

More info about getting duration of video file at: https://ffmpeg.xabe.net/docs.html

Tomasz Żmuda
  • 629
  • 6
  • 9
  • I tried the FFMpeg project, code looks good but couldn't get it to compile, if you have time could you provide a GitHub guide to get it working? – Jeremy Thompson Sep 13 '17 at 01:43
  • 1
    This project is in .NET Standard 2.0. Maybe this is the problem. According to documentation .NET Standard is supported in .NET Framework above 4.6.1 ([link](https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-net-standard-2-0/)) but not everytime works on it. I'll try to fix the problem. @JeremyThompson if you could, please make issue on github with compilation errors. – Tomasz Żmuda Sep 14 '17 at 04:46
  • 1
    @TomaszŻmuda `VideoInfo` is pending obsoletion, and is being replaced with `MediaInfo`. – maxp Jan 02 '18 at 15:16
  • .Get() is not a function of MediaInfo – Freerey May 21 '21 at 19:42
  • 1
    @Freerey thanks for editing. Public API was changed a some time ago – Tomasz Żmuda May 22 '21 at 20:07
2

With DirectShow.Net wrapper library, you can use the DirectShow API MediaDet object. The method get_StreamLength is the one that gets the length of the video in seconds, which can be converted into mins/hrs using a deterministic algorithm. To use this API make sure the MPEG-4 dependency has been installed on your operating system.

or you can opt for Media Info library for getting information from a video file.

See an implementation using DirectShow.NET wrapper library: Getting length of video

Danyal Imran
  • 2,515
  • 13
  • 21
2

We get the video Durations in many ways are

Method 1: Shell 32 API

Method 2: WMPLib (windows Media Player Library)

Method 3: FFMpeg Wrapper

check the below links

Method 1 and Method 2 Link get-the-length-of-a-video-in-c#

Method 3 Reference Link video file time duration in ffmpeg

In this Method used for get video properties and duraion all these three i was checked previously.it's working fine some custom attriburtes in file properties not show in first 2 Methods.

the Method 3 is not working in shared hosting. it's requied for dedicated server.

umasankar
  • 599
  • 1
  • 9
  • 28
2

Your code looks fine but if you can check the attributes of the media Info like:

using WMPLib; // this file is called Interop.WMPLib.dll
WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();
string FilePath = "yourFilePath";
IWMPMedia mediaInfo = wmp.newMedia(FilePath);

// write duration
Console.WriteLine("Duration = " + mediaInfo.duration);

// write named attributes
for (int i=0; i < mediaInfo.attributeCount; i++) 
{
    Console.WriteLine(mediaInfo.getAttributeName(i) + " = " +  mediaInfo.getItemInfo(mediaInfo.getAttributeName(i)) );
}
0

Following code snippet may help you guys :

using WMPLib;
// ...your code here...

var player = new WindowsMediaPlayer();
var clip = player.newMedia(VideoFilePath);
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