8 Replies - 46216 Views - Last Post: 20 August 2010 - 09:43 AM Rate Topic: -----

#1 vamshi09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-July 08

how to get video file duration(time ) using c#

Posted 24 July 2008 - 01:08 AM

hi
Is This A Good Question/Topic? 0
  • +

Replies To: how to get video file duration(time ) using c#

#2 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: how to get video file duration(time ) using c#

Posted 24 July 2008 - 08:22 AM

you could try checking the file properties for this info. Otherwise I think you'd have to come up with some kind of crazy formulas to calculate the time based on the file size, video resolution and sound resolution. Not really sure, never tried it?
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: how to get video file duration(time ) using c#

Posted 24 July 2008 - 08:47 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#4 kusumyadav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-August 08

Re: how to get video file duration(time ) using c#

Posted 22 August 2008 - 03:52 AM

View Postjayman9, on 24 Jul, 2008 - 08:47 AM, said:

[rules][/rules]



i want to convert .flv file into .wmv, for that i am using a thrid party tool
"flash to video console". its a command line.
through my .net code i am initiating its exe.this tool needs the maxtime. for that i need to know the duartion of my flv file.can you please help me to get the duration of flash file.
i am attaching my code here :

Const g_strV2FPath As String = "C:\Program Files\VidFilters\Flash to Video Console\FVEConsole.exe"
Dim strSourceFile As String = "C:\Program Files\Adobe\Flash Media Server 3\applications\FlexWebCamCapture\streams\_definst_\CapturedVideoAudioFile.flv"
Dim Process As New System.Diagnostics.Process

Dim objPlayer As New WMPLib.WindowsMediaPlayer objPlayer.mediaCollection.add(strSourceFile)
objPlayer.currentMedia = objPlayer.mediaCollection.getAll
Dim sngDuration As Single = objPlayer.currentMedia.duration
Process.Start(g_strV2FPath, "-Input " + """" + strSourceFile + """" + " -Output " + """" + "E:\converted wmv\CapturedVideoAudioFile.wmv" + """" + " -MaxTime ""600""" + """" + "")


i am using windows media player but this code is throwing an error.

any help is appriciated

kusum
Was This Post Helpful? 0
  • +
  • -

#5 Korupt  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 185
  • Joined: 22-June 08

Re: how to get video file duration(time ) using c#

Posted 22 August 2008 - 12:14 PM

you could add a timer to your form and then just make it start (on the button that your video is supposed to start) and display the time it's counting

and kusumyadav first of all your code is in VB not C# second of all this is not your topic make your own and ask for help don't hijack other peoples
Was This Post Helpful? 0
  • +
  • -

#6 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: how to get video file duration(time ) using c#

Posted 22 August 2008 - 01:12 PM

Here is a link to a codeproject post that will read the flv header file. You should be able to calculate the duration from this info.

http://www.codeproje...;select=2417085

Hope this helps,

Kitt
Was This Post Helpful? 0
  • +
  • -

#7 hardimon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 25-October 09

Re: how to get video file duration(time ) using c#

Posted 25 October 2009 - 11:18 PM

How about using ffmpeg.exe as a process within your code?

using(Process ffmpeg = new Process())
{
	 String duration;  // soon will hold our video's duration in the form "HH:MM:SS.UU"
	 String result;  // temp variable holding a string representation of our video's duration
	 StreamReader errorreader;  // StringWriter to hold output from ffmpeg

	 // we want to execute the process without opening a shell
	 ffmpeg.StartInfo.UseShellExecute = false;
	 ffmpeg.StartInfo.ErrorDialog = false;

	 // redirect StandardError so we can parse it
	 // for some reason the output comes through over StandardError
	 ffmpeg.StartInfo.RedirectStandardError = true;

	 // set the file name of our process, including the full path
	 // (as well as quotes, as if you were calling it from the command-line)
	 ffmpeg.StartInfo.FileName = "[directory of ffmpeg.exe]\ffmpeg.exe";

	 // set the command-line arguments of our process, including full paths of any files
	 // (as well as quotes, as if you were passing these arguments on the command-line)
	 ffmpeg.StartInfo.Arguments = "-i [directory of video file]\video_file";

	 // start the process
	 ffmpeg.Start();

	 // now that the process is started, we can redirect output to the StreamReader we defined
	 errorreader = ffmpeg.StandardError;

	 // wait until ffmpeg comes back
	 ffmpeg.WaitForExit([time_to_wait_in_milliseconds]);

	 // read the output from ffmpeg, which for some reason is found in Process.StandardError
	 result = errorreader.ReadToEnd();

	 // a little convoluded, this string manipulation...
	 // working from the inside out, it:
	 // takes a substring of result, starting from the end of the "Duration: " label contained within,
	 // (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there)
	 // and going the full length of the timestamp.
	 // The resulting substring is of the form "HH:MM:SS.UU"

	 duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00.00").Length);
}



Quick Edit:

you need to include System (of course...), System.Diagnostics (for Process support), and System.IO (for StreamReaders)

Quick Edit Again:

My fault, the Convert.ToDouble() doesn't change it into a double representing seconds xD. I was doing something else with that...

The conversion from HH:MM:SS to seconds is left as an exercise for the OP :P

I hope I'm not breaking the rules here. It looks like this topic was posted a long time ago, so I figured what the hey. If I'm in the wrong, someone let me know. I'm pretty new.

Hope that helps,
Stephen H

This post has been edited by hardimon: 26 October 2009 - 12:32 AM

Was This Post Helpful? 1

#8 Guest_bmmarko98@gmail.com*


Reputation:

Re: how to get video file duration(time ) using c#

Posted 20 August 2010 - 09:40 AM

View Posthardimon, on 25 October 2009 - 10:18 PM, said:

How about using ffmpeg.exe as a process within your code?

using(Process ffmpeg = new Process())
{
	 String duration;  // soon will hold our video's duration in the form "HH:MM:SS.UU"
	 String result;  // temp variable holding a string representation of our video's duration
	 StreamReader errorreader;  // StringWriter to hold output from ffmpeg

	 // we want to execute the process without opening a shell
	 ffmpeg.StartInfo.UseShellExecute = false;
	 ffmpeg.StartInfo.ErrorDialog = false;

	 // redirect StandardError so we can parse it
	 // for some reason the output comes through over StandardError
	 ffmpeg.StartInfo.RedirectStandardError = true;

	 // set the file name of our process, including the full path
	 // (as well as quotes, as if you were calling it from the command-line)
	 ffmpeg.StartInfo.FileName = "[directory of ffmpeg.exe]\ffmpeg.exe";

	 // set the command-line arguments of our process, including full paths of any files
	 // (as well as quotes, as if you were passing these arguments on the command-line)
	 ffmpeg.StartInfo.Arguments = "-i [directory of video file]\video_file";

	 // start the process
	 ffmpeg.Start();

	 // now that the process is started, we can redirect output to the StreamReader we defined
	 errorreader = ffmpeg.StandardError;

	 // wait until ffmpeg comes back
	 ffmpeg.WaitForExit([time_to_wait_in_milliseconds]);

	 // read the output from ffmpeg, which for some reason is found in Process.StandardError
	 result = errorreader.ReadToEnd();

	 // a little convoluded, this string manipulation...
	 // working from the inside out, it:
	 // takes a substring of result, starting from the end of the "Duration: " label contained within,
	 // (execute "ffmpeg.exe -i somevideofile" on the command-line to verify for yourself that it is there)
	 // and going the full length of the timestamp.
	 // The resulting substring is of the form "HH:MM:SS.UU"

	 duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00.00").Length);
}



Quick Edit:

you need to include System (of course...), System.Diagnostics (for Process support), and System.IO (for StreamReaders)

Quick Edit Again:

My fault, the Convert.ToDouble() doesn't change it into a double representing seconds xD. I was doing something else with that...

The conversion from HH:MM:SS to seconds is left as an exercise for the OP :P

I hope I'm not breaking the rules here. It looks like this topic was posted a long time ago, so I figured what the hey. If I'm in the wrong, someone let me know. I'm pretty new.

Hope that helps,
Stephen H


Hi, i just want to comment your idea. It was my way to go in first project. I was writing ffmpeg wrapper for .NET and a was using string parsing from standardError that ffmpeg.exe generates to retrive duration of input media and the time based progress of a conversion progress. It worked fine with with most video and audio files, but not with all. When I tried to convert You Tube video ffmpeg couldn't read duration of input file. It generated string in format "Duration: N/A" but it done conversion process without any problems.
So, this way to read input file duration is good for most of media types, but not in all...
If you broke the rules I broke them too, by posting year after last replay, but I hope this was useful for further work, just to know :)
Was This Post Helpful? 0

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: how to get video file duration(time ) using c#

Posted 20 August 2010 - 09:43 AM

I appreciate everyone's willingness to help, but please avoid necroposting in the future. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1