Reputation: 3667
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 !
Upvotes: 1
Views: 3970
Reputation: 1383
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 inSystem32
folder.
Upvotes: 0
Reputation: 2679
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;
Upvotes: 0
Reputation: 1158
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.
Upvotes: 0
Reputation: 637
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
Upvotes: 2
Reputation: 2747
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)
Upvotes: 1