MoviePlayer.Duration

Hi all,

I need to get the duration of a video file. I’m loading it in a MoviePlayer, playing it, grabbing the duration, then unloading it. Consider the following:

Movieplayer1.Movie = f.OpenAsMovie
Movieplayer1.Volume = 0
Movieplayer1.Play
Textfield1.text = MoviePlayer1.Duration.ToText
Movieplayer1.stop
Movieplayer1.close

This works fine on Windows. But on Mac, Textfield1 ends up getting NaN.

I’ve determined the problem is that the MoviePlayer doesn’t have enough time to start playing the movie before I query the Duration. If I wait anywhere from .5 to 3 seconds (depending on the file and where it is), I can poll .Duration and everything works.

But because the load time is variable and the .Play command seems to be non-blocking (next command executes immediately), I need to find out WHEN the MoviePlayer starts playing. Unfortunately, per the docs, I don’t see a way to query the status of the MoviePlayer (find out if it’s playing/paused/stopped/etc), unless I’m missing something.

The MoviePlayer.Play event also fires too early to query .Duration.

I could test the .Position value, but the precision of that value is in full seconds, which will introduce a 1s slow down in each loop of my application. Unacceptable when testing the lengths of hundreds or thousands of files.

Has anyone encountered this before and/or come up with a crafty workaround?

After taking a nap and re-looking at this with fresh eyes, I built a workaround using a timer, waiting for the duration to be nonzero. It’s kludgy, but it works for the most part. It will get caught in a loop when it encounters a file with an actual duration of zero though.

Would be nice for the MoviePlayer.Play command to have a blocking option.

LR: The movie must be playing for this property to hold a value. If no movie is playing, it is set equal to zero.

There are also command line tools which can display the duration

mdls on Mac, ffmpeg, ffprobe …

ffmpeg -i movie.mp4 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,//
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 movie.mp4

If you search in the forum the timer seems to be the fallback position to this question almost universally.

The movie is loaded asynchronously in the background.

You can use our plugin to work with AVAssetMBS class directly and simply loop until status is loaded and you can access the duration.