| 1 | package com.longtailvideo.jwplayer.muxing { |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | import com.longtailvideo.jwplayer.utils.Logger; |
|---|
| 5 | import com.longtailvideo.jwplayer.muxing.*; |
|---|
| 6 | import flash.utils.ByteArray; |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | /** Representation of an FLV file. **/ |
|---|
| 10 | public class FLV { |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | /** List of FLV tags. **/ |
|---|
| 14 | private var _tags:Array; |
|---|
| 15 | /** Is this the first video? **/ |
|---|
| 16 | private var _first:Boolean; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | /** Transmux the M2TS file into an FLV file. **/ |
|---|
| 20 | public function FLV(first:Boolean=false) { |
|---|
| 21 | _tags = new Array(); |
|---|
| 22 | _first = first; |
|---|
| 23 | }; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** Return the bytes for the player. **/ |
|---|
| 27 | public function get data():ByteArray { |
|---|
| 28 | var flv:ByteArray = new ByteArray(); |
|---|
| 29 | if(_first) { |
|---|
| 30 | _writeHeader(flv); |
|---|
| 31 | } |
|---|
| 32 | // Sort the tags by timestamp; private data first. |
|---|
| 33 | _tags.sortOn( |
|---|
| 34 | ['timestamp','isPrivate'], |
|---|
| 35 | [Array.NUMERIC, Array.NUMERIC | Array.DESCENDING] |
|---|
| 36 | ); |
|---|
| 37 | //var stt:Number = _tags[0].timestamp; |
|---|
| 38 | // Append all tags |
|---|
| 39 | var pos:Number = flv.position; |
|---|
| 40 | for(var i:Number=0; i<_tags.length; i++) { |
|---|
| 41 | // Set the timestamps zero-indexed. |
|---|
| 42 | //_tags[i].timestamp -= stt; |
|---|
| 43 | // The Tag itself. |
|---|
| 44 | //if(_first || !_tags[i].isPrivate) { |
|---|
| 45 | flv.writeBytes(_tags[i].data); |
|---|
| 46 | flv.writeUnsignedInt(flv.position - pos); |
|---|
| 47 | pos = flv.position; |
|---|
| 48 | //} |
|---|
| 49 | } |
|---|
| 50 | // Add an end-of-sequence tag. |
|---|
| 51 | // _writeEOSTag(flv); |
|---|
| 52 | Logger.log("timestamps from " + _tags[0].timestamp + " to " +_tags[_tags.length-1].timestamp); |
|---|
| 53 | return flv; |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** Push an FLV tag into the FLV file. **/ |
|---|
| 58 | public function push(tag:FLVTag):void { |
|---|
| 59 | _tags.push(tag); |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /** Write end-of-sequence FLV tag. **/ |
|---|
| 64 | private function _writeEOSTag(tag:ByteArray):void { |
|---|
| 65 | // Size of the tag in bytes after StreamID. |
|---|
| 66 | tag.writeUnsignedInt(0x09000005); |
|---|
| 67 | // Timestamp (lower 24 plus upper 8) |
|---|
| 68 | var tsp:Number = _tags[_tags.length-1].timestamp + 1; |
|---|
| 69 | tag.writeByte(tsp >> 16); |
|---|
| 70 | tag.writeByte(tsp >> 8); |
|---|
| 71 | tag.writeByte(tsp); |
|---|
| 72 | tag.writeByte(tsp >> 24); |
|---|
| 73 | // StreamID (3 empty bytes), Keyframe, AVC. |
|---|
| 74 | tag.writeUnsignedInt(0x00000017); |
|---|
| 75 | //Sequence end, composition time |
|---|
| 76 | tag.writeUnsignedInt(0x02000000); |
|---|
| 77 | // PreviousTagSize |
|---|
| 78 | tag.writeUnsignedInt(0x00000016); |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | /** Write the FLV file header. **/ |
|---|
| 83 | private function _writeHeader(flv:ByteArray):void { |
|---|
| 84 | // "F" + "L" + "V". |
|---|
| 85 | flv.writeByte(0x46); |
|---|
| 86 | flv.writeByte(0x4C); |
|---|
| 87 | flv.writeByte(0x56); |
|---|
| 88 | // File version (1) |
|---|
| 89 | flv.writeByte(0x01); |
|---|
| 90 | // Audio + Video tags. |
|---|
| 91 | flv.writeByte(0x05); |
|---|
| 92 | // Length of the header. |
|---|
| 93 | flv.writeUnsignedInt(0x00000009); |
|---|
| 94 | // PreviousTagSize0 |
|---|
| 95 | flv.writeUnsignedInt(0x00000000); |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | } |
|---|