Reputation: 39
using Microsoft.DirectX.AudioVideoPlayback;
Video vvideo = new Video(FileUpload.FileName.ToString());
StringBuilder sb = new StringBuilder();
sb.Append(duration.toString());
error message;
“Installed an access attempt was made to the application domain.”
“vvideo” instance is created error msj ://
But down found cods I found a working code in c #. but asp.net does not work
string file1 = "c://ds.mpeg"
IMediaPosition m_objMediaPosition = null;
FilgraphManager m_objFilterGraph = new FilgraphManager();
m_objFilterGraph.RenderFile(filename);
m_objMediaPosition = m_objFilterGraph as IMediaPosition;
int s = (int)m_objMediaPosition.Duration;
int h = s / 3600;
int m = (s - (h * 3600)) / 60;
s = s - (h * 3600 + m * 60);
I DON'T TAKE VIDEO DURATION BOYS :/
Upvotes: 2
Views: 5243
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.TryParse(so.Properties.System.Media.Duration.Value.ToString(), out var 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: 1
Reputation: 1079
You can use our wrapper for ffprobe Alturos.VideoInfo.
You can use it simply by installing the nuget
package. Also the ffprobe binary is required.
PM> install-package Alturos.VideoInfo
Example
var videoFilePath = "myVideo.mp4";
var videoAnalyer = new VideoAnalyzer("ffprobe.exe");
var analyzeResult = videoAnalyer.GetVideoInfo(videoFilePath);
var duration = analyzeResult.VideoInfo.Format.Duration;
Upvotes: 0
Reputation: 39
I make Function everywhere it uses ;)
public string f_VideoDuration((add path)parameters)
{
try
{
string sInputVideo = Page.MapPath(add path);
string sPath = " -i " + sInputVideo ;
Process ffmepg = new Process();
ffmepg.StartInfo.FileName = (Server.MapPath("ffmpeg.exe"));
ffmepg.StartInfo.UseShellExecute = false;
ffmepg.StartInfo.RedirectStandardOutput = true;
ffmepg.StartInfo.RedirectStandardError = true;
ffmepg.StartInfo.CreateNoWindow = true;
ffmepg.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ffmepg.StartInfo.Arguments = sPath;
ffmepg.EnableRaisingEvents = true;
ffmepg.Start();
string sDuration = ffmepg.StandardError.ReadToEnd().ToString();
ffmepg.Close();
ffmepg.Dispose();
sDuration = sDuration.Remove(0, sDuration.LastIndexOf("Duration: ") + 10);
sDuration = sDuration.Substring(0, sDuration.IndexOf(","));
return sDuration;
}
catch (Exception ex)
{
throw (ex);
}
}
Upvotes: 1