ソースを参照

Added calculateFrameCount() and calculateDuration(); close #22

Dominic Szablewski 11 年 前
コミット
2cf6a81c62
共有2 個のファイルを変更した35 個の追加1 個の削除を含む
  1. 10 1
      README.md
  2. 25 0
      jsmpg.js

+ 10 - 1
README.md ファイルの表示

@@ -28,7 +28,16 @@ player.stop();
28 28
 // An 'onload' callback can be specified in the 'options' argument
29 29
 var mpegLoaded = function( player ) {
30 30
 	console.log('Loaded', player);
31
-}
31
+	
32
+	// calculateFrameCount() and calculateDuration() can only be called
33
+	// after the mpeg has been fully loaded. So this callback is the ideal
34
+	// place to fetch this info
35
+	var frames = player.calculateFrameCount(),
36
+		duration = player.calculateDuration();
37
+		
38
+	console.log('Duration: '+duration+' seconds ('+frames+' frames)');
39
+};
40
+
32 41
 var player = new jsmpeg('file.mpeg', {onload:mpegLoaded});
33 42
 
34 43
 // If you don't use 'autoplay' and don't explicitly call .play(), you can get individual

+ 25 - 0
jsmpg.js ファイルの表示

@@ -361,6 +361,31 @@ jsmpeg.prototype.fillArray = function(a, value) {
361 361
 	}
362 362
 };
363 363
 
364
+jsmpeg.prototype.cachedFrameCount = 0;
365
+jsmpeg.prototype.calculateFrameCount = function() {
366
+	if( !this.buffer || this.cachedFrameCount ) { 
367
+		return this.cachedFrameCount; 
368
+	}
369
+	
370
+	// Remember the buffer position, so we can rewind to the beginning and 
371
+	// reset to the current position afterwards
372
+	var currentPlaybackIndex = this.buffer.index,
373
+		frames = 0;
374
+	
375
+	this.buffer.index = 0;
376
+	while( this.findStartCode(START_PICTURE) !== BitReader.NOT_FOUND ) {
377
+		frames++;
378
+	}
379
+	this.buffer.index = currentPlaybackIndex;
380
+	
381
+	this.cachedFrameCount = frames;
382
+	return frames;
383
+};
384
+
385
+jsmpeg.prototype.calculateDuration = function() {
386
+	return this.calculateFrameCount() * (1/this.pictureRate);
387
+};
388
+
364 389
 
365 390
 
366 391
 // ----------------------------------------------------------------------------