-
Notifications
You must be signed in to change notification settings - Fork 27
/
multi_codec_encoding.py
1227 lines (950 loc) · 47.6 KB
/
multi_codec_encoding.py
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 time
import concurrent.futures
from datetime import datetime
from os import path
from bitmovin_api_sdk import AacAudioConfiguration, AclEntry, AclPermission, \
AudioAdaptationSet, AudioMediaInfo, BitmovinApi, BitmovinApiLogger, CmafMuxing, CodecConfiguration, \
DashCmafRepresentation, DashFmp4Representation, DashManifest, DashProfile, DashRepresentationType, \
DolbyDigitalAudioConfiguration, DolbyDigitalChannelLayout, Encoding, EncodingOutput, Fmp4Muxing, \
H264VideoConfiguration, H265VideoConfiguration, HlsManifest, HttpInput, Input, MessageType, Muxing, MuxingStream, \
Output, Period, PresetConfiguration, S3Output, Status, Stream, StreamInfo, StreamInput, StreamSelectionMode Task, \
TsMuxing, VideoAdaptationSet, VorbisAudioConfiguration, Vp9VideoConfiguration, WebmMuxing
from common.config_provider import ConfigProvider
"""
This example demonstrates how to use different codecs and muxing types in a single encoding.
<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>
"""
EXAMPLE_NAME = "MultiCodecEncoding-{}".format(datetime.now().isoformat(timespec='seconds'))
config_provider = ConfigProvider()
bitmovin_api = BitmovinApi(api_key=config_provider.get_bitmovin_api_key(),
# uncomment the following line if you are working with a multi-tenant account
# tenant_org_id=config_provider.get_bitmovin_tenant_org_id(),
logger=BitmovinApiLogger())
HLS_AUDIO_GROUP_AAC_FMP4 = "audio-aac-fmp4"
HLS_AUDIO_GROUP_AAC_TS = "audio-aac-ts"
HLS_AUDIO_GROUP_DOLBY_DIGITAL_FMP4 = "audio-dolby-digital-fmp4"
H264_TS_SEGMENTS_PATH_FORMAT = "video/h264/ts/{}p_{}"
H264_CMAF_SEGMENTS_PATH_FORMAT = "video/h264/cmaf/{}p_{}"
AAC_FMP4_SEGMENTS_PATH = "audio/aac/fmp4"
AAC_TS_SEGMENTS_PATH = "audio/aac/ts"
H265_FMP4_SEGMENTS_PATH_FORMAT = "video/h265/fmp4/{}p_{}"
DOLBY_DIGITAL_FMP4_SEGMENTS_PATH = "audio/dolby-digital/fmp4"
VP9_WEBM_SEGMENTS_PATH_FORMAT = "video/vp9/webm/{}p_{}"
VORBIS_WEBM_SEGMENTS_PATH = "audio/vorbis/webm"
class Rendition:
def __init__(self, height, bitrate):
# type: (int, int) -> None
self.height = height
self.bitrate = bitrate
class H264AndAacEncodingTracking:
def __init__(self, encoding):
# type: (Encoding) -> None
self.encoding = encoding
self.aac_audio_stream = None
self.aac_fmp4_muxing = None
self.aac_ts_muxing = None
self.h264_video_streams = {}
self.h264_cmaf_muxings = {}
self.h264_ts_muxings = {}
self.renditions = [Rendition(height=234, bitrate=145000),
Rendition(height=360, bitrate=365000),
Rendition(height=432, bitrate=730000),
Rendition(height=540, bitrate=2000000),
Rendition(height=720, bitrate=3000000)]
class H265AndDolbyDigitalEncodingTracking:
def __init__(self, encoding):
# type: (Encoding) -> None
self.encoding = encoding
self.dolby_digital_audio_stream = None
self.dolby_digital_fmp4_muxing = None
self.h265_video_streams = {}
self.h265_fmp4_muxings = {}
self.renditions = [Rendition(height=540, bitrate=600000),
Rendition(height=720, bitrate=2400000),
Rendition(height=1080, bitrate=4500000),
Rendition(height=2160, bitrate=11600000)]
class Vp9AndVorbisEncodingTracking:
def __init__(self, encoding):
# type: (Encoding) -> None
self.encoding = encoding
self.vp9_webm_muxings = {}
self.vorbis_webm_muxing = None
self.renditions = [Rendition(height=540, bitrate=600000),
Rendition(height=720, bitrate=2400000),
Rendition(height=1080, bitrate=4500000),
Rendition(height=2160, bitrate=11600000)]
def main():
http_input = _create_http_input(host=config_provider.get_http_input_host())
output = _create_s3_output(
bucket_name=config_provider.get_s3_output_bucket_name(),
access_key=config_provider.get_s3_output_access_key(),
secret_key=config_provider.get_s3_output_secret_key()
)
input_path = config_provider.get_http_input_file_path()
h264_and_aac_encoding_tracking = _create_h264_and_aac_encoding(
encoding_input=http_input,
input_path=input_path,
output=output
)
h265_and_dolby_digital_encoding_tracking = _create_h265_and_dolby_digital_encoding(
encoding_input=http_input,
input_path=input_path,
output=output
)
vp9_and_vorbis_encoding_tracking = _create_vp9_and_vorbis_encoding(
encoding_input=http_input,
input_path=input_path,
output=output
)
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
executor.map(_execute_encoding, [
h264_and_aac_encoding_tracking.encoding,
h265_and_dolby_digital_encoding_tracking.encoding,
vp9_and_vorbis_encoding_tracking.encoding
])
dash_manifest = _extend_dash_manifest(
output=output,
h264_and_aac_encoding_tracking=h264_and_aac_encoding_tracking,
h265_and_dolby_digital_encoding_tracking=h265_and_dolby_digital_encoding_tracking,
vp9_and_vorbis_encoding_tracking=vp9_and_vorbis_encoding_tracking
)
_execute_dash_manifest(dash_manifest=dash_manifest)
hls_manifest = _extend_hls_manifest(
output=output,
h264_and_aac_encoding_tracking=h264_and_aac_encoding_tracking,
h265_and_dolby_digital_encoding_tracking=h265_and_dolby_digital_encoding_tracking
)
_execute_hls_manifest(hls_manifest=hls_manifest)
def _create_h264_and_aac_encoding(encoding_input, input_path, output):
# type: (Input, str, Output) -> H264AndAacEncodingTracking
"""
Creates the encoding with H264 codec/TS muxing, H264 codec/CMAF muxing, AAC codec/fMP4 muxing
:param encoding_input: the input that should be used
:param input_path: the path to the input file
:param output: the output that should be used
:return: the tracking information for the encoding
"""
encoding = _create_encoding(
name="H.264 Encoding",
description="H.264 -> TS muxing, H.264 -> CMAF muxing, AAC -> fMP4 muxing"
)
encoding_tracking = H264AndAacEncodingTracking(encoding=encoding)
for rendition in encoding_tracking.renditions:
video_configuration = _create_h264_video_configuration(height=rendition.height, bitrate=rendition.bitrate)
video_stream = _create_stream(
encoding=encoding,
encoding_input=encoding_input,
input_path=input_path,
codec_configuration=video_configuration
)
cmaf_muxing = _create_cmaf_muxing(
encoding=encoding,
output=output,
output_path=H264_CMAF_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
stream=video_stream
)
ts_muxing = _create_ts_muxing(
encoding=encoding,
output=output,
output_path=H264_TS_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
stream=video_stream
)
encoding_tracking.h264_video_streams[rendition] = video_stream
encoding_tracking.h264_cmaf_muxings[rendition] = cmaf_muxing
encoding_tracking.h264_ts_muxings[rendition] = ts_muxing
aac_config = _create_aac_audio_configuration()
aac_audio_stream = _create_stream(
encoding=encoding,
encoding_input=encoding_input,
input_path=input_path,
codec_configuration=aac_config
)
encoding_tracking.aac_audio_stream = aac_audio_stream
encoding_tracking.aac_fmp4_muxing = _create_fmp4_muxing(
encoding=encoding,
output=output,
output_path=AAC_FMP4_SEGMENTS_PATH,
stream=aac_audio_stream
)
encoding_tracking.aac_ts_muxing = _create_ts_muxing(
encoding=encoding,
output=output,
output_path=AAC_TS_SEGMENTS_PATH,
stream=aac_audio_stream
)
return encoding_tracking
def _create_h265_and_dolby_digital_encoding(encoding_input, input_path, output):
# type: (Input, str, Output) -> H265AndDolbyDigitalEncodingTracking
"""
Creates the encoding with H265 codec/fMP4 muxing, Dolby Digital codec/fMP4 muxing
:param encoding_input: the input that should be used
:param input_path: the path to the input file
:param output: the output that should be used
:return: the tracking information for the encoding
"""
encoding = _create_encoding(name="H.265 Encoding", description="H.265 -> fMP4 muxing, Dolby Digital -> fMP4 muxing")
encoding_tracking = H265AndDolbyDigitalEncodingTracking(encoding=encoding)
for rendition in encoding_tracking.renditions:
video_configuration = _create_h265_video_configuration(height=rendition.height, bitrate=rendition.bitrate)
video_stream = _create_stream(
encoding=encoding,
encoding_input=encoding_input,
input_path=input_path,
codec_configuration=video_configuration
)
fmp4_muxing = _create_fmp4_muxing(
encoding=encoding,
output=output,
output_path=H265_FMP4_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
stream=video_stream
)
encoding_tracking.h265_video_streams[rendition] = video_stream
encoding_tracking.h265_fmp4_muxings[rendition] = fmp4_muxing
dolby_digital_config = _create_dolby_digital_audio_configuration()
dolby_digital_audio_stream = _create_stream(
encoding=encoding,
encoding_input=encoding_input,
input_path=input_path,
codec_configuration=dolby_digital_config
)
encoding_tracking.dolby_digital_audio_stream = dolby_digital_audio_stream
encoding_tracking.dolby_digital_fmp4_muxing = _create_fmp4_muxing(
encoding=encoding,
output=output,
output_path=DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
stream=dolby_digital_audio_stream
)
return encoding_tracking
def _create_vp9_and_vorbis_encoding(encoding_input, input_path, output):
# type: (Input, str, Output) -> Vp9AndVorbisEncodingTracking
"""
Created the encoding with VP9 codec/WebM muxing, Vorbis codec / WebM muxing
:param encoding_input: the input that should be used
:param input_path: the path to the input file
:param output: the output that should be used
:return: the tracking information for the encoding
"""
encoding = _create_encoding(name="VP9/Vorbis Encoding", description="VP9 -> WebM muxing, Vorbis -> WebM muxing")
encoding_tracking = Vp9AndVorbisEncodingTracking(encoding=encoding)
for rendition in encoding_tracking.renditions:
vp9_config = _create_vp9_video_configuration(rendition.height, rendition.bitrate)
vp9_video_stream = _create_stream(
encoding=encoding,
encoding_input=encoding_input,
input_path=input_path,
codec_configuration=vp9_config
)
encoding_tracking.vp9_webm_muxings[rendition] = _create_webm_muxing(
encoding=encoding,
output=output,
output_path=VP9_WEBM_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
stream=vp9_video_stream
)
vorbis_audio_configuration = _create_vorbis_audio_configuration()
vorbis_audio_stream = _create_stream(
encoding=encoding,
encoding_input=encoding_input,
input_path=input_path,
codec_configuration=vorbis_audio_configuration
)
encoding_tracking.vorbis_webm_muxing = _create_webm_muxing(
encoding=encoding,
output=output,
output_path=VORBIS_WEBM_SEGMENTS_PATH,
stream=vorbis_audio_stream
)
return encoding_tracking
def _extend_dash_manifest(output,
h264_and_aac_encoding_tracking,
h265_and_dolby_digital_encoding_tracking,
vp9_and_vorbis_encoding_tracking):
# type: (Output, H264AndAacEncodingTracking, H265AndDolbyDigitalEncodingTracking, Vp9AndVorbisEncodingTracking) -> DashManifest
"""
Creates the DASH manifest with all the representations.
:param output: the output that should be used
:param h264_and_aac_encoding_tracking: the tracking information for the H264/AAC encoding
:param h265_and_dolby_digital_encoding_tracking: the tracking information for the H265 encoding
:param vp9_and_vorbis_encoding_tracking: the tracking information for the VP9/Vorbis encoding
:return: the created DASH manifest
"""
dash_manifest = _create_dash_manifest(name="stream.mpd", dash_profile=DashProfile.LIVE, output=output,
output_path="")
period = bitmovin_api.encoding.manifests.dash.periods.create(manifest_id=dash_manifest.id, period=Period())
video_adaptation_set_vp9 = bitmovin_api.encoding.manifests.dash.periods.adaptationsets.video.create(
manifest_id=dash_manifest.id,
period_id=period.id,
video_adaptation_set=VideoAdaptationSet()
)
video_adaptation_set_h265 = bitmovin_api.encoding.manifests.dash.periods.adaptationsets.video.create(
manifest_id=dash_manifest.id,
period_id=period.id,
video_adaptation_set=VideoAdaptationSet()
)
video_adaptation_set_h264 = bitmovin_api.encoding.manifests.dash.periods.adaptationsets.video.create(
manifest_id=dash_manifest.id,
period_id=period.id,
video_adaptation_set=VideoAdaptationSet()
)
vorbis_audio_adaptationset = bitmovin_api.encoding.manifests.dash.periods.adaptationsets.audio.create(
manifest_id=dash_manifest.id,
period_id=period.id,
audio_adaptation_set=AudioAdaptationSet(lang="en"))
dolby_digital_audio_adaptationset = bitmovin_api.encoding.manifests.dash.periods.adaptationsets.audio.create(
manifest_id=dash_manifest.id,
period_id=period.id,
audio_adaptation_set=AudioAdaptationSet(lang="en"))
aac_audio_adaptationset = bitmovin_api.encoding.manifests.dash.periods.adaptationsets.audio.create(
manifest_id=dash_manifest.id,
period_id=period.id,
audio_adaptation_set=AudioAdaptationSet(lang="en"))
# Add representations to VP9 adaptation set
# Add VORBIS WEBM muxing to VORBIS audio adaptation set
_create_dash_webm_representation(
encoding=vp9_and_vorbis_encoding_tracking.encoding,
muxing=vp9_and_vorbis_encoding_tracking.vorbis_webm_muxing,
dash_manifest=dash_manifest,
period=period,
segment_path=VORBIS_WEBM_SEGMENTS_PATH,
adaptation_set_id=vorbis_audio_adaptationset.id
)
# Add VP9 WEBM muxing to VP9 adaptation set
for rendition in vp9_and_vorbis_encoding_tracking.vp9_webm_muxings.keys():
_create_dash_webm_representation(
encoding=vp9_and_vorbis_encoding_tracking.encoding,
muxing=vp9_and_vorbis_encoding_tracking.vp9_webm_muxings[rendition],
dash_manifest=dash_manifest,
period=period,
segment_path=VP9_WEBM_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
adaptation_set_id=video_adaptation_set_vp9.id
)
# Add representations to H265 adaptation set
# Add Dolby Digital FMP4 muxing to Dolby Digital audio adaptation set
_create_dash_fmp4_representation(
encoding=h265_and_dolby_digital_encoding_tracking.encoding,
muxing=h265_and_dolby_digital_encoding_tracking.dolby_digital_fmp4_muxing,
dash_manifest=dash_manifest,
period=period,
segment_path=DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
adaptation_set_id=dolby_digital_audio_adaptationset.id
)
# Add H265 FMP4 muxing to H265 video adaptation set
for rendition in h265_and_dolby_digital_encoding_tracking.h265_fmp4_muxings.keys():
_create_dash_fmp4_representation(
encoding=h265_and_dolby_digital_encoding_tracking.encoding,
muxing=h265_and_dolby_digital_encoding_tracking.h265_fmp4_muxings[rendition],
dash_manifest=dash_manifest,
period=period,
segment_path=H265_FMP4_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
adaptation_set_id=video_adaptation_set_h265.id
)
# Add representations to H264 adaptation set
# Add AAC FMP4 muxing to AAC audio adaptation set
_create_dash_fmp4_representation(
encoding=h264_and_aac_encoding_tracking.encoding,
muxing=h264_and_aac_encoding_tracking.aac_fmp4_muxing,
dash_manifest=dash_manifest,
period=period,
segment_path=AAC_FMP4_SEGMENTS_PATH,
adaptation_set_id=aac_audio_adaptationset.id
)
# Add H264 CMAF muxings to H264 video adaptation set
for rendition in h264_and_aac_encoding_tracking.h264_cmaf_muxings.keys():
_create_dash_cmaf_representation(
encoding=h264_and_aac_encoding_tracking.encoding,
muxing=h264_and_aac_encoding_tracking.h264_cmaf_muxings[rendition],
dash_manifest=dash_manifest,
period=period,
segment_path=H264_CMAF_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
adaptation_set_id=video_adaptation_set_h264.id
)
return dash_manifest
def _extend_hls_manifest(output,
h264_and_aac_encoding_tracking,
h265_and_dolby_digital_encoding_tracking):
# type: (Output, H264AndAacEncodingTracking, H265AndDolbyDigitalEncodingTracking) -> HlsManifest
"""
Creates the HLS manifest master playlist with the different sub playlists
:param output: the output that should be used
:param h264_and_aac_encoding_tracking: the tracking information for the H264/AAC encoding
:param h265_and_dolby_digital_encoding_tracking: the tracking information for the H265 encoding
:return: the created DASH manifest
"""
hls_manifest = _create_hls_manifest(
name="master.m3u8",
output=output,
output_path=""
)
# Add representations to H265 adaptation set
_create_audio_media_playlist(
encoding=h265_and_dolby_digital_encoding_tracking.encoding,
manifest=hls_manifest,
audio_muxing=h265_and_dolby_digital_encoding_tracking.dolby_digital_fmp4_muxing,
audio_stream=h265_and_dolby_digital_encoding_tracking.dolby_digital_audio_stream,
uri="audio_dolby_digital_fmp4.m3u8",
segments_path=DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
audio_group=HLS_AUDIO_GROUP_DOLBY_DIGITAL_FMP4
)
for rendition in h265_and_dolby_digital_encoding_tracking.h265_fmp4_muxings.keys():
_create_video_media_playlist(
encoding=h265_and_dolby_digital_encoding_tracking.encoding,
manifest=hls_manifest,
video_muxing=h265_and_dolby_digital_encoding_tracking.h265_fmp4_muxings.get(rendition),
video_stream=h265_and_dolby_digital_encoding_tracking.h265_video_streams.get(rendition),
uri="video_h265_{}p_{}.m3u8".format(rendition.height, rendition.bitrate),
segment_path=H265_FMP4_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
audio_group=HLS_AUDIO_GROUP_DOLBY_DIGITAL_FMP4
)
# Add representations to H264 adaptation set
_create_audio_media_playlist(
encoding=h264_and_aac_encoding_tracking.encoding,
manifest=hls_manifest,
audio_muxing=h264_and_aac_encoding_tracking.aac_fmp4_muxing,
audio_stream=h264_and_aac_encoding_tracking.aac_audio_stream,
uri="audio_aac_fmp4.m3u8",
segments_path=AAC_FMP4_SEGMENTS_PATH,
audio_group=HLS_AUDIO_GROUP_AAC_FMP4
)
_create_audio_media_playlist(
encoding=h264_and_aac_encoding_tracking.encoding,
manifest=hls_manifest,
audio_muxing=h264_and_aac_encoding_tracking.aac_ts_muxing,
audio_stream=h264_and_aac_encoding_tracking.aac_audio_stream,
uri="audio_aac_ts.m3u8",
segments_path=AAC_TS_SEGMENTS_PATH,
audio_group=HLS_AUDIO_GROUP_AAC_TS
)
# Add H264 TS muxings to H264 video adaptation set
for rendition in h264_and_aac_encoding_tracking.h264_ts_muxings.keys():
_create_video_media_playlist(
encoding=h264_and_aac_encoding_tracking.encoding,
manifest=hls_manifest,
video_muxing=h264_and_aac_encoding_tracking.h264_ts_muxings.get(rendition),
video_stream=h264_and_aac_encoding_tracking.h264_video_streams.get(rendition),
uri="video_h264_{}p_{}.m3u8".format(rendition.height, rendition.bitrate),
segment_path=H264_TS_SEGMENTS_PATH_FORMAT.format(rendition.height, rendition.bitrate),
audio_group=HLS_AUDIO_GROUP_AAC_TS
)
return hls_manifest
def _create_dash_cmaf_representation(encoding, muxing, dash_manifest, period, segment_path, adaptation_set_id):
# type: (Encoding, CmafMuxing, DashManifest, Period, str, str) -> None
"""
Creates a DASH CMAF representation
<p>API endpoint:
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsDashPeriodsAdaptationsetsRepresentationsCmafByManifestIdAndPeriodIdAndAdaptationsetId
:param encoding: the encoding where the resources belong to
:param muxing: the muxing that should be used for this representation
:param dash_manifest: the dash manifest to which the representation should be added
:param period: the period to which the representation should be added
:param segment_path: the path the the CMAF segments
:param adaptation_set_id: the adaptation set to which the representation should be added
"""
dash_cmaf_representation = DashCmafRepresentation(
type_=DashRepresentationType.TEMPLATE,
encoding_id=encoding.id,
muxing_id=muxing.id,
segment_path=segment_path
)
bitmovin_api.encoding.manifests.dash.periods.adaptationsets.representations.cmaf.create(
manifest_id=dash_manifest.id,
period_id=period.id,
adaptationset_id=adaptation_set_id,
dash_cmaf_representation=dash_cmaf_representation
)
def _create_dash_fmp4_representation(encoding, muxing, dash_manifest, period, segment_path, adaptation_set_id):
# type: (Encoding, CmafMuxing, DashManifest, Period, str, str) -> None
"""
Creates a DASH FMP4 representation
<p>API endpoint:
https://bitmovin.com/docs/encoding/api-reference/all#/Encoding/PostEncodingEncodingsMuxingsFmp4ByEncodingId
:param encoding: the encoding where the resources belong to
:param muxing: the muxing that should be used for this representation
:param dash_manifest: the dash manifest to which the representation should be added
:param period: the period to which the representation should be added
:param segment_path: the path the the FMP4 segments
:param adaptation_set_id: the adaptation set to which the representation should be added
"""
dash_fmp4_representation = DashFmp4Representation(
type_=DashRepresentationType.TEMPLATE,
encoding_id=encoding.id,
muxing_id=muxing.id,
segment_path=segment_path
)
bitmovin_api.encoding.manifests.dash.periods.adaptationsets.representations.cmaf.create(
manifest_id=dash_manifest.id,
period_id=period.id,
adaptationset_id=adaptation_set_id,
dash_cmaf_representation=dash_fmp4_representation
)
def _create_dash_webm_representation(encoding, muxing, dash_manifest, period, segment_path, adaptation_set_id):
# type: (Encoding, CmafMuxing, DashManifest, Period, str, str) -> None
"""
Creates a DASH WebM representation
<p>API endpoint:
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsDashPeriodsAdaptationsetsRepresentationsWebmByManifestIdAndPeriodIdAndAdaptationsetId
:param encoding: the encoding where the resources belong to
:param muxing: the muxing that should be used for this representation
:param dash_manifest: the dash manifest to which the representation should be added
:param period: the period to which the representation should be added
:param segment_path: the path the the WebM segments
:param adaptation_set_id: the adaptation set to which the representation should be added
"""
dash_webm_representation = DashFmp4Representation(
type_=DashRepresentationType.TEMPLATE,
encoding_id=encoding.id,
muxing_id=muxing.id,
segment_path=segment_path
)
bitmovin_api.encoding.manifests.dash.periods.adaptationsets.representations.webm.create(
manifest_id=dash_manifest.id,
period_id=period.id,
adaptationset_id=adaptation_set_id,
dash_webm_representation=dash_webm_representation
)
def _create_dash_manifest(name, dash_profile, output, output_path):
# type: (str, DashProfile, Output, str) -> DashManifest
"""
Creates a DASH manifest
<p>API endpoint:
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsDash
:param name: the resource name
:param dash_profile: the DASH profile of the manifest (ON_DEMAND, LIVE)
:param output: the output of the manifest
:param output_path: the output path where the manifest is written to
:return: the created manifest
"""
dash_manifest = DashManifest(
name=name,
profile=dash_profile,
outputs=[_build_encoding_output(output=output, output_path=output_path)]
)
return bitmovin_api.encoding.manifests.dash.create(dash_manifest=dash_manifest)
def _create_hls_manifest(name, output, output_path):
# type: (str, Output, str) -> HlsManifest
"""
Creates a HLS manifest
<p>API endpoint:
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsDash
:param name: the resource name
:param output: the output of the manifest
:param output_path: the output path where the manifest is written to
:return: the created manifest
"""
hls_manifest = HlsManifest(
name=name,
outputs=[_build_encoding_output(output=output, output_path=output_path)]
)
return bitmovin_api.encoding.manifests.hls.create(hls_manifest=hls_manifest)
def _create_audio_media_playlist(encoding,
manifest,
audio_muxing,
audio_stream,
uri,
segments_path,
audio_group):
# type: (Encoding, HlsManifest, Muxing, Stream, str, str, str) -> None
"""
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 audio_muxing: the audio muxing that should be used
:param audio_stream: the audio stream of the muxing
:param segments_path: the path to the audio segments
"""
audio_media_info = AudioMediaInfo(
name=uri,
uri=uri,
group_id=audio_group,
encoding_id=encoding.id,
stream_id=audio_stream.id,
muxing_id=audio_muxing.id,
language="en",
assoc_language="en",
autoselect=False,
is_default=False,
forced=False,
segment_path=segments_path
)
bitmovin_api.encoding.manifests.hls.media.audio.create(manifest_id=manifest.id, audio_media_info=audio_media_info)
def _create_video_media_playlist(encoding, manifest, video_muxing, video_stream, uri, segment_path, audio_group):
# type: (Encoding, HlsManifest, Muxing, Stream, str, str, str) -> None
"""
Creates an HLS video playlist
<p>API endpoint:
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsHls
:param encoding: the encoding where the resources belong to
:param manifest: the master manifest where the video stream playlist should belong to
:param video_muxing: the muxing that should be used
:param video_stream: the stream of the muxing
:param uri: the relative uri of the playlist file that will be generated
:param segment_path: the path pointing to the respective video segments
"""
stream_info = StreamInfo(
uri=uri,
encoding_id=encoding.id,
stream_id=video_stream.id,
muxing_id=video_muxing.id,
audio=audio_group,
segment_path=segment_path
)
bitmovin_api.encoding.manifests.hls.streams.create(manifest_id=manifest.id, stream_info=stream_info)
def _execute_encoding(encoding):
# type: (Encoding) -> None
"""
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
"""
bitmovin_api.encoding.encodings.start(encoding_id=encoding.id)
task = _get_encoding_status(encoding_id=encoding.id)
while task.status is not Status.FINISHED and task.status is not Status.ERROR:
task = _get_encoding_status(encoding_id=encoding.id)
if task.status is Status.ERROR:
_log_task_errors(task=task)
raise Exception("Encoding failed")
print("Encoding finished successfully")
def _execute_dash_manifest(dash_manifest):
# type: (DashManifest) -> None
"""
Starts the dash manifest generation process and periodically polls its status until it reaches
a final state
<p>API endpoints:
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsDashStartByManifestId
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/GetEncodingManifestsDashStatusByManifestId
<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 dash_manifest: The dash manifest to be started
"""
bitmovin_api.encoding.manifests.dash.start(manifest_id=dash_manifest.id)
task = _get_dash_manifest_status(manifest_id=dash_manifest.id)
while task.status is not Status.FINISHED and task.status is not Status.ERROR:
task = _get_dash_manifest_status(manifest_id=dash_manifest.id)
if task.status is Status.ERROR:
_log_task_errors(task=task)
raise Exception("DASH manifest failed")
print("DASH manifest finished successfully")
def _execute_hls_manifest(hls_manifest):
# type: (HlsManifest) -> None
"""
Starts the hls manifest generation process and periodically polls its status until it reaches a
final state
<p>API endpoints:
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/PostEncodingManifestsHlsStartByManifestId
https://bitmovin.com/docs/encoding/api-reference/sections/manifests#/Encoding/GetEncodingManifestsHlsStatusByManifestId
<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 hls_manifest: The dash manifest to be started
"""
bitmovin_api.encoding.manifests.hls.start(manifest_id=hls_manifest.id)
task = _get_hls_manifest_status(manifest_id=hls_manifest.id)
while task.status is not Status.FINISHED and task.status is not Status.ERROR:
task = _get_hls_manifest_status(manifest_id=hls_manifest.id)
if task.status is Status.ERROR:
_log_task_errors(task=task)
raise Exception("HLS manifest failed")
print("HLS manifest finished successfully")
def _get_encoding_status(encoding_id):
time.sleep(5)
task = bitmovin_api.encoding.encodings.status(encoding_id=encoding_id)
print("Encoding status is {} (progress: {} %)".format(task.status, task.progress))
return task
def _get_dash_manifest_status(manifest_id):
time.sleep(5)
task = bitmovin_api.encoding.manifests.dash.status(manifest_id=manifest_id)
return task
def _get_hls_manifest_status(manifest_id):
time.sleep(5)
task = bitmovin_api.encoding.manifests.hls.status(manifest_id=manifest_id)
return task
def _create_encoding(name, description):
# type: (str, str) -> Encoding
"""
Creates an Encoding object. This is the base object to configure your encoding.
API endpoint:
https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodings
:param name: A name that will help you identify the encoding in our dashboard (required)
:param description: A description of the encoding (optional)
"""
encoding = Encoding(
name=name,
description=description
)
return bitmovin_api.encoding.encodings.create(encoding=encoding)
def _create_http_input(host):
# type: (str) -> HttpInput
"""
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>
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.
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
"""
http_input = HttpInput(host=host)
return bitmovin_api.encoding.inputs.http.create(http_input=http_input)
def _create_s3_output(bucket_name, access_key, secret_key):
# type: (str, str, str) -> S3Output
"""
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 bucket_name: The name of the S3 bucket
:param access_key: The access key of your S3 account
:param secret_key: The secret key of your S3 account
"""
s3_output = S3Output(
bucket_name=bucket_name,
access_key=access_key,
secret_key=secret_key
)
return bitmovin_api.encoding.outputs.s3.create(s3_output=s3_output)
def _create_h264_video_configuration(height, bitrate):
# type: (int, int) -> H264VideoConfiguration
"""
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
"""
config = H264VideoConfiguration(
name="H.264 {}p".format(height),
preset_configuration=PresetConfiguration.VOD_STANDARD,
height=height,
bitrate=bitrate
)
return bitmovin_api.encoding.configurations.video.h264.create(h264_video_configuration=config)
def _create_h265_video_configuration(height, bitrate):
# type: (int, int) -> H265VideoConfiguration
"""
Creates a basic 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
"""
config = H265VideoConfiguration(
name="H.265 {}p".format(height),
preset_configuration=PresetConfiguration.VOD_STANDARD,
height=height,
bitrate=bitrate
)
return bitmovin_api.encoding.configurations.video.h265.create(h265_video_configuration=config)
def _create_stream(encoding, encoding_input, input_path, codec_configuration):
# type: (Encoding, Input, str, CodecConfiguration) -> Stream
"""
Adds a video or audio 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 encoding_input: The input resource providing the input file
:param input_path: The path to the input file