3
3
import com .netflix .imflibrary .IMFErrorLogger ;
4
4
import com .netflix .imflibrary .IMFErrorLoggerImpl ;
5
5
import com .netflix .imflibrary .RESTfulInterfaces .PayloadRecord ;
6
+ import com .netflix .imflibrary .app .IMFTrackFileReader ;
6
7
import com .netflix .imflibrary .app .IMPFixer ;
7
8
import com .netflix .imflibrary .st0429_8 .PackingList ;
8
9
import com .netflix .imflibrary .st0429_9 .AssetMap ;
30
31
import java .math .BigInteger ;
31
32
import java .net .URISyntaxException ;
32
33
import java .nio .file .Files ;
34
+ import java .nio .file .Path ;
35
+ import java .nio .file .Paths ;
33
36
import java .util .*;
34
37
35
38
import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
@@ -39,7 +42,6 @@ public class IMPAssembler {
39
42
private static final Logger logger = LoggerFactory .getLogger (IMPAssembler .class );
40
43
41
44
42
-
43
45
/**
44
46
* Generate the CPL, PKL, and AssetMap XML files given a simple timeline of track entries
45
47
* Code adapted from IMPFixer
@@ -52,6 +54,8 @@ public AssembledIMPResult assembleIMFFromFiles(SimpleTimeline simpleTimeline, Fi
52
54
IMFErrorLogger imfErrors = new IMFErrorLoggerImpl ();
53
55
List <Composition .VirtualTrack > virtualTracks = new ArrayList <>();
54
56
Map <UUID , UUID > trackFileIdToResourceMap = new HashMap <>();
57
+ Map <UUID , List <Long >> sampleRateMap = new HashMap <>();
58
+ Map <UUID , BigInteger > sampleCountMap = new HashMap <>();
55
59
56
60
57
61
for (Track track : simpleTimeline .getTracks ()) {
@@ -86,16 +90,58 @@ public AssembledIMPResult assembleIMFFromFiles(SimpleTimeline simpleTimeline, Fi
86
90
Files .copy (trackEntry .getFile ().toPath (), outputTrackFile .toPath (), REPLACE_EXISTING );
87
91
}
88
92
93
+ IMFTrackFileReader imfTrackFileReader = new IMFTrackFileReader (outputDirectory , resourceByteRangeProvider );
94
+
95
+ // get sample rate or use cached value
96
+ List <Long > sampleRate = null ;
97
+ if (trackEntry .getSampleRate () != null ) {
98
+ // if user provided sample rate, use it
99
+ sampleRate = Arrays .asList (trackEntry .getSampleRate ().getNumerator (), trackEntry .getSampleRate ().getDenominator ());
100
+ logger .info ("Using sample rate from user: {}/{}" , sampleRate .get (0 ), sampleRate .get (1 ));
101
+ } else if (!sampleRateMap .containsKey (trackFileId )) {
102
+ // sample rate has not already been found, find it
103
+ sampleRate = imfTrackFileReader .getEssenceEditRateAsList (imfErrors );
104
+ sampleRateMap .put (trackFileId , sampleRate );
105
+ logger .info ("Found sample rate of: {}/{}" , sampleRate .get (0 ), sampleRate .get (1 ));
106
+ } else {
107
+ sampleRate = sampleRateMap .get (trackFileId );
108
+ logger .info ("Using cached sample rate of: {}/{}" , sampleRate .get (0 ), sampleRate .get (1 ));
109
+ }
110
+
111
+
112
+ // get sample count or use cached value
113
+ BigInteger sampleCount = null ;
114
+ if (trackEntry .getIntrinsicDuration () != null ) {
115
+ // use sample count provided by user
116
+ sampleCount = trackEntry .getIntrinsicDuration ();
117
+ logger .info ("Intrinsic duration from user: {}" , sampleCount );
118
+ } else if (!sampleCountMap .containsKey (trackFileId )) {
119
+ // compute sample count
120
+ sampleCount = imfTrackFileReader .getEssenceDuration (imfErrors );
121
+ sampleCountMap .put (trackFileId , sampleCount );
122
+ logger .info ("Found essence duration of: {}" , sampleCount );
123
+ } else {
124
+ // use cached sample count
125
+ sampleCount = sampleCountMap .get (trackFileId );
126
+ logger .info ("Using cached intrinsic duration of: {}" , sampleCount );
127
+ }
128
+
129
+ // delete temporary file left over from FileByteRangeProvider or ByteArrayByteRangeProvider
130
+ Path tempFilePath = Paths .get (outputDirectory .getAbsolutePath (), "range" );
131
+ logger .info ("Deleting temporary file if it exists: {}" , tempFilePath );
132
+ Files .deleteIfExists (tempFilePath );
133
+
134
+
89
135
// add to resources
90
136
logger .info ("Adding file to resources: {}.." , trackEntry .getFile ().getName ());
91
137
resources .add (
92
138
new IMFTrackFileResourceType (
93
139
UUIDHelper .fromUUID (IMFUUIDGenerator .getInstance ().generateUUID ()),
94
140
UUIDHelper .fromUUID (trackFileId ),
95
- Arrays . asList ( trackEntry . getSampleRate (). getNumerator (), trackEntry . getSampleRate (). getDenominator ()), // defaults to 1/1
96
- trackEntry . getIntrinsicDuration () ,
141
+ sampleRate , // defaults to 1/1
142
+ sampleCount ,
97
143
trackEntry .getEntryPoint (), // defaults to 0 if null
98
- trackEntry .getDuration (), // defaults to intrinsic duration if null
144
+ trackEntry .getDuration () == null ? sampleCount : trackEntry . getDuration () , // defaults to intrinsic duration if null
99
145
trackEntry .getRepeatCount (), // defaults to 1 if null
100
146
UUIDHelper .fromUUID (getOrGenerateSourceEncoding (trackFileIdToResourceMap , trackFileId )), // used as the essence descriptor id
101
147
hash ,
@@ -336,13 +382,13 @@ public static class TrackEntry {
336
382
/**
337
383
* Constructor for a track entry to be used to construct a simple timeline
338
384
* @param file - the MXF file
339
- * @param sampleRate - the sample rate
340
- * @param intrinsicDuration - the intrinsic duration
341
- * @param entryPoint - the entry point ( if null, defaults to 0)
342
- * @param duration - the duration ( if null, defaults to intrinsic duration)
343
- * @param repeatCount - the repeat count ( if null, defaults to 1)
385
+ * @param sampleRate - the sample rate, optional, introspected if null
386
+ * @param intrinsicDuration - the intrinsic duration, optional, introspected if null
387
+ * @param entryPoint - the entry point, if null, defaults to 0
388
+ * @param duration - the duration, if null, defaults to intrinsic duration
389
+ * @param repeatCount - the repeat count, if null, defaults to 1
344
390
*/
345
- public TrackEntry (@ Nonnull File file , @ Nonnull Composition .EditRate sampleRate , @ Nonnull BigInteger intrinsicDuration , @ Nullable BigInteger entryPoint , @ Nullable BigInteger duration , @ Nullable BigInteger repeatCount ) {
391
+ public TrackEntry (@ Nonnull File file , @ Nullable Composition .EditRate sampleRate , @ Nullable BigInteger intrinsicDuration , @ Nullable BigInteger entryPoint , @ Nullable BigInteger duration , @ Nullable BigInteger repeatCount ) {
346
392
this .file = file ;
347
393
this .sampleRate = sampleRate ;
348
394
this .intrinsicDuration = intrinsicDuration ;
0 commit comments