-
Notifications
You must be signed in to change notification settings - Fork 27
/
MultiCodecEncoding.php
1589 lines (1415 loc) · 51.3 KB
/
MultiCodecEncoding.php
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
<?php
require './vendor/autoload.php';
require_once './common/ConfigProvider.php';
use BitmovinApiSdk\BitmovinApi;
use BitmovinApiSdk\Common\BitmovinApiException;
use BitmovinApiSdk\Common\Logging\ConsoleLogger;
use BitmovinApiSdk\Configuration;
use BitmovinApiSdk\Models\AacAudioConfiguration;
use BitmovinApiSdk\Models\AclEntry;
use BitmovinApiSdk\Models\AclPermission;
use BitmovinApiSdk\Models\AudioAdaptationSet;
use BitmovinApiSdk\Models\AudioMediaInfo;
use BitmovinApiSdk\Models\CmafMuxing;
use BitmovinApiSdk\Models\CodecConfiguration;
use BitmovinApiSdk\Models\DashCmafRepresentation;
use BitmovinApiSdk\Models\DashFmp4Representation;
use BitmovinApiSdk\Models\DashManifest;
use BitmovinApiSdk\Models\DashProfile;
use BitmovinApiSdk\Models\DashRepresentationType;
use BitmovinApiSdk\Models\DashWebmRepresentation;
use BitmovinApiSdk\Models\DolbyDigitalAudioConfiguration;
use BitmovinApiSdk\Models\DolbyDigitalChannelLayout;
use BitmovinApiSdk\Models\Encoding;
use BitmovinApiSdk\Models\EncodingOutput;
use BitmovinApiSdk\Models\Fmp4Muxing;
use BitmovinApiSdk\Models\H264VideoConfiguration;
use BitmovinApiSdk\Models\H265VideoConfiguration;
use BitmovinApiSdk\Models\HlsManifest;
use BitmovinApiSdk\Models\HttpInput;
use BitmovinApiSdk\Models\Input;
use BitmovinApiSdk\Models\MessageType;
use BitmovinApiSdk\Models\Muxing;
use BitmovinApiSdk\Models\MuxingStream;
use BitmovinApiSdk\Models\Output;
use BitmovinApiSdk\Models\Period;
use BitmovinApiSdk\Models\PresetConfiguration;
use BitmovinApiSdk\Models\S3Output;
use BitmovinApiSdk\Models\Status;
use BitmovinApiSdk\Models\Stream;
use BitmovinApiSdk\Models\StreamInfo;
use BitmovinApiSdk\Models\StreamInput;
use BitmovinApiSdk\Models\StreamSelectionMode;
use BitmovinApiSdk\Models\Task;
use BitmovinApiSdk\Models\TsMuxing;
use BitmovinApiSdk\Models\VideoAdaptationSet;
use BitmovinApiSdk\Models\VorbisAudioConfiguration;
use BitmovinApiSdk\Models\Vp9VideoConfiguration;
use BitmovinApiSdk\Models\WebmMuxing;
/**
* 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 PHP 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>
*/
$exampleName = 'MultiCodecEncoding';
$DATE_STRING = date("Y-m-d") . "T" . date("H:i:s");
const HLS_AUDIO_GROUP_AAC_FMP4 = "audio-aac-fmp4";
const HLS_AUDIO_GROUP_AAC_TS = "audio-aac-ts";
const HLS_AUDIO_GROUP_DOLBY_DIGITAL_FMP4 = "audio-dolby-digital-fmp4";
$configProvider = new ConfigProvider();
// Helper classes for manifest generation
class Rendition
{
public $height;
public $bitrate;
public function __construct(int $height, int $bitrate)
{
$this->height = $height;
$this->bitrate = $bitrate;
}
}
class H264AndAacEncodingTracking
{
public $encoding;
public $renditions;
public $h264VideoStreams;
public $h264CmafMuxings;
public $h264TsMuxings;
public $aacAudioStream;
public $aacFmp4Muxing;
public $aacTsMuxing;
public $H264_TS_SEGMENTS_PATH_FORMAT = "video/h264/ts/%dp_%d";
public $H264_CMAF_SEGMENTS_PATH_FORMAT = "video/h264/cmaf/%dp_%d";
public $AAC_FMP4_SEGMENTS_PATH = "audio/aac/fmp4";
public $AAC_TS_SEGMENTS_PATH = "audio/aac/ts";
public function __construct(Encoding $encoding)
{
$this->encoding = $encoding;
$this->renditions = [
new Rendition(234, 145000),
new Rendition(360, 365000),
new Rendition(432, 730000),
new Rendition(540, 2000000),
new Rendition(720, 3000000),
];
}
}
class H265AndDolbyDigitalEncodingTracking
{
public $encoding;
public $renditions;
public $h265VideoStreams;
public $h265Fmp4Muxings;
public $dolbyDigitalAudioStream;
public $dolbyDigitalFmp4Muxing;
public $H265_FMP4_SEGMENTS_PATH_FORMAT = "video/h265/fmp4/%dp_%d";
public $DOLBY_DIGITAL_FMP4_SEGMENTS_PATH = "audio/dolby-digital/fmp4";
public function __construct(Encoding $encoding)
{
$this->encoding = $encoding;
$this->renditions = [
new Rendition(540, 600000),
new Rendition(720, 2400000),
new Rendition(1080, 4500000),
new Rendition(2160, 11600000),
];
}
}
class Vp9AndVorbisEncodingTracking
{
public $encoding;
public $renditions;
public $vp9WebmMuxing;
public $vorbisWebmMuxing;
public $VP9_WEBM_SEGMENTS_PATH_FORMAT = "video/vp9/webm/%dp_%d";
public $VORBIS_WEBM_SEGMENTS_PATH = "audio/vorbis/webm";
public function __construct(Encoding $encoding)
{
$this->encoding = $encoding;
$this->renditions = [
new Rendition(540, 600000),
new Rendition(720, 2400000),
new Rendition(1080, 4500000),
new Rendition(2160, 11600000),
];
}
}
try {
$bitmovinApi = new BitmovinApi(
Configuration::create()
->apiKey($configProvider->getBitmovinApiKey())
// uncomment the following line if you are working with a multi-tenant account
// ->tenantOrgId($configProvider->getBitmovinTenantOrgId())
->logger(new ConsoleLogger())
);
$input = createHttpInput($configProvider->getHttpInputHost());
$inputFilePath = $configProvider->getHttpInputFilePath();
$output = createS3Output(
$configProvider->getS3OutputBucketName(),
$configProvider->getS3OutputAccessKey(),
$configProvider->getS3OutputSecretKey()
);
$h264AndAacEncodingTracking = createH264AndAacEncoding(
$input,
$inputFilePath,
$output
);
$h265AndDolbyDigitalEncodingTracking = createH265AndDolbyDigitalEncoding(
$input,
$inputFilePath,
$output
);
$vp9AndVorbisEncodingTracking = createVp9AndVorbisEncoding(
$input,
$inputFilePath,
$output
);
executeEncodings([
$h264AndAacEncodingTracking->encoding,
$h265AndDolbyDigitalEncodingTracking->encoding,
$vp9AndVorbisEncodingTracking->encoding,
]);
$dashManifest = createDashManifestWithRepresentations(
$output,
$h264AndAacEncodingTracking,
$h265AndDolbyDigitalEncodingTracking,
$vp9AndVorbisEncodingTracking
);
executeDashManifest($dashManifest);
$hlsManifest = createHlsManifestWithRepresentations(
$output,
$h264AndAacEncodingTracking,
$h265AndDolbyDigitalEncodingTracking
);
executeHlsManifest($hlsManifest);
} catch (Exception $exception) {
echo $exception;
}
/**
* Creates the encoding with H264 codec/TS muxing, H264 codec/CMAF muxing, AAC codec/fMP4 muxing
*
* @param Input $input the input that should be used
* @param string $inputFilePath the path to the input file
* @param Output $output the output that should be used
* @return H264AndAacEncodingTracking the tracking information for the encoding
* @throws BitmovinApiException
* @throws Exception
*/
function createH264AndAacEncoding(
Input $input,
string $inputFilePath,
Output $output
) {
$encoding = createEncoding(
"H.264 Encoding",
"H.264 -> TS muxing, H.264 -> CMAF muxing, AAC -> fMP4 muxing, AAC -> TS muxing"
);
$encodingTracking = new H264AndAacEncodingTracking($encoding);
foreach ($encodingTracking->renditions as $rendition) {
$videoConfiguration = createH264Config(
$rendition->height,
$rendition->bitrate
);
$videoStream = createStream(
$encoding,
$input,
$inputFilePath,
$videoConfiguration
);
$cmafMuxingOutputPath = sprintf(
$encodingTracking->H264_CMAF_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
);
$tsMuxingOutputPath = sprintf(
$encodingTracking->H264_TS_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
);
$cmafMuxing = createCmafMuxing(
$encoding,
$output,
$cmafMuxingOutputPath,
$videoStream
);
$tsMuxing = createTsMuxing(
$encoding,
$output,
$tsMuxingOutputPath,
$videoStream
);
$encodingTracking->h264VideoStreams[getKey($rendition)] = $videoStream;
$encodingTracking->h264CmafMuxings[getKey($rendition)] = $cmafMuxing;
$encodingTracking->h264TsMuxings[getKey($rendition)] = $tsMuxing;
}
// Add an AAC audio stream to the encoding
$aacConfig = createAacConfig();
$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,
$encodingTracking->AAC_FMP4_SEGMENTS_PATH,
$aacAudioStream
);
$encodingTracking->aacTsMuxing = createTsMuxing(
$encoding,
$output,
$encodingTracking->AAC_TS_SEGMENTS_PATH,
$aacAudioStream
);
return $encodingTracking;
}
/**
* Creates the encoding with H265 codec/fMP4 muxing, Dolby Digital codec/fMP4 muxing
*
* @param HttpInput $input the input that should be used
* @param string $inputFilePath the path to the input file
* @param Output $output the output that should be used
* @return H265AndDolbyDigitalEncodingTracking the tracking information for the encoding
* @throws BitmovinApiException
*/
function createH265AndDolbyDigitalEncoding(
HttpInput $input,
string $inputFilePath,
Output $output
) {
$encoding = createEncoding(
"H.265 Encoding",
"H.265 -> fMP4 muxing, Dolby Digital -> fMP4 muxing"
);
$encodingTracking = new H265AndDolbyDigitalEncodingTracking($encoding);
// Add streams and muxings for h265 encoding
foreach ($encodingTracking->renditions as $rendition) {
$videoConfiguration = createH265Config(
$rendition->height,
$rendition->bitrate
);
$videoStream = createStream(
$encoding,
$input,
$inputFilePath,
$videoConfiguration
);
$fmp4Muxing = createFmp4Muxing(
$encoding,
$output,
sprintf(
$encodingTracking->H265_FMP4_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
),
$videoStream
);
$encodingTracking->h265VideoStreams[getKey($rendition)] = $videoStream;
$encodingTracking->h265Fmp4Muxings[getKey($rendition)] = $fmp4Muxing;
}
$dolbyDigitalConfig = createDolbyDigitalConfig();
$encodingTracking->dolbyDigitalAudioStream = createStream(
$encoding,
$input,
$inputFilePath,
$dolbyDigitalConfig
);
// Create a fMP4 muxing with the Dolby Digital stream
$encodingTracking->dolbyDigitalFmp4Muxing = createFmp4Muxing(
$encoding,
$output,
$encodingTracking->DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
$encodingTracking->dolbyDigitalAudioStream
);
return $encodingTracking;
}
/**
* Created the encoding with VP9 codec/WebM muxing, Vorbis codec / WebM muxing
*
* @param HttpInput $input the input that should be used
* @param string $inputFilePath the path to the input file
* @param Output $output the output that should be used
* @return Vp9AndVorbisEncodingTracking the tracking information for the encoding
* @throws BitmovinApiException
*/
function createVp9AndVorbisEncoding(
HttpInput $input,
string $inputFilePath,
Output $output
) {
$encoding = createEncoding(
"VP9/Vorbis Encoding",
"VP9 -> WebM muxing, Vorbis -> WebM muxing"
);
$encodingTracking = new Vp9AndVorbisEncodingTracking($encoding);
// Create video streams and add webm muxings to the VP9 encoding
foreach ($encodingTracking->renditions as $rendition) {
$vp9Config = createVp9Config($rendition->height, $rendition->bitrate);
$vp9VideoStream = createStream(
$encoding,
$input,
$inputFilePath,
$vp9Config
);
$encodingTracking->vp9WebmMuxing[getKey($rendition)] = createWebmMuxing(
$encoding,
$output,
sprintf(
$encodingTracking->VP9_WEBM_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
),
$vp9VideoStream
);
}
// Create Vorbis audio configuration
$vorbisAudioConfiguration = createVorbisConfig();
$vorbisAudioStream = createStream(
$encoding,
$input,
$inputFilePath,
$vorbisAudioConfiguration
);
// Create a WebM muxing with the Vorbis audio stream
$encodingTracking->vorbisWebmMuxing = createWebmMuxing(
$encoding,
$output,
$encodingTracking->VORBIS_WEBM_SEGMENTS_PATH,
$vorbisAudioStream
);
return $encodingTracking;
}
/**
* Creates the DASH manifest with all the representations.
*
* @param Output $output the output that should be used
* @param H264AndAacEncodingTracking $h264AndAacEncodingTracking the tracking information for the H264/AAC encoding
* @param H265AndDolbyDigitalEncodingTracking $h265AndDolbyDigitalEncodingTracking the tracking information for the H265 encoding
* @param Vp9AndVorbisEncodingTracking $vp9AndVorbisEncodingTracking the tracking information for the VP9/Vorbis encoding
* @return DashManifest the created DASH manifest
* @throws BitmovinApiException
*/
function createDashManifestWithRepresentations(
Output $output,
H264AndAacEncodingTracking $h264AndAacEncodingTracking,
H265AndDolbyDigitalEncodingTracking $h265AndDolbyDigitalEncodingTracking,
Vp9AndVorbisEncodingTracking $vp9AndVorbisEncodingTracking
) {
global $bitmovinApi;
$dashManifest = createDashManifest(
"stream.mpd",
DashProfile::LIVE(),
$output,
"/"
);
$period = $bitmovinApi->encoding->manifests->dash->periods->create(
$dashManifest->id,
new Period()
);
$videoAdaptationSetVp9 = $bitmovinApi->encoding->manifests->dash->periods->adaptationsets->video->create(
$dashManifest->id,
$period->id,
new VideoAdaptationSet()
);
$videoAdaptationSetH265 = $bitmovinApi->encoding->manifests->dash->periods->adaptationsets->video->create(
$dashManifest->id,
$period->id,
new VideoAdaptationSet()
);
$videoAdaptationSetH264 = $bitmovinApi->encoding->manifests->dash->periods->adaptationsets->video->create(
$dashManifest->id,
$period->id,
new VideoAdaptationSet()
);
$vorbisAudioAdaptationSet = createAudioAdaptionSet(
$dashManifest,
$period,
"en"
);
$dolbyDigitalAudioAdaptationSet = createAudioAdaptionSet(
$dashManifest,
$period,
"en"
);
$aacAudioAdaptationSet = createAudioAdaptionSet(
$dashManifest,
$period,
"en"
);
// Add representations to VP9 adaptation set
// Add VP9 WEBM muxing to VP9 adaptation set
foreach ($vp9AndVorbisEncodingTracking->renditions as $rendition) {
createDashWebmRepresentation(
$vp9AndVorbisEncodingTracking->encoding,
$vp9AndVorbisEncodingTracking->vp9WebmMuxing[getKey($rendition)],
$dashManifest,
$period,
sprintf(
$vp9AndVorbisEncodingTracking->VP9_WEBM_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
),
$videoAdaptationSetVp9->id
);
}
// Add VORBIS WEBM muxing to VORBIS audio adaptation set
createDashWebmRepresentation(
$vp9AndVorbisEncodingTracking->encoding,
$vp9AndVorbisEncodingTracking->vorbisWebmMuxing,
$dashManifest,
$period,
$vp9AndVorbisEncodingTracking->VORBIS_WEBM_SEGMENTS_PATH,
$vorbisAudioAdaptationSet->id
);
// Add representations to H265 adaptation set
// Add H265 FMP4 muxing to H265 video adaptation set
foreach ($h265AndDolbyDigitalEncodingTracking->renditions as $rendition) {
createDashFmp4Representation(
$h265AndDolbyDigitalEncodingTracking->encoding,
$h265AndDolbyDigitalEncodingTracking->h265Fmp4Muxings[getKey($rendition)],
$dashManifest,
$period,
sprintf(
$h265AndDolbyDigitalEncodingTracking->H265_FMP4_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
),
$videoAdaptationSetH265->id
);
}
// Add Dolby Digital FMP4 muxing to AAC audio adaptation set
createDashFmp4Representation(
$h265AndDolbyDigitalEncodingTracking->encoding,
$h265AndDolbyDigitalEncodingTracking->dolbyDigitalFmp4Muxing,
$dashManifest,
$period,
$h265AndDolbyDigitalEncodingTracking->DOLBY_DIGITAL_FMP4_SEGMENTS_PATH,
$dolbyDigitalAudioAdaptationSet->id
);
// Add representations to H264 adaptation set
// Add H264 CMAF muxing to H264 video adaptation set
foreach ($h264AndAacEncodingTracking->renditions as $rendition) {
createDashCmafRepresentation(
$h264AndAacEncodingTracking->encoding,
$h264AndAacEncodingTracking->h264CmafMuxings[getKey($rendition)],
$dashManifest,
$period,
sprintf(
$h264AndAacEncodingTracking->H264_CMAF_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
),
$videoAdaptationSetH264->id
);
}
// Add AAC FMP4 muxing to AAC audio adaptation set
createDashFmp4Representation(
$h264AndAacEncodingTracking->encoding,
$h264AndAacEncodingTracking->aacFmp4Muxing,
$dashManifest,
$period,
$h264AndAacEncodingTracking->AAC_FMP4_SEGMENTS_PATH,
$aacAudioAdaptationSet->id
);
return $dashManifest;
}
/**
* Creates the HLS manifest master playlist with the different sub playlists
*
* @param Output $output the output that should be used
* @param H264AndAacEncodingTracking $h264AndAacEncodingTracking the tracking information for the H264/AAC encoding
* @param H265AndDolbyDigitalEncodingTracking $h265AndDolbyDigitalEncodingTracking the tracking information for the H265 encoding
* @return HlsManifest the created HLS manifest
* @throws BitmovinApiException
*/
function createHlsManifestWithRepresentations(
Output $output,
H264AndAacEncodingTracking $h264AndAacEncodingTracking,
H265AndDolbyDigitalEncodingTracking $h265AndDolbyDigitalEncodingTracking
) {
$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
foreach ($h265AndDolbyDigitalEncodingTracking->renditions as $rendition) {
createVideoStreamPlaylist(
$h265AndDolbyDigitalEncodingTracking->encoding,
$hlsManifest,
$h265AndDolbyDigitalEncodingTracking->h265Fmp4Muxings[getKey($rendition)],
$h265AndDolbyDigitalEncodingTracking->h265VideoStreams[getKey($rendition)],
sprintf(
"video_h265_%dp_%d.m3u8",
$rendition->height,
$rendition->bitrate
),
sprintf(
$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
foreach ($h264AndAacEncodingTracking->renditions as $rendition) {
createVideoStreamPlaylist(
$h264AndAacEncodingTracking->encoding,
$hlsManifest,
$h264AndAacEncodingTracking->h264TsMuxings[getKey($rendition)],
$h264AndAacEncodingTracking->h264VideoStreams[getKey($rendition)],
sprintf(
"video_h264_%dp_%d.m3u8",
$rendition->height,
$rendition->bitrate
),
sprintf(
$h264AndAacEncodingTracking->H264_TS_SEGMENTS_PATH_FORMAT,
$rendition->height,
$rendition->bitrate
),
HLS_AUDIO_GROUP_AAC_TS
);
}
return $hlsManifest;
}
/**
* 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[] $encodings The encodings to be started
* @throws BitmovinApiException
* @throws Exception
*/
function executeEncodings(array $encodings)
{
global $bitmovinApi;
foreach ($encodings as $encoding) {
$bitmovinApi->encoding->encodings->start($encoding->id);
}
do {
sleep(5);
$allFinished = true;
foreach ($encodings as $encoding) {
$task = $bitmovinApi->encoding->encodings->status($encoding->id);
echo 'Encoding status is ' .
$task->status .
' (progress: ' .
$task->progress .
' %)' .
PHP_EOL;
if ($task->status == Status::ERROR()) {
logTaskErrors($task);
throw new Exception('Encoding failed');
}
if ($task->status != Status::FINISHED()) {
$allFinished = false;
}
}
} while (!$allFinished);
echo 'Encoding finished successfully' . PHP_EOL;
}
/**
* 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 DashManifest $dashManifest The dash manifest to be started
* @throws BitmovinApiException
* @throws Exception
*/
function executeDashManifest(DashManifest $dashManifest)
{
global $bitmovinApi;
$bitmovinApi->encoding->manifests->dash->start($dashManifest->id);
do {
sleep(5);
$task = $bitmovinApi->encoding->manifests->dash->status(
$dashManifest->id
);
} while (
$task->status != Status::FINISHED() &&
$task->status != Status::ERROR()
);
if ($task->status == Status::ERROR()) {
logTaskErrors($task);
throw new Exception('DASH manifest failed');
}
echo 'DASH manifest finished successfully' . PHP_EOL;
}
/**
* 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 HlsManifest $hlsManifest The dash manifest to be started
* @throws BitmovinApiException
* @throws Exception
*/
function executeHlsManifest(HlsManifest $hlsManifest)
{
global $bitmovinApi;
$bitmovinApi->encoding->manifests->hls->start($hlsManifest->id);
do {
sleep(5);
$task = $bitmovinApi->encoding->manifests->hls->status(
$hlsManifest->id
);
} while (
$task->status != Status::FINISHED() &&
$task->status != Status::ERROR()
);
if ($task->status == Status::ERROR()) {
logTaskErrors($task);
throw new Exception('HLS manifest failed');
}
echo 'HLS manifest finished successfully' . PHP_EOL;
}
/**
* 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 $encoding The encoding to add the muxing to
* @param Output $output The output that should be used for the muxing to write the segments to
* @param string $outputPath The output path where the fragmented segments will be written to
* @param Stream $stream The stream that is associated with the muxing
* @return CmafMuxing
* @throws Exception
*/
function createCmafMuxing(
Encoding $encoding,
Output $output,
string $outputPath,
Stream $stream
) {
global $bitmovinApi;
$muxingStream = new MuxingStream();
$muxingStream->streamId($stream->id);
$muxing = new CmafMuxing();
$muxing->outputs([buildEncodingOutput($output, $outputPath)]);
$muxing->streams([$muxingStream]);
$muxing->segmentLength(4.0);
return $bitmovinApi->encoding->encodings->muxings->cmaf->create(
$encoding->id,
$muxing
);
}
/**
* Creates an fMP4 muxing.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsMuxingsFmp4ByEncodingId
*
* @param Encoding $encoding The encoding to add the fMP4 muxing to
* @param Output $output The output that should be used for the muxing to write the segments to
* @param string $outputPath The output path where the fragments will be written to
* @param Stream $stream An array of streams to be added to the muxing
* @return Fmp4Muxing
* @throws BitmovinApiException
* @throws Exception
*/
function createFmp4Muxing(
Encoding $encoding,
Output $output,
string $outputPath,
Stream $stream
) {
global $bitmovinApi;
$muxingStream = new MuxingStream();
$muxingStream->streamId($stream->id);
$muxing = new Fmp4Muxing();
$muxing->outputs([buildEncodingOutput($output, $outputPath)]);
$muxing->streams([$muxingStream]);
$muxing->segmentLength(4.0);
return $bitmovinApi->encoding->encodings->muxings->fmp4->create(
$encoding->id,
$muxing
);
}
/**
* Creates a TS muxing.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsMuxingsTsByEncodingId
*
* @param Encoding $encoding The encoding to add the muxing to
* @param Output $output The output that should be used for the muxing to write the segments to
* @param string $outputPath The output path where the fragments will be written to
* @param Stream $stream An array of streams to be added to the muxing
* @return TsMuxing
* @throws BitmovinApiException
* @throws Exception
*/
function createTsMuxing(
Encoding $encoding,
Output $output,
string $outputPath,
Stream $stream
) {
global $bitmovinApi;
$muxingStream = new MuxingStream();
$muxingStream->streamId($stream->id);
$muxing = new TsMuxing();
$muxing->outputs([buildEncodingOutput($output, $outputPath)]);
$muxing->streams([$muxingStream]);
$muxing->segmentLength(4.0);
return $bitmovinApi->encoding->encodings->muxings->ts->create(
$encoding->id,
$muxing
);
}
/**
* Creates a progressive WebM muxing.
*
* <p>API endpoint:
* https://bitmovin.com/docs/encoding/api-reference/sections/encodings#/Encoding/PostEncodingEncodingsMuxingsTsByEncodingId
*
* @param Encoding $encoding The encoding to add the muxing to
* @param Output $output The output that should be used for the muxing to write the segments to
* @param string $outputPath The output path where the fragments will be written to
* @param Stream $stream An array of streams to be added to the muxing
* @return WebmMuxing
* @throws BitmovinApiException
* @throws Exception
*/
function createWebmMuxing(
Encoding $encoding,
Output $output,
string $outputPath,
Stream $stream
) {
global $bitmovinApi;
$muxingStream = new MuxingStream();
$muxingStream->streamId($stream->id);
$muxing = new WebmMuxing();
$muxing->outputs([buildEncodingOutput($output, $outputPath)]);
$muxing->streams([$muxingStream]);
$muxing->segmentLength(4.0);
return $bitmovinApi->encoding->encodings->muxings->webm->create(
$encoding->id,
$muxing
);
}
/**
* 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