Get duration of Video after upload

Viewed 4748

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?

5 Answers

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.

Related