Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get video duration without playing?

Tags:

c#

wpf

video

I am using MediaTimeline and MediaClock to control the WMV video playback.

Timeline = new MediaTimeline(new Uri(VideoFileName));
Clock = Timeline.CreateClock(true) as MediaClock;

When I look at the Clock.NaturalDuration, value is set to Automatic and does not contain duration TimeSpan yet.

When I assign Clock to MediaElement and video starts playing, I can now see the NaturalDuration.TimeSpan is present and OK.

Is there a way to get the video duration other than assigning clock to media element and playing?

Is there a way to get duration even without usage of media element (this would be the best)?

like image 678
Dusan Avatar asked Oct 29 '25 05:10

Dusan


2 Answers

I had similar need to check out video length, so I read that this property is available when MediaOpened event is being raised, otherwise is set to Automatic - read this http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.naturalduration(v=vs.95).aspx

media.MediaOpened += new System.Windows.RoutedEventHandler(media_MediaOpened);
            media.LoadedBehavior = MediaState.Manual;
            media.UnloadedBehavior = MediaState.Manual;
            media.Play();

void media_MediaOpened( object sender, System.Windows.RoutedEventArgs e )
        {
            progress.Maximum = (int)media.NaturalDuration.TimeSpan.TotalSeconds;
            timer.Start();
            isPlaying = true;
        }
like image 64
Marek Bar Avatar answered Oct 31 '25 18:10

Marek Bar


getting duration of video file in Win Rt App or Metro C#

string path = ApplicationData.Current.LocalFolder.Path;
StorageFile videoFile = 
        await StorageFile.GetFileFromPathAsync(presentationItem.Slide_path_local);
Windows.Storage.FileProperties.VideoProperties x = 
        await videoFile.Properties.GetVideoPropertiesAsync();
Duration videoDuration = x.Duration;

Best way to get duration of video file

like image 28
user3714810 Avatar answered Oct 31 '25 20:10

user3714810



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!