I’m scripting with the VLC browser plugin to determine the length of any video file. I first tell VLC to attempt to play the file. Then I periodically probe it for its length. Once it tells me the length is non-zero, I know the video has successfully begun playing and the length is accurate.
The hard part is error detection. I have to detect if the supplied file is a busted video or not even video at all. Someone could lie with a text file incorrectly named as video.avi and VLC wouldn’t be able to play it. I arbitrarily decided that if VLC reports the length as 0 for 5 straight seconds, then I’d consider the supplied file a dud. Is this an accurate assumption? Is it possible that a severely fragmented harddrive would take more then 5 seconds to supply VLC with a video file? Does the bitrate of the file have anything to do with read time?
Below a snippet of my Javascript which determines the file length. You don’t have to read it to understand my question, but some of you might like to see it.
/**
* Find the total length of a playlist item.
*
* @param PlaylistItem playlistItem
* @param options
* onSuccess: void function(int length)
* onFailure: void function() - timeout
* onComplete: void function() - called after onSuccess or onFailure
* @return void
*/
findLength: function(playlistItem, options) {
var option = {
onSuccess: Prototype.emptyFunction,
onFailure: Prototype.emptyFunction,
onComplete: Prototype.emptyFunction
};
Object.extend(option, options);
if (playlistItem.getLength() > 0) {
option.onSuccess(playlistItem.getLength());
option.onComplete();
}
if (this.lengthPoller) {
this.lengthPoller.stop();
}
this.preview(playlistItem);
this.lengthPoller = new PeriodicalExecuter(
function(poller) {
if (this.secondsInComa >= MYAPP.Vlc.MAX_SECONDS_IN_COMA) {
this.secondsInComa = 0;
option.onFailure();
this.stop();
poller.stop();
option.onComplete();
} else {
var currLength = this.vlc.input.length;
if (currLength > 0) {
currLength /= 1000;
playlistItem.setLength(currLength);
option.onSuccess(currLength);
this.secondsInComa = 0;
this.stop();
poller.stop();
option.onComplete();
} else {
this.secondsInComa += MYAPP.Vlc.LENGTH_POLLING_PERIOD;
}
}
}.bind(this),
MYAPP.Vlc.LENGTH_POLLING_PERIOD
);
}
I found out that it can take greater than 5 seconds. Windows will put a harddrive to sleep if it’s inactive. I tried to load up a video from an inactive drive. It took greater than 5 seconds for the drive to spool up. My code incorrectly marked the video as defective.