/
MultiCodecEncoding.java
1232 lines (1088 loc) · 50.1 KB
/
MultiCodecEncoding.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import com.bitmovin.api.sdk.BitmovinApi;
import com.bitmovin.api.sdk.model.AacAudioConfiguration;
import com.bitmovin.api.sdk.model.AclEntry;
import com.bitmovin.api.sdk.model.AclPermission;
import com.bitmovin.api.sdk.model.AudioAdaptationSet;
import com.bitmovin.api.sdk.model.AudioMediaInfo;
import com.bitmovin.api.sdk.model.CmafMuxing;
import com.bitmovin.api.sdk.model.CodecConfiguration;
import com.bitmovin.api.sdk.model.DashCmafRepresentation;
import com.bitmovin.api.sdk.model.DashFmp4Representation;
import com.bitmovin.api.sdk.model.DashManifest;
import com.bitmovin.api.sdk.model.DashProfile;
import com.bitmovin.api.sdk.model.DashRepresentationType;
import com.bitmovin.api.sdk.model.DashWebmRepresentation;
import com.bitmovin.api.sdk.model.DolbyDigitalAudioConfiguration;
import com.bitmovin.api.sdk.model.DolbyDigitalChannelLayout;
import com.bitmovin.api.sdk.model.Encoding;
import com.bitmovin.api.sdk.model.EncodingOutput;
import com.bitmovin.api.sdk.model.Fmp4Muxing;
import com.bitmovin.api.sdk.model.H264VideoConfiguration;
import com.bitmovin.api.sdk.model.H265VideoConfiguration;
import com.bitmovin.api.sdk.model.HlsManifest;
import com.bitmovin.api.sdk.model.HttpInput;
import com.bitmovin.api.sdk.model.MessageType;
import com.bitmovin.api.sdk.model.Muxing;
import com.bitmovin.api.sdk.model.MuxingStream;
import com.bitmovin.api.sdk.model.Output;
import com.bitmovin.api.sdk.model.Period;
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.StreamInfo;
import com.bitmovin.api.sdk.model.StreamInput;
import com.bitmovin.api.sdk.model.StreamSelectionMode;
import com.bitmovin.api.sdk.model.Task;
import com.bitmovin.api.sdk.model.TsMuxing;
import com.bitmovin.api.sdk.model.VideoAdaptationSet;
import com.bitmovin.api.sdk.model.VorbisAudioConfiguration;
import com.bitmovin.api.sdk.model.Vp9VideoConfiguration;
import com.bitmovin.api.sdk.model.WebmMuxing;
import common.ConfigProvider;
import feign.Logger.Level;
import feign.slf4j.Slf4jLogger;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This example showcases how to run a multi-codec workflow with the Bitmovin API following the best
* practices. It is currently recommended to run one encoding job per codec to achieve optimal
* performance and execution stability. After the encodings have been performed, renditions from
* multiple encodings can be muxed together to build the desired manifest.
*
* <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>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 MultiCodecEncoding {
private static final Logger logger = LoggerFactory.getLogger(MultiCodecEncoding.class);
private static final String DATE_STRING =
new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(new Date());
private static final String HLS_AUDIO_GROUP_AAC_FMP4 = "audio-aac-fmp4";
private static final String HLS_AUDIO_GROUP_AAC_TS = "audio-aac-ts";
private static final String HLS_AUDIO_GROUP_DOLBY_DIGITAL_FMP4 = "audio-dolby-digital-fmp4";
private static BitmovinApi bitmovinApi;
private static ConfigProvider configProvider;
// Helper classes for manifest generation
private static class Rendition {
public int height;
public Long bitrate;
public Rendition(int height, Long bitrate) {
this.height = height;
this.bitrate = bitrate;
}
}
private static class H264AndAacEncodingTracking {
final Encoding encoding;
List<Rendition> renditions;
Map<Rendition, Stream> h264VideoStreams;
Map<Rendition, CmafMuxing> h264CmafMuxings;
Map<Rendition, TsMuxing> h264TsMuxings;
Stream aacAudioStream;
Fmp4Muxing aacFmp4Muxing;
TsMuxing aacTsMuxing;
static final String H264_TS_SEGMENTS_PATH_FORMAT = "video/h264/ts/%dp_%d";
static final String H264_CMAF_SEGMENTS_PATH_FORMAT = "video/h264/cmaf/%dp_%d";
static final String AAC_FMP4_SEGMENTS_PATH = "audio/aac/fmp4";
static final String AAC_TS_SEGMENTS_PATH = "audio/aac/ts";
public H264AndAacEncodingTracking(Encoding encoding) {
this.encoding = encoding;
h264VideoStreams = new HashMap<>();
h264CmafMuxings = new HashMap<>();
h264TsMuxings = new HashMap<>();
renditions =
Arrays.asList(
new Rendition(234, 145_000L),
new Rendition(360, 365_000L),
new Rendition(432, 730_000L),
new Rendition(540, 2_000_000L),
new Rendition(720, 3_000_000L));
}
}
private static class H265AndDolbyDigitalEncodingTracking {
final Encoding encoding;
List<Rendition> renditions;
Map<Rendition, Stream> h265VideoStreams;
Map<Rendition, Fmp4Muxing> h265Fmp4Muxings;
Stream dolbyDigitalAudioStream;
Fmp4Muxing dolbyDigitalFmp4Muxing;
static final String H265_FMP4_SEGMENTS_PATH_FORMAT = "video/h265/fmp4/%dp_%d";
static final String DOLBY_DIGITAL_FMP4_SEGMENTS_PATH = "audio/dolby-digital/fmp4";
public H265AndDolbyDigitalEncodingTracking(Encoding encoding) {
this.encoding = encoding;
h265VideoStreams = new HashMap<>();
h265Fmp4Muxings = new HashMap<>();
renditions =
Arrays.asList(
new Rendition(540, 600_000L),
new Rendition(720, 2_400_000L),
new Rendition(1080, 4_500_000L),
new Rendition(2160, 11_600_000L));
}
}
private static class Vp9AndVorbisEncodingTracking {
final Encoding encoding;
List<Rendition> renditions;
Map<Rendition, WebmMuxing> vp9WebmMuxing;
WebmMuxing vorbisWebmMuxing;
static final String VP9_WEBM_SEGMENTS_PATH_FORMAT = "video/vp9/webm/%dp_%d";
static final String VORBIS_WEBM_SEGMENTS_PATH = "audio/vorbis/webm";
public Vp9AndVorbisEncodingTracking(Encoding encoding) {
this.encoding = encoding;
vp9WebmMuxing = new HashMap<>();
renditions =
Arrays.asList(
new Rendition(540, 600_000L),
new Rendition(720, 2_400_000L),
new Rendition(1080, 4_500_000L),
new Rendition(2160, 11_600_000L));
}
}
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.FULL) // set the logger and log level for the API client
.build();
HttpInput input = createHttpInput(configProvider.getHttpInputHost());
Output output =
createS3Output(
configProvider.getS3OutputBucketName(),
configProvider.getS3OutputAccessKey(),
configProvider.getS3OutputSecretKey());
String inputFilePath = configProvider.getHttpInputFilePath();
H264AndAacEncodingTracking h264AndAacEncodingTracking =
createH264AndAacEncoding(input, inputFilePath, output);
H265AndDolbyDigitalEncodingTracking h265AndDolbyDigitalEncodingTracking =
createH265AndDolbyDigitalEncoding(input, inputFilePath, output);
Vp9AndVorbisEncodingTracking vp9AndVorbisEncodingTracking =
createVp9AndVorbisEncoding(input, inputFilePath, output);
ExecutorService executor = Executors.newFixedThreadPool(3);
List<Callable<Encoding>> encodingTasks =
Arrays.asList(
() -> executeEncoding(h264AndAacEncodingTracking.encoding),
() -> executeEncoding(h265AndDolbyDigitalEncodingTracking.encoding),
() -> executeEncoding(vp9AndVorbisEncodingTracking.encoding));
executor.invokeAll(encodingTasks);
executor.shutdown();
DashManifest dashManifest =
createDashManifest(
output,
h264AndAacEncodingTracking,
h265AndDolbyDigitalEncodingTracking,
vp9AndVorbisEncodingTracking);
executeDashManifest(dashManifest);
HlsManifest hlsManifest =
createHlsManifest(output, h264AndAacEncodingTracking, h265AndDolbyDigitalEncodingTracking);
executeHlsManifest(hlsManifest);
}
/**
* Creates the encoding with H264 codec/Ts muxing, H264 codec/CMAF muxing, AAC codec/fMP4 muxing
*
* @param input the input that should be used
* @param inputFilePath the path to the input file
* @param output the output that should be used
* @return the tracking information for the encoding
*/
private static H264AndAacEncodingTracking createH264AndAacEncoding(
HttpInput input, String inputFilePath, Output output) {
Encoding encoding =
createEncoding(
"H.264 Encoding",
"H.264 -> TS muxing, H.264 -> CMAF muxing, AAC -> fMP4 muxing, AAC -> TS muxing");
H264AndAacEncodingTracking encodingTracking = new H264AndAacEncodingTracking(encoding);
for (Rendition rendition : encodingTracking.renditions) {
H264VideoConfiguration videoConfiguration =
createH264VideoConfig(rendition.height, rendition.bitrate);
Stream videoStream = createStream(encoding, input, inputFilePath, videoConfiguration);
String cmafMuxingOutputPath =
String.format(
H264AndAacEncodingTracking.H264_CMAF_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate);
String tsMuxingOutputPath =
String.format(
H264AndAacEncodingTracking.H264_TS_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate);
CmafMuxing cmafMuxing = createCmafMuxing(encoding, output, cmafMuxingOutputPath, videoStream);
TsMuxing tsMuxing = createTsMuxing(encoding, output, tsMuxingOutputPath, videoStream);
encodingTracking.h264VideoStreams.put(rendition, videoStream);
encodingTracking.h264CmafMuxings.put(rendition, cmafMuxing);
encodingTracking.h264TsMuxings.put(rendition, tsMuxing);
}
// Add an AAC audio stream to the encoding
AacAudioConfiguration aacConfig = createAacAudioConfig();
Stream aacAudioStream = createStream(encoding, input, inputFilePath, aacConfig);
encodingTracking.aacAudioStream = aacAudioStream;
// Create a fMP4 muxing and a TS muxing with the AAC stream
encodingTracking.aacFmp4Muxing =
createFmp4Muxing(
encoding, output, H264AndAacEncodingTracking.AAC_FMP4_SEGMENTS_PATH, aacAudioStream);
encodingTracking.aacTsMuxing =
createTsMuxing(
encoding, output, H264AndAacEncodingTracking.AAC_TS_SEGMENTS_PATH, aacAudioStream);
return encodingTracking;
}
/**
* Creates the encoding with H265 codec/fMP4 muxing, Dolby Digital codec/fMP4 muxing
*
* @param input the input that should be used
* @param inputFilePath the path to the input file
* @param output the output that should be used
* @return the tracking information for the encoding
*/
private static H265AndDolbyDigitalEncodingTracking createH265AndDolbyDigitalEncoding(
HttpInput input, String inputFilePath, Output output) {
Encoding encoding =
createEncoding("H.265 Encoding", "H.265 -> fMP4 muxing, Dolby Digital -> fMP4 muxing");
H265AndDolbyDigitalEncodingTracking encodingTracking =
new H265AndDolbyDigitalEncodingTracking(encoding);
// Add streams and muxings for h265 encoding
for (Rendition rendition : encodingTracking.renditions) {
H265VideoConfiguration videoConfiguration =
createH265VideoConfig(rendition.height, rendition.bitrate);
Stream videoStream = createStream(encoding, input, inputFilePath, videoConfiguration);
Fmp4Muxing fmp4Muxing =
createFmp4Muxing(
encoding,
output,
String.format(
H265AndDolbyDigitalEncodingTracking.H265_FMP4_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate),
videoStream);
encodingTracking.h265VideoStreams.put(rendition, videoStream);
encodingTracking.h265Fmp4Muxings.put(rendition, fmp4Muxing);
}
DolbyDigitalAudioConfiguration dolbyDigitalConfig = createDolbyDigitalAudioConfig();
encodingTracking.dolbyDigitalAudioStream =
createStream(encoding, input, inputFilePath, dolbyDigitalConfig);
// Create a fMP4 muxing with the Dolby Digital stream
encodingTracking.dolbyDigitalFmp4Muxing =
createFmp4Muxing(
encoding,
output,
H265AndDolbyDigitalEncodingTracking.DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
encodingTracking.dolbyDigitalAudioStream);
return encodingTracking;
}
/**
* Created the encoding with VP9 codec/WebM muxing, Vorbis codec / WebM muxing
*
* @param input the input that should be used
* @param inputFilePath the path to the input file
* @param output the output that should be used
* @return the tracking information for the encoding
*/
private static Vp9AndVorbisEncodingTracking createVp9AndVorbisEncoding(
HttpInput input, String inputFilePath, Output output) {
Encoding encoding =
createEncoding("VP9/Vorbis Encoding", "VP9 -> WebM muxing, Vorbis -> WebM muxing");
Vp9AndVorbisEncodingTracking encodingTracking = new Vp9AndVorbisEncodingTracking(encoding);
// Create video streams and add webm muxings to the VP9 encoding
for (Rendition rendition : encodingTracking.renditions) {
Vp9VideoConfiguration vp9Config =
createVp9VideoConfiguration(rendition.height, rendition.bitrate);
Stream vp9VideoStream = createStream(encoding, input, inputFilePath, vp9Config);
encodingTracking.vp9WebmMuxing.put(
rendition,
createWebmMuxing(
encoding,
output,
String.format(
Vp9AndVorbisEncodingTracking.VP9_WEBM_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate),
vp9VideoStream));
}
// Create Vorbis audio configuration
VorbisAudioConfiguration vorbisAudioConfiguration = createVorbisAudioConfiguration();
Stream vorbisAudioStream =
createStream(encoding, input, inputFilePath, vorbisAudioConfiguration);
// Create a WebM muxing with the Vorbis audio stream
encodingTracking.vorbisWebmMuxing =
createWebmMuxing(
encoding,
output,
Vp9AndVorbisEncodingTracking.VORBIS_WEBM_SEGMENTS_PATH,
vorbisAudioStream);
return encodingTracking;
}
/**
* Creates the DASH manifest with all the representations.
*
* @param output the output that should be used
* @param h264AndAacEncodingTracking the tracking information for the H264/AAC encoding
* @param h265AndDolbyDigitalEncodingTracking the tracking information for the H265 encoding
* @param vp9AndVorbisEncodingTracking the tracking information for the VP9/Vorbis encoding
* @return the created DASH manifest
*/
private static DashManifest createDashManifest(
Output output,
H264AndAacEncodingTracking h264AndAacEncodingTracking,
H265AndDolbyDigitalEncodingTracking h265AndDolbyDigitalEncodingTracking,
Vp9AndVorbisEncodingTracking vp9AndVorbisEncodingTracking) {
DashManifest dashManifest = createDashManifest("stream.mpd", DashProfile.LIVE, output, "/");
final Period period =
bitmovinApi.encoding.manifests.dash.periods.create(dashManifest.getId(), new Period());
final VideoAdaptationSet videoAdaptationSetVp9 =
bitmovinApi.encoding.manifests.dash.periods.adaptationsets.video.create(
dashManifest.getId(), period.getId(), new VideoAdaptationSet());
final VideoAdaptationSet videoAdaptationSetH265 =
bitmovinApi.encoding.manifests.dash.periods.adaptationsets.video.create(
dashManifest.getId(), period.getId(), new VideoAdaptationSet());
final VideoAdaptationSet videoAdaptationSetH264 =
bitmovinApi.encoding.manifests.dash.periods.adaptationsets.video.create(
dashManifest.getId(), period.getId(), new VideoAdaptationSet());
AudioAdaptationSet vorbisAudioAdaptationSet =
createAudioAdaptionSet(dashManifest, period, "en");
AudioAdaptationSet ddAudioAdaptationSet = createAudioAdaptionSet(dashManifest, period, "en");
AudioAdaptationSet aacAudioAdaptationSet = createAudioAdaptionSet(dashManifest, period, "en");
// Add representations to VP9 adaptation set
// Add VP9 WEBM muxing to VP9 adaptation set
for (Rendition rendition : vp9AndVorbisEncodingTracking.vp9WebmMuxing.keySet()) {
createDashWebmRepresentation(
vp9AndVorbisEncodingTracking.encoding,
vp9AndVorbisEncodingTracking.vp9WebmMuxing.get(rendition),
dashManifest,
period,
String.format(
Vp9AndVorbisEncodingTracking.VP9_WEBM_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate),
videoAdaptationSetVp9.getId());
}
// Add VORBIS WEBM muxing to VORBIS audio adaptation set
createDashWebmRepresentation(
vp9AndVorbisEncodingTracking.encoding,
vp9AndVorbisEncodingTracking.vorbisWebmMuxing,
dashManifest,
period,
Vp9AndVorbisEncodingTracking.VORBIS_WEBM_SEGMENTS_PATH,
vorbisAudioAdaptationSet.getId());
// Add representations to H265 adaptation set
// Add H265 FMP4 muxing to H265 video adaptation set
for (Rendition rendition : h265AndDolbyDigitalEncodingTracking.h265Fmp4Muxings.keySet()) {
createDashFmp4Representation(
h265AndDolbyDigitalEncodingTracking.encoding,
h265AndDolbyDigitalEncodingTracking.h265Fmp4Muxings.get(rendition),
dashManifest,
period,
String.format(
H265AndDolbyDigitalEncodingTracking.H265_FMP4_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate),
videoAdaptationSetH265.getId());
}
// Add Dolby Digital FMP4 muxing to Dolby Digital audio adaptation set
createDashFmp4Representation(
h265AndDolbyDigitalEncodingTracking.encoding,
h265AndDolbyDigitalEncodingTracking.dolbyDigitalFmp4Muxing,
dashManifest,
period,
H265AndDolbyDigitalEncodingTracking.DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
ddAudioAdaptationSet.getId());
// Add representations to H264 adaptation set
// Add H264 CMAF muxing to H264 video adaptation set
for (Rendition rendition : h264AndAacEncodingTracking.h264CmafMuxings.keySet()) {
createDashCmafRepresentation(
h264AndAacEncodingTracking.encoding,
h264AndAacEncodingTracking.h264CmafMuxings.get(rendition),
dashManifest,
period,
String.format(
H264AndAacEncodingTracking.H264_CMAF_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate),
videoAdaptationSetH264.getId());
}
// Add AAC FMP4 muxing to AAC audio adaptation set
createDashFmp4Representation(
h264AndAacEncodingTracking.encoding,
h264AndAacEncodingTracking.aacFmp4Muxing,
dashManifest,
period,
H264AndAacEncodingTracking.AAC_FMP4_SEGMENTS_PATH,
aacAudioAdaptationSet.getId());
return dashManifest;
}
/**
* Creates the HLS manifest master playlist with the different sub playlists
*
* @param output the output that should be used
* @param h264AndAacEncodingTracking the tracking information for the H264/AAC encoding
* @param h265AndDolbyDigitalEncodingTracking the tracking information for the H265 encoding
* @return the created HLS manifest
*/
private static HlsManifest createHlsManifest(
Output output,
H264AndAacEncodingTracking h264AndAacEncodingTracking,
H265AndDolbyDigitalEncodingTracking h265AndDolbyDigitalEncodingTracking) {
HlsManifest hlsManifest = createHlsMasterManifest("master.m3u8", output, "/");
// Create h265 audio playlists
createAudioMediaPlaylist(
h265AndDolbyDigitalEncodingTracking.encoding,
hlsManifest,
h265AndDolbyDigitalEncodingTracking.dolbyDigitalFmp4Muxing,
h265AndDolbyDigitalEncodingTracking.dolbyDigitalAudioStream,
"audio_dolby_digital_fmp4.m3u8",
H265AndDolbyDigitalEncodingTracking.DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
HLS_AUDIO_GROUP_DOLBY_DIGITAL_FMP4);
// Create h265 video playlists
for (Rendition rendition : h265AndDolbyDigitalEncodingTracking.h265Fmp4Muxings.keySet()) {
createVideoStreamPlaylist(
h265AndDolbyDigitalEncodingTracking.encoding,
hlsManifest,
h265AndDolbyDigitalEncodingTracking.h265Fmp4Muxings.get(rendition),
h265AndDolbyDigitalEncodingTracking.h265VideoStreams.get(rendition),
String.format("video_h265_%dp_%d.m3u8", rendition.height, rendition.bitrate),
String.format(
H265AndDolbyDigitalEncodingTracking.H265_FMP4_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate),
HLS_AUDIO_GROUP_DOLBY_DIGITAL_FMP4);
}
// Create h264 audio playlists
createAudioMediaPlaylist(
h264AndAacEncodingTracking.encoding,
hlsManifest,
h264AndAacEncodingTracking.aacFmp4Muxing,
h264AndAacEncodingTracking.aacAudioStream,
"audio_aac_fmp4.m3u8",
H264AndAacEncodingTracking.AAC_FMP4_SEGMENTS_PATH,
HLS_AUDIO_GROUP_AAC_FMP4);
createAudioMediaPlaylist(
h264AndAacEncodingTracking.encoding,
hlsManifest,
h264AndAacEncodingTracking.aacTsMuxing,
h264AndAacEncodingTracking.aacAudioStream,
"audio_aac_ts.m3u8",
H264AndAacEncodingTracking.AAC_TS_SEGMENTS_PATH,
HLS_AUDIO_GROUP_AAC_TS);
// Create h264 video playlists
for (Rendition rendition : h264AndAacEncodingTracking.h264TsMuxings.keySet()) {
createVideoStreamPlaylist(
h264AndAacEncodingTracking.encoding,
hlsManifest,
h264AndAacEncodingTracking.h264TsMuxings.get(rendition),
h264AndAacEncodingTracking.h264VideoStreams.get(rendition),
String.format("video_h264_%dp_%d.m3u8", rendition.height, rendition.bitrate),
String.format(
H264AndAacEncodingTracking.H264_TS_SEGMENTS_PATH_FORMAT,
rendition.height,
rendition.bitrate),
HLS_AUDIO_GROUP_AAC_TS);
}
return hlsManifest;
}
/**
* 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) {
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) {
S3Output s3Output = new S3Output();
s3Output.setBucketName(bucketName);
s3Output.setAccessKey(accessKey);
s3Output.setSecretKey(secretKey);
return bitmovinApi.encoding.outputs.s3.create(s3Output);
}
/**
* 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 This is the name of the encoding
* @param description This is the description of the encoding
*/
private static Encoding createEncoding(String name, String description) {
Encoding encoding = new Encoding();
encoding.setName(name);
encoding.setDescription(description);
return bitmovinApi.encoding.encodings.create(encoding);
}
/**
* Creates a stream which binds an input file to a codec configuration. The stream is used for
* muxings later on.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsStreamsByEncodingId
*
* @param encoding The encoding to add the stream onto
* @param input The input that should be used
* @param inputPath The path to the input file
* @param codecConfiguration The codec configuration to be applied to the stream
*/
private static Stream createStream(
Encoding encoding, HttpInput input, String inputPath, CodecConfiguration codecConfiguration) {
StreamInput streamInput = new StreamInput();
streamInput.setInputId(input.getId());
streamInput.setInputPath(inputPath);
streamInput.setSelectionMode(StreamSelectionMode.AUTO);
Stream stream = new Stream();
stream.addInputStreamsItem(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
*/
private static H264VideoConfiguration createH264VideoConfig(int height, long bitrate) {
H264VideoConfiguration config = new H264VideoConfiguration();
config.setName("H.264 video config " + height + "p");
config.setPresetConfiguration(PresetConfiguration.VOD_STANDARD);
config.setHeight(height);
config.setBitrate(bitrate);
return bitmovinApi.encoding.configurations.video.h264.create(config);
}
/**
* Creates a base H.265 video configuration. The width of the video will be set accordingly to the
* aspect ratio of the source video.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsVideoH265
*/
private static H265VideoConfiguration createH265VideoConfig(int height, long bitrate) {
H265VideoConfiguration config = new H265VideoConfiguration();
config.setName("H.265 video config " + height + "p");
config.setPresetConfiguration(PresetConfiguration.VOD_STANDARD);
config.setHeight(height);
config.setBitrate(bitrate);
return bitmovinApi.encoding.configurations.video.h265.create(config);
}
/**
* Creates a base VP9 video configuration. The width of the video will be set accordingly to the
* aspect ratio of the source video.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsVideoVp9
*/
private static Vp9VideoConfiguration createVp9VideoConfiguration(int height, long bitrate) {
Vp9VideoConfiguration config = new Vp9VideoConfiguration();
config.setName("VP9 video configuration " + height + "p");
config.setPresetConfiguration(PresetConfiguration.VOD_STANDARD);
config.setHeight(height);
config.setBitrate(bitrate);
return bitmovinApi.encoding.configurations.video.vp9.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() {
AacAudioConfiguration config = new AacAudioConfiguration();
config.setName("AAC 128 kbit/s");
config.setBitrate(128_000L);
return bitmovinApi.encoding.configurations.audio.aac.create(config);
}
/**
* Creates a Vorbis audio configuration. The sample rate of the audio will be set accordingly to
* the sample rate of the source audio.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsAudioVorbis
*/
private static VorbisAudioConfiguration createVorbisAudioConfiguration() {
VorbisAudioConfiguration config = new VorbisAudioConfiguration();
config.setName("Vorbis 128 kbit/s");
config.setBitrate(128_000L);
return bitmovinApi.encoding.configurations.audio.vorbis.create(config);
}
/**
* Creates a fragmented MP4 muxing. This will generate segments with a given segment length for
* adaptive streaming.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/all#/Encoding/PostEncodingEncodingsMuxingsFmp4ByEncodingId
*
* @param encoding The encoding to add the 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 fragmented segments will be written to
* @param stream The stream that is associated with the muxing
*/
private static Fmp4Muxing createFmp4Muxing(
Encoding encoding, Output output, String outputPath, Stream stream) {
MuxingStream muxingStream = new MuxingStream();
muxingStream.setStreamId(stream.getId());
Fmp4Muxing muxing = new Fmp4Muxing();
muxing.addOutputsItem(buildEncodingOutput(output, outputPath));
muxing.addStreamsItem(muxingStream);
muxing.setSegmentLength(4.0);
return bitmovinApi.encoding.encodings.muxings.fmp4.create(encoding.getId(), muxing);
}
/**
* Creates a CMAF muxing. This will generate segments with a given segment length for adaptive
* streaming.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/all#/Encoding/PostEncodingEncodingsMuxingsCmafByEncodingId
*
* @param encoding The encoding to add the 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 fragmented segments will be written to
* @param stream The stream that is associated with the muxing
*/
private static CmafMuxing createCmafMuxing(
Encoding encoding, Output output, String outputPath, Stream stream) {
MuxingStream muxingStream = new MuxingStream();
muxingStream.setStreamId(stream.getId());
CmafMuxing muxing = new CmafMuxing();
muxing.addOutputsItem(buildEncodingOutput(output, outputPath));
muxing.addStreamsItem(muxingStream);
muxing.setSegmentLength(4.0);
return bitmovinApi.encoding.encodings.muxings.cmaf.create(encoding.getId(), muxing);
}
/**
* Creates a WebM muxing. This will generate segments with a given segment length for adaptive
* streaming.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/all#/Encoding/PostEncodingEncodingsMuxingsWebmByEncodingId
*
* @param encoding The encoding to add the 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 fragmented segments will be written to
* @param stream The stream that is associated with the muxing
*/
private static WebmMuxing createWebmMuxing(
Encoding encoding, Output output, String outputPath, Stream stream) {
MuxingStream muxingStream = new MuxingStream();
muxingStream.setStreamId(stream.getId());
WebmMuxing muxing = new WebmMuxing();
muxing.addOutputsItem(buildEncodingOutput(output, outputPath));
muxing.addStreamsItem(muxingStream);
muxing.setSegmentLength(4.0);
return bitmovinApi.encoding.encodings.muxings.webm.create(encoding.getId(), muxing);
}
/**
* Creates a fragmented TS muxing. This will generate segments with a given segment length for
* adaptive streaming.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/all#/Encoding/PostEncodingEncodingsMuxingsFmp4ByEncodingId
*
* @param encoding The encoding where to add the 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 segments will be written to
* @param stream The stream that is associated with the muxing
*/
private static TsMuxing createTsMuxing(
Encoding encoding, Output output, String outputPath, Stream stream) {
MuxingStream muxingStream = new MuxingStream();
muxingStream.setStreamId(stream.getId());
TsMuxing muxing = new TsMuxing();
muxing.addOutputsItem(buildEncodingOutput(output, outputPath));
muxing.addStreamsItem(muxingStream);
muxing.setSegmentLength(4.0);
return bitmovinApi.encoding.encodings.muxings.ts.create(encoding.getId(), muxing);
}
/**
* Creates a Dolby Digital audio configuration.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/configurations#/Encoding/PostEncodingConfigurationsAudioDD
*/
private static DolbyDigitalAudioConfiguration createDolbyDigitalAudioConfig() {
DolbyDigitalAudioConfiguration config = new DolbyDigitalAudioConfiguration();
config.setName("DolbyDigital Channel Layout 5.1");
config.setBitrate(256_000L);
config.setChannelLayout(DolbyDigitalChannelLayout.CL_5_1);
return bitmovinApi.encoding.configurations.audio.dolbyDigital.create(config);
}
/**
* 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 = MultiCodecEncoding.class.getSimpleName();
return Paths.get(
configProvider.getS3OutputBasePath(),
String.format("%s-%s", className, DATE_STRING),
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 Encoding executeEncoding(Encoding encoding) throws InterruptedException {
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");
return encoding;
}
/** Creates the HLS master manifest. */
private static HlsManifest createHlsMasterManifest(
String name, Output output, String outputPath) {
HlsManifest hlsManifest = new HlsManifest();
hlsManifest.setName(name);
hlsManifest.addOutputsItem(buildEncodingOutput(output, outputPath));
return bitmovinApi.encoding.manifests.hls.create(hlsManifest);
}
/**
* Creates an HLS audio media playlist.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsHlsMediaAudioByManifestId
*
* @param encoding the encoding where the resources belong to
* @param manifest the manifest where the audio playlist should be added
* @param audioMuxing the audio muxing that should be used
* @param audioStream the audio stream of the muxing
* @param audioSegmentsPath the path to the audio segments
*/
private static void createAudioMediaPlaylist(
Encoding encoding,
HlsManifest manifest,
Muxing audioMuxing,
Stream audioStream,
String uri,
String audioSegmentsPath,