-
Notifications
You must be signed in to change notification settings - Fork 27
/
ConcatenationMultipleInputs.java
454 lines (404 loc) · 21.6 KB
/
ConcatenationMultipleInputs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import com.bitmovin.api.sdk.BitmovinApi;
import com.bitmovin.api.sdk.common.BitmovinException;
import com.bitmovin.api.sdk.model.AacAudioConfiguration;
import com.bitmovin.api.sdk.model.ConcatenationInputConfiguration;
import com.bitmovin.api.sdk.model.TimeBasedTrimmingInputStream;
import com.bitmovin.api.sdk.model.ConcatenationInputStream;
import com.bitmovin.api.sdk.model.IngestInputStream;
import com.bitmovin.api.sdk.model.AclEntry;
import com.bitmovin.api.sdk.model.AclPermission;
import com.bitmovin.api.sdk.model.CodecConfiguration;
import com.bitmovin.api.sdk.model.Encoding;
import com.bitmovin.api.sdk.model.EncodingOutput;
import com.bitmovin.api.sdk.model.H264VideoConfiguration;
import com.bitmovin.api.sdk.model.HttpInput;
import com.bitmovin.api.sdk.model.Input;
import com.bitmovin.api.sdk.model.MessageType;
import com.bitmovin.api.sdk.model.Mp4Muxing;
import com.bitmovin.api.sdk.model.MuxingStream;
import com.bitmovin.api.sdk.model.Output;
import com.bitmovin.api.sdk.model.PresetConfiguration;
import com.bitmovin.api.sdk.model.S3Output;
import com.bitmovin.api.sdk.model.StartEncodingRequest;
import com.bitmovin.api.sdk.model.Status;
import com.bitmovin.api.sdk.model.Stream;
import com.bitmovin.api.sdk.model.StreamInput;
import com.bitmovin.api.sdk.model.StreamSelectionMode;
import com.bitmovin.api.sdk.model.Task;
import common.ConfigProvider;
import feign.Logger.Level;
import feign.slf4j.Slf4jLogger;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This example demonstrates how to use concatenation and trimming to combine multiple input files into a single output.
* This script is the full version of the script documented in the tutorial on concatenation and trimming
* https://bitmovin.com/docs/encoding/tutorials/stitching-and-trimming-part-1-the-basics
*
* <p>The following configuration parameters are expected:
*
* <ul>
* <li>BITMOVIN_API_KEY - Your API key for the Bitmovin API
* <li>BITMOVIN_TENANT_ORG_ID - (optional) The ID of the Organisation in which you want to perform
* the encoding.
* <li>HTTP_INPUT_HOST - The Hostname or IP address of the HTTP server hosting your input files,
* e.g.: my-storage.biz
* <li>HTTP_INPUT_FILE_PATH - The path to your input file on the provided HTTP server
* Example: videos/1080p_Sintel.mp4
* <li>HTTP_INPUT_BUMPER_FILE_PATH - The path to your input file on the provided HTTP server to be concatenated before HTTP_INPUT_FILE_PATH
* Example: videos/bumper.mp4
* <li>HTTP_INPUT_PROMO_FILE_PATH - The path to your input file on the provided HTTP server to be concatenated after HTTP_INPUT_FILE_PATH
* Example: videos/promo.mp4
* <li>S3_OUTPUT_BUCKET_NAME - The name of your S3 output bucket. Example: my-bucket-name
* <li>S3_OUTPUT_ACCESS_KEY - The access key of your S3 output bucket
* <li>S3_OUTPUT_SECRET_KEY - The secret key of your S3 output bucket
* <li>S3_OUTPUT_BASE_PATH - The base path on your S3 output bucket where content will be written.
* Example: /outputs
* </ul>
*
* <p>Configuration parameters will be retrieved from these sources in the listed order:
*
* <ol>
* <li>command line arguments (eg BITMOVIN_API_KEY=xyz)
* <li>properties file located in the root folder of the JAVA examples at ./examples.properties
* (see examples.properties.template as reference)
* <li>environment variables
* <li>properties file located in the home folder at ~/.bitmovin/examples.properties (see
* examples.properties.template as reference)
* </ol>
*/
public class ConcatenationMultipleInputs {
private static final Logger logger = LoggerFactory.getLogger(ConcatenationMultipleInputs.class);
private static BitmovinApi bitmovinApi;
private static ConfigProvider configProvider;
public static void main(String[] args) throws Exception {
configProvider = new ConfigProvider(args);
bitmovinApi =
BitmovinApi.builder()
.withApiKey(configProvider.getBitmovinApiKey())
// uncomment the following line if you are working with a multi-tenant account
// .withTenantOrgId(configProvider.getBitmovinTenantOrgId())
.withLogger(
new Slf4jLogger(), Level.BASIC) // set the logger and log level for the API client
.build();
Encoding encoding =
createEncoding(
"ConcatenationMultipleInputs", "Encoding with a concatenation in MP4 muxing");
HttpInput input = createHttpInput(configProvider.getHttpInputHost());
String mainFilePath = configProvider.getHttpInputFilePath();
String bumperFilePath = configProvider.getHttpInputBumperFilePath();
String promoFilePath = configProvider.getHttpInputPromoFilePath();
Output output = createS3Output(
configProvider.getS3OutputBucketName(),
configProvider.getS3OutputAccessKey(),
configProvider.getS3OutputSecretKey()
);
IngestInputStream main = createIngestInputStream(encoding, input, mainFilePath);
IngestInputStream bumper = createIngestInputStream(encoding, input, bumperFilePath);
IngestInputStream promo = createIngestInputStream(encoding, input, promoFilePath);
TimeBasedTrimmingInputStream mainPart1 = createTimeBasedTrimmingInputStream(encoding, main, 10.0, 90.0);
TimeBasedTrimmingInputStream mainPart2= createTimeBasedTrimmingInputStream(encoding, main, 109.0, 60.0);
ConcatenationInputConfiguration bumperConfig = new ConcatenationInputConfiguration();
bumperConfig.setInputStreamId(bumper.getId());
bumperConfig.setIsMain(false);
bumperConfig.setPosition(0);
ConcatenationInputConfiguration part1Config = new ConcatenationInputConfiguration();
part1Config.setInputStreamId(mainPart1.getId());
part1Config.setIsMain(true);
part1Config.setPosition(1);
ConcatenationInputConfiguration promo1Config = new ConcatenationInputConfiguration();
promo1Config.setInputStreamId(promo.getId());
promo1Config.setIsMain(false);
promo1Config.setPosition(2);
ConcatenationInputConfiguration part2Config = new ConcatenationInputConfiguration();
part2Config.setInputStreamId(mainPart2.getId());
part2Config.setIsMain(false);
part2Config.setPosition(3);
ConcatenationInputConfiguration promo2Config = new ConcatenationInputConfiguration();
promo2Config.setInputStreamId(promo.getId());
promo2Config.setIsMain(false);
promo2Config.setPosition(4);
ConcatenationInputStream allTogether = createConcatentationInputStream(encoding, Arrays.asList(
bumperConfig,
part1Config,
promo1Config,
part2Config,
promo2Config
));
AacAudioConfiguration aacAudioConfiguration = createAacAudioConfig();
Stream aacAudioStream = createStreamWithConcatenationInputStreams(encoding, allTogether, aacAudioConfiguration);
H264VideoConfiguration videoConfiguration = createH264VideoConfig(1080, 4800000);
Stream videoStream = createStreamWithConcatenationInputStreams(encoding, allTogether, videoConfiguration);
createMp4Muxing(encoding, output, "multiple-inputs-concatenation-mp4", Arrays.asList(videoStream, aacAudioStream), "video.mp4");
executeEncoding(encoding);
}
/**
* Creates an Encoding object. This is the base object to configure your encoding.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodings
*
* @param name A name that will help you identify the encoding, e.g. in the Bitmovin dashboard
* @param description An optional description providing more detailed information about the
* encoding
*/
private static Encoding createEncoding(String name, String description) throws BitmovinException {
Encoding encoding = new Encoding();
encoding.setName(name);
encoding.setDescription(description);
return bitmovinApi.encoding.encodings.create(encoding);
}
/**
* Creates a resource representing an HTTP server providing the input files. For alternative input
* methods see <a
* href="https://bitmovin.com/docs/encoding/articles/supported-input-output-storages">list of
* supported input and output storages</a>
*
* <p>For reasons of simplicity, a new input resource is created on each execution of this
* example. In production use, this method should be replaced by a <a
* href="https://bitmovin.com/docs/encoding/api-reference/sections/inputs#/Encoding/GetEncodingInputsHttpByInputId">get
* call</a> to retrieve an existing resource.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/inputs#/Encoding/PostEncodingInputsHttp
*
* @param host The hostname or IP address of the HTTP server e.g.: my-storage.biz
*/
private static HttpInput createHttpInput(String host) throws BitmovinException {
HttpInput input = new HttpInput();
input.setHost(host);
return bitmovinApi.encoding.inputs.http.create(input);
}
/**
* Creates a resource representing an AWS S3 cloud storage bucket to which generated content will
* be transferred. For alternative output methods see <a
* href="https://bitmovin.com/docs/encoding/articles/supported-input-output-storages">list of
* supported input and output storages</a>
*
* <p>The provided credentials need to allow <i>read</i>, <i>write</i> and <i>list</i> operations.
* <i>delete</i> should also be granted to allow overwriting of existings files. See <a
* href="https://bitmovin.com/docs/encoding/faqs/how-do-i-create-a-aws-s3-bucket-which-can-be-used-as-output-location">creating
* an S3 bucket and setting permissions</a> for further information
*
* <p>For reasons of simplicity, a new output resource is created on each execution of this
* example. In production use, this method should be replaced by a <a
* href="https://bitmovin.com/docs/encoding/api-reference/sections/outputs#/Encoding/GetEncodingOutputsS3">get
* call</a> retrieving an existing resource.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/outputs#/Encoding/PostEncodingOutputsS3
*
* @param bucketName The name of the S3 bucket
* @param accessKey The access key of your S3 account
* @param secretKey The secret key of your S3 account
*/
private static S3Output createS3Output(String bucketName, String accessKey, String secretKey)
throws BitmovinException {
S3Output s3Output = new S3Output();
s3Output.setBucketName(bucketName);
s3Output.setAccessKey(accessKey);
s3Output.setSecretKey(secretKey);
return bitmovinApi.encoding.outputs.s3.create(s3Output);
}
/**
* Creates an IngestInputStream and adds it to an encoding.
* The IngestInputStream is used to define where a file to read a stream from is located.
*
* <p>API endpoints:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsInputStreamsIngestByEncodingId
*
* @param encoding The encoding to be started
* @param input The input resource providing the input file
* @param inputPath The path to the input file
*/
private static IngestInputStream createIngestInputStream(Encoding encoding, Input input, String inputPath) {
IngestInputStream ingestInputStream = new IngestInputStream();
ingestInputStream.setInputId(input.getId());
ingestInputStream.setInputPath(inputPath);
ingestInputStream.setSelectionMode(StreamSelectionMode.AUTO);
return bitmovinApi.encoding.encodings.inputStreams.ingest.create(encoding.getId(), ingestInputStream);
}
/**
* Creates a TimeBasedTrimmingInputStream and adds it to an encoding.
* The TimeBasedTrimmingInputStream is used to define a section of an IngestInputStream using an offset and a duration expressed in seconds
*
* <p>API endpoints:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsInputStreamsTrimmingTimeBasedByEncodingId
*
* @param encoding The encoding to be started
* @param ingestInputStream The IngestInputStream instance created from the input file
* @param offset Defines the offset in seconds at which the encoding should start, beginning at 0.
* @param duration Defines how many seconds of the input will be encoded.
*/
private static TimeBasedTrimmingInputStream createTimeBasedTrimmingInputStream(Encoding encoding, IngestInputStream ingestInputStream, double offset, double duration) {
TimeBasedTrimmingInputStream timeBasedTrimmingInputStream = new TimeBasedTrimmingInputStream();
timeBasedTrimmingInputStream.setInputStreamId(ingestInputStream.getId());
timeBasedTrimmingInputStream.setOffset(offset);
timeBasedTrimmingInputStream.setDuration(duration);
return bitmovinApi.encoding.encodings.inputStreams.trimming.timeBased.create(encoding.getId(), timeBasedTrimmingInputStream);
}
/**
* Creates a ConcatenationInputStream and adds it to an encoding.
* The ConcatenationInputStream is used to define a concatenated stream from multiple input files
*
* <p>API endpoints:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsInputStreamsConcatenationByEncodingId
*
* @param encoding The encoding to be started
* @param concatenationInputs List of ConcatenationInputConfiguration which include each concatenation configuration
*/
private static ConcatenationInputStream createConcatentationInputStream(Encoding encoding, List<ConcatenationInputConfiguration> concatenationInputs) {
ConcatenationInputStream concatenationInputStream = new ConcatenationInputStream();
concatenationInputStream.setConcatenation(concatenationInputs);
return bitmovinApi.encoding.encodings.inputStreams.concatenation.create(encoding.getId(), concatenationInputStream);
}
/**
* Adds an audio mix input stream to an encoding
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsStreamsByEncodingId
*
* @param encoding The encoding to which the stream will be added
* @param concatenationInputStream The input resource providing the input file
* @param codecConfiguration The codec configuration to be applied to the stream
*/
private static Stream createStreamWithConcatenationInputStreams(Encoding encoding, ConcatenationInputStream concatenationInputStream, CodecConfiguration codecConfiguration) {
StreamInput streamInput = new StreamInput();
streamInput.setInputStreamId(concatenationInputStream.getId());
Stream stream = new Stream();
stream.setInputStreams(Arrays.asList(streamInput));
stream.setCodecConfigId(codecConfiguration.getId());
return bitmovinApi.encoding.encodings.streams.create(encoding.getId(), stream);
}
/**
* Creates a configuration for the H.264 video codec to be applied to video streams.
*
* <p>The output resolution is defined by setting the height to 1080 pixels. Width will be
* determined automatically to maintain the aspect ratio of your input video.
*
* <p>To keep things simple, we use a quality-optimized VoD preset configuration, which will apply
* proven settings for the codec. See <a
* href="https://bitmovin.com/docs/encoding/tutorials/how-to-optimize-your-h264-codec-configuration-for-different-use-cases">How
* to optimize your H264 codec configuration for different use-cases</a> for alternative presets.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsVideoH264
*
* @param height The height of the output video
* @param bitrate The target bitrate of the output video
*/
private static H264VideoConfiguration createH264VideoConfig(int height, long bitrate) throws BitmovinException {
H264VideoConfiguration config = new H264VideoConfiguration();
config.setName(String.format("H.264 %sp", height));
config.setPresetConfiguration(PresetConfiguration.VOD_STANDARD);
config.setHeight(height);
config.setBitrate(bitrate);
return bitmovinApi.encoding.configurations.video.h264.create(config);
}
/**
* Creates a configuration for the AAC audio codec to be applied to audio streams.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsAudioAac
*/
private static AacAudioConfiguration createAacAudioConfig() throws BitmovinException {
AacAudioConfiguration config = new AacAudioConfiguration();
config.setName("AAC 128 kbit/s");
config.setBitrate(128_000L);
return bitmovinApi.encoding.configurations.audio.aac.create(config);
}
/**
* Creates an MP4 muxing.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsMuxingsMp4ByEncodingId
*
* @param encoding The encoding to add the MP4 muxing to
* @param output The output that should be used for the muxing to write the segments to
* @param outputPath The output path where the fragments will be written to
* @param streams A list of streams to be added to the muxing
* @param fileName The name of the file that will be written to the output
*/
private static Mp4Muxing createMp4Muxing(
Encoding encoding, Output output, String outputPath, List<Stream> streams, String fileName)
throws BitmovinException {
Mp4Muxing muxing = new Mp4Muxing();
muxing.addOutputsItem(buildEncodingOutput(output, outputPath));
muxing.setFilename(fileName);
for (Stream stream : streams) {
MuxingStream muxingStream = new MuxingStream();
muxingStream.setStreamId(stream.getId());
muxing.addStreamsItem(muxingStream);
}
return bitmovinApi.encoding.encodings.muxings.mp4.create(encoding.getId(), muxing);
}
/**
* Builds an EncodingOutput object which defines where the output content (e.g. of a muxing) will
* be written to. Public read permissions will be set for the files written, so they can be
* accessed easily via HTTP.
*
* @param output The output resource to be used by the EncodingOutput
* @param outputPath The path where the content will be written to
*/
private static EncodingOutput buildEncodingOutput(Output output, String outputPath) {
AclEntry aclEntry = new AclEntry();
aclEntry.setPermission(AclPermission.PUBLIC_READ);
EncodingOutput encodingOutput = new EncodingOutput();
encodingOutput.setOutputPath(buildAbsolutePath(outputPath));
encodingOutput.setOutputId(output.getId());
encodingOutput.addAclItem(aclEntry);
return encodingOutput;
}
/**
* Builds an absolute path by concatenating the S3_OUTPUT_BASE_PATH configuration parameter, the
* name of this example class and the given relative path
*
* <p>e.g.: /s3/base/path/ClassName/relative/path
*
* @param relativePath The relative path that is concatenated
* @return The absolute path
*/
public static String buildAbsolutePath(String relativePath) {
String className = ConcatenationMultipleInputs.class.getSimpleName();
return Paths.get(configProvider.getS3OutputBasePath(), className, relativePath).toString();
}
/**
* Starts the actual encoding process and periodically polls its status until it reaches a final
* state
*
* <p>API endpoints:
* https://bitmovin.com/docs/encoding/api-reference/all#/Encoding/PostEncodingEncodingsStartByEncodingId
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/GetEncodingEncodingsStatusByEncodingId
*
* <p>Please note that you can also use our webhooks API instead of polling the status. For more
* information consult the API spec:
* https://bitmovin.com/docs/encoding/api-reference/sections/notifications-webhooks
*
* @param encoding The encoding to be started
*/
private static void executeEncoding(Encoding encoding)
throws InterruptedException, BitmovinException {
bitmovinApi.encoding.encodings.start(encoding.getId(), new StartEncodingRequest());
Task task;
do {
Thread.sleep(5000);
task = bitmovinApi.encoding.encodings.status(encoding.getId());
logger.info("Encoding status is {} (progress: {} %)", task.getStatus(), task.getProgress());
} while (task.getStatus() != Status.FINISHED
&& task.getStatus() != Status.ERROR
&& task.getStatus() != Status.CANCELED);
if (task.getStatus() == Status.ERROR) {
logTaskErrors(task);
throw new RuntimeException("Encoding failed");
}
logger.info("Encoding finished successfully");
}
private static void logTaskErrors(Task task) {
task.getMessages().stream()
.filter(msg -> msg.getType() == MessageType.ERROR)
.forEach(msg -> logger.error(msg.getText()));
}
}