1
1
ISOFile . prototype . add = BoxParser . Box . prototype . add ;
2
+ ISOFile . prototype . addBox = BoxParser . Box . prototype . addBox ;
2
3
3
- ISOFile . prototype . init = function ( ) {
4
- var ftyp = this . add ( "ftyp" ) . set ( "major_brand" , "iso4" )
4
+ ISOFile . prototype . init = function ( _options ) {
5
+ var options = _options || { } ;
6
+ var ftyp = this . add ( "ftyp" ) . set ( "major_brand" , ( options . brands && options . brands [ 0 ] ) || "iso4" )
5
7
. set ( "minor_version" , 0 )
6
- . set ( "compatible_brands" , [ ] ) ;
8
+ . set ( "compatible_brands" , options . brands || [ "iso4" ] ) ;
7
9
var moov = this . add ( "moov" ) ;
8
- moov . add ( "mvhd" ) . set ( "timescale" , 600 )
9
- . set ( "rate" , 1 )
10
+ moov . add ( "mvhd" ) . set ( "timescale" , options . timescale || 600 )
11
+ . set ( "rate" , options . rate || 1 )
10
12
. set ( "creation_time" , 0 )
11
13
. set ( "modification_time" , 0 )
12
- . set ( "duration" , 0 )
14
+ . set ( "duration" , options . duration || 0 )
13
15
. set ( "volume" , 1 )
14
16
. set ( "matrix" , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] )
15
17
. set ( "next_track_id" , 1 ) ;
@@ -19,7 +21,7 @@ ISOFile.prototype.init = function () {
19
21
20
22
ISOFile . prototype . addTrack = function ( _options ) {
21
23
if ( ! this . moov ) {
22
- this . init ( ) ;
24
+ this . init ( _options ) ;
23
25
}
24
26
25
27
var options = _options || { } ;
@@ -30,11 +32,14 @@ ISOFile.prototype.addTrack = function (_options) {
30
32
31
33
var trak = this . moov . add ( "trak" ) ;
32
34
this . moov . mvhd . next_track_id = options . id + 1 ;
33
- trak . add ( "tkhd" ) . set ( "creation_time" , 0 )
35
+ trak . add ( "tkhd" ) . set ( "flags" , BoxParser . TKHD_FLAG_ENABLED |
36
+ BoxParser . TKHD_FLAG_IN_MOVIE |
37
+ BoxParser . TKHD_FLAG_IN_PREVIEW )
38
+ . set ( "creation_time" , 0 )
34
39
. set ( "modification_time" , 0 )
35
40
. set ( "track_id" , options . id )
36
- . set ( "duration" , 0 )
37
- . set ( "layer" , 0 )
41
+ . set ( "duration" , options . duration || 0 )
42
+ . set ( "layer" , options . layer || 0 )
38
43
. set ( "alternate_group" , 0 )
39
44
. set ( "volume" , 1 )
40
45
. set ( "matrix" , [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] )
@@ -45,7 +50,7 @@ ISOFile.prototype.addTrack = function (_options) {
45
50
mdia . add ( "mdhd" ) . set ( "creation_time" , 0 )
46
51
. set ( "modification_time" , 0 )
47
52
. set ( "timescale" , options . timescale || 1 )
48
- . set ( "duration" , 0 )
53
+ . set ( "duration" , options . media_duration || 0 )
49
54
. set ( "language" , options . language || 0 ) ;
50
55
51
56
mdia . add ( "hdlr" ) . set ( "handler" , options . hdlr || "vide" )
@@ -54,7 +59,9 @@ ISOFile.prototype.addTrack = function (_options) {
54
59
mdia . add ( "elng" ) . set ( "extended_language" , options . language || "fr-FR" ) ;
55
60
56
61
var minf = mdia . add ( "minf" ) ;
57
- var sample_entry = new BoxParser [ options . type + "SampleEntry" ] ( ) ;
62
+ if ( BoxParser [ options . type + "SampleEntry" ] === undefined ) return ;
63
+ var sample_description_entry = new BoxParser [ options . type + "SampleEntry" ] ( ) ;
64
+ sample_description_entry . data_reference_index = 1 ;
58
65
var media_type = "" ;
59
66
for ( var i = 0 ; i < BoxParser . sampleEntryCodes . length ; i ++ ) {
60
67
var code = BoxParser . sampleEntryCodes [ i ] ;
@@ -66,30 +73,39 @@ ISOFile.prototype.addTrack = function (_options) {
66
73
switch ( media_type ) {
67
74
case "Visual" :
68
75
minf . add ( "vmhd" ) . set ( "graphicsmode" , 0 ) . set ( "opcolor" , [ 0 , 0 , 0 ] ) ;
69
- sample_entry . set ( "width" , options . width )
76
+ sample_description_entry . set ( "width" , options . width )
70
77
. set ( "height" , options . height )
71
78
. set ( "horizresolution" , 0x48 << 16 )
72
79
. set ( "vertresolution" , 0x48 << 16 )
73
80
. set ( "frame_count" , 1 )
74
81
. set ( "compressorname" , options . type + " Compressor" )
75
82
. set ( "depth" , 0x18 ) ;
76
-
77
- sample_entry . add ( "avcC" ) . set ( "SPS" , [ ] )
78
- . set ( "PPS" , [ ] )
79
- . set ( "configurationVersion" , 1 )
80
- . set ( "AVCProfileIndication" , 0 )
81
- . set ( "profile_compatibility" , 0 )
82
- . set ( "AVCLevelIndication" , 0 )
83
- . set ( "lengthSizeMinusOne" , 0 ) ;
83
+ // sample_description_entry.add("avcC").set("SPS", [])
84
+ // .set("PPS", [])
85
+ // .set("configurationVersion", 1)
86
+ // .set("AVCProfileIndication",0)
87
+ // .set("profile_compatibility", 0)
88
+ // .set("AVCLevelIndication" ,0)
89
+ // .set("lengthSizeMinusOne", 0);
84
90
break ;
85
91
case "Audio" :
86
- minf . add ( "smhd" ) ;
92
+ minf . add ( "smhd" ) . set ( "balance" , options . balance || 0 ) ;
93
+ sample_description_entry . set ( "channel_count" , options . channel_count || 2 )
94
+ . set ( "samplesize" , options . samplesize || 16 )
95
+ . set ( "samplerate" , options . samplerate || 1 << 16 ) ;
87
96
break ;
88
97
case "Hint" :
89
- minf . add ( "hmhd" ) ;
98
+ minf . add ( "hmhd" ) ; // TODO: add properties
90
99
break ;
91
100
case "Subtitle" :
92
101
minf . add ( "sthd" ) ;
102
+ switch ( options . type ) {
103
+ case "stpp" :
104
+ sample_description_entry . set ( "namespace" , options . namespace || "nonamespace" )
105
+ . set ( "schema_location" , options . schema_location || "" )
106
+ . set ( "auxiliary_mime_types" , options . auxiliary_mime_types || "" ) ;
107
+ break ;
108
+ }
93
109
break ;
94
110
case "Metadata" :
95
111
minf . add ( "nmhd" ) ;
@@ -101,9 +117,12 @@ ISOFile.prototype.addTrack = function (_options) {
101
117
minf . add ( "nmhd" ) ;
102
118
break ;
103
119
}
120
+ if ( options . description ) {
121
+ sample_description_entry . addBox ( options . description ) ;
122
+ }
104
123
minf . add ( "dinf" ) . add ( "dref" ) . addEntry ( ( new BoxParser [ "url Box" ] ( ) ) . set ( "flags" , 0x1 ) ) ;
105
124
var stbl = minf . add ( "stbl" ) ;
106
- stbl . add ( "stsd" ) . addEntry ( sample_entry ) ;
125
+ stbl . add ( "stsd" ) . addEntry ( sample_description_entry ) ;
107
126
stbl . add ( "stts" ) . set ( "sample_counts" , [ ] )
108
127
. set ( "sample_deltas" , [ ] ) ;
109
128
stbl . add ( "stsc" ) . set ( "first_chunk" , [ ] )
@@ -118,18 +137,20 @@ ISOFile.prototype.addTrack = function (_options) {
118
137
. set ( "default_sample_size" , options . default_sample_size || 0 )
119
138
. set ( "default_sample_flags" , options . default_sample_flags || 0 ) ;
120
139
this . buildTrakSampleLists ( trak ) ;
121
- return trak ;
140
+ return options . id ;
122
141
}
123
142
124
- BoxParser . Box . prototype . computeSize = function ( ) {
143
+ BoxParser . Box . prototype . computeSize = function ( stream_ ) {
125
144
var stream = stream_ || new DataStream ( ) ;
126
145
stream . endianness = DataStream . BIG_ENDIAN ;
127
146
this . write ( stream ) ;
128
147
}
129
148
130
- ISOFile . prototype . addSample = function ( trak , data , _options ) {
149
+ ISOFile . prototype . addSample = function ( track_id , data , _options ) {
131
150
var options = _options || { } ;
132
151
var sample = { } ;
152
+ var trak = this . getTrackById ( track_id ) ;
153
+ if ( trak === null ) return ;
133
154
sample . number = trak . samples . length ;
134
155
sample . track_id = trak . tkhd . track_id ;
135
156
sample . timescale = trak . mdia . mdhd . timescale ;
@@ -155,13 +176,13 @@ ISOFile.prototype.addSample = function (trak, data, _options) {
155
176
156
177
this . processSamples ( ) ;
157
178
158
- // var moof = ISOFile.createSingleSampleMoof(sample);
159
- // this.moofs.push (moof);
160
- //this.boxes.push(moof );
161
- // moof.computeSize();
162
- ///* adjusting the data_offset now that the moof size is known*/
163
- //moof.trafs[0].truns[0].data_offset = moof.size+8; //8 is mdat header
164
- //this.add("mdat").data = data ;
179
+ var moof = ISOFile . createSingleSampleMoof ( sample ) ;
180
+ this . addBox ( moof ) ;
181
+ moof . computeSize ( ) ;
182
+ /* adjusting the data_offset now that the moof size is known*/
183
+ moof . trafs [ 0 ] . truns [ 0 ] . data_offset = moof . size + 8 ; //8 is mdat header
184
+ this . add ( "mdat" ) . data = data ;
185
+ return sample ;
165
186
}
166
187
167
188
ISOFile . createSingleSampleMoof = function ( sample ) {
0 commit comments