HTTP Live Streaming (HLS) packaging allows you to generate a master playlist for multiple video streams at different bitrates, caption streams, and audio streams. This way, the video content can be delivered as a series of small media files that are typically in the TS format. Each file contains a short video, and multiple versions of the video at different bitrates and resolutions are generated. This ensures smooth playback of streaming media based on the network conditions of users. To perform HLS packaging, you need to create an HLS packaging workflow. Then, you can specify the ID of the HLS packaging workflow when you call the AddMedia operation to use the workflow to process videos.
Procedure
Create an HLS packaging workflow. When you create an HLS packaging workflow, take note of the objects described in the following table. For more information about the API operation that is used to create a workflow, see AddMediaWorkflow.
Object
Description
Topology
The business process that is defined by using a directed acyclic graph (DAG).
Activity
The nodes that constitute the topology. When you create an HLS packaging workflow, pay attention to the activities described in the following table.
Dependencies
The edges in the topology. The edges indicate the dependencies among activities.
Table 1: HLS packaging activities Activity
Description
Dependency
Specifies the location for storing the output master playlist.
Supported parent node: Start
Supported child nodes: SubtitleGroup, AudioGroup, and Transcode for video streams only
Specifies the ID and language of each caption group.
Supported parent node: PackageConfig
Supported child node: Transcode for caption streams only
Specifies the ID and language of each audio group.
Supported parent node: PackageConfig
Supported child node: Transcode for audio streams only
Extracts video, audio, or caption streams.
Supported parent node: PackageConfig, SubtitleGroup, and AudioGroup.
Supported child node: GenerateMasterPlayList
Specifies the multi-bitrate video streams, audio streams, and caption streams to generate a master playlist.
Supported parent node: Transcode
Supported child node: Report
Call the AddMedia operation to trigger the HLS packaging workflow. Take note of the following items:
You must specify the ID of the HLS packaging workflow when you call the AddMedia operation.
For caption stream extraction, you can configure the OverrideParams parameter to override the URL of the caption file specified by the WebVTTSubtitleURL parameter in the Transcode activity. Only Web Video Text Tracks (WebVTT) caption files are supported.
Set the TriggerMode parameter of the workflow to NotInAuto.
Scenario
In this example, two video streams and three audio streams are extracted from a source file in the MXF format. The source file can also be in the MP4, FLV, M3U8, or TS format. Then, two WebVTT caption streams are extracted, and a master playlist is generated for the video streams, audio streams, and caption streams.
Specify the output location and name of the master playlist
Specify the Object Storage Service (OSS) bucket in which the master playlist file is to be stored.
Specify the location of the master playlist.
Specify the name of the master playlist.
Sample code:
{ "Parameters" : { "Output" : "{\"Bucket\": \"processedmediafile\",\"Location\": \"oss-cn-hangzhou\",\"MasterPlayListName\": \"{MediaId}/{RunId}/hls/master.m3u8\"}" }, "Type" : "PackageConfig" }
Output: the output location and name of the master playlist. For more information, see the "PackageConfig" section of the Methods supported for media workflows topic.
Type: the activity type. In this example, this parameter is set to PackageConfig.
Specify an audio group
Specify the ID of the audio group. The two audio streams belong to the same audio group. Sample code:
{
"Parameters" : {
"GroupId" : "audios"
},
"Type" : "AudioGroup"
}
GroupId: the ID of the audio group. In this example, this parameter is set to audios.
Type: the activity type. In this example, this parameter is set to AudioGroup.
Extract audio streams
To extract audio streams from the MXF source file, you must remove the video streams.
Configurations of output audio streams:
Codec: AAC
Sample rate: 48000 Hz
Format: Stereo
Sample code:
{ "Name" : "audio-extract-1", "Parameters" : { "Outputs" : "[{\"TemplateId\":\"S00000001-100020\",\"AudioStreamMap\":\"0:a:0\",\"Video\":{\"Remove\":\"true\"}}]", "ExtXMedia" : "{\"URI\": \"sd/audio-en.m3u8\",\"Name\": \"audio-en\",\"Language\": \"en-US\"}" }
TemplateId: the ID of the template to be used to extract the audio stream. In this example, the template whose ID is S00000001-100020 is used. This template extracts an audio file in the M3U8 format. The bitrate of the audio is 80 Kbit/s. For more information, see Preset template details.
AudioStreamMap: the sequence number of the audio stream. For more information, see the "Output" section of the Parameter details topic.
Remove: specifies whether to remove video streams from the output. For more information, see Parameter details.
ExtXMedia: the information about the media playlist. The URI parameter specifies the name of the media playlist. For more information, see the "ExtXMedia" section of the Parameter details topic.
Type: the activity type. In this example, this parameter is set to Transcode.
Extract video streams
To extract video streams from the MXF source file, you must remove the audio streams. Sample code:
{
"Name" : "video-extract",
"Parameters" : {
"Outputs" : "[{\"TemplateId\":\"1fe5393bdb7b2b883f0a0fc91e81****\",\"Audio\":{\"Remove\":\"true\"}}]",
"MultiBitrateVideoStream" : "{\"URI\": \"sd/video1.m3u8\"}"
},
"Type" : "Transcode"
}
In this example, a custom transcoding template whose ID is 1fe5393bdb7b2b883f0a0fc91e81**** is used. You can perform the following steps to create a custom transcoding template: Log on to the ApsaraVideo Media Processing (MPS) console. In the left-side navigation pane, choose . On the Encoding Template page, click Create Template on the Custom Template tab to create a custom transcoding template. Configure the transcoding parameters based on the following video configurations:
Codec: H.264
Resolution: 384 × 216
Profile: Main
Bitrate: 240 Kbit/s
FPS: 25
Pixel format: YUV420P Max GOP size: 1 Segment length: 4 seconds
Output format: M3U8
Remove: specifies whether to remove the audio streams from the output. For more information, see Parameter details.
MultiBitrateVIdeoStream: the information about the media playlist. The URI parameter specifies the name of the media playlist.Parameter details
Type: the activity type. In this example, this parameter is set to Transcode.
Specify a caption group
Specify the ID of the caption group. The two caption streams belong to the same caption group. Sample code:
{
"Parameters" : {
"GroupId" : "subtitles"
},
"Type" : "SubtitleGroup"
}
GroupId: the ID of the caption group. In this example, this parameter is set to subtitles.
Type: the activity type. In this example, this parameter is set to SubtitleGroup.
Extract caption streams
Upload WebVTT caption files to OSS. Sample code:
{
"Name" : "subtitle-extract-1",
"Parameters" : {
"WebVTTSubtitleURL" : "http://example-bucket-****.oss-cn-hangzhou.aliyun-inc.com/ShawshankRedemption****.vtt",
"ExtXMedia" : "{\"URI\": \"zh/subtitle1-cn.m3u8\",\"Name\": \"subtitle-cn\",\"Language\": \"cn\"}"
},
"Type" : "Transcode"
}
WebVTTSubtitleURL
: the URL of the caption file. You can configure the OverrideParams parameter when you call the AddMedia operation to override the URL of the caption file. For more information, see the OverrideParams parameter in the "Request parameters" section of the AddMedia topic.ExtXMedia: the information about the media playlist. The URI parameter specifies the name of the media playlist. For more information, see the "ExtXMedia" section of the Parameter details topic.
Type: the activity type. In this example, this parameter is set to Transcode.
Generate a master playlist
Generate a master playlist based on the extracted audio, video, and caption streams. Sample code:
{
"Parameters" : {
"MasterPlayList" : "{\"MultiBitrateVideoStreams\": [{\"RefActivityName\": \"video-extract\",\"ExtXStreamInfo\": {\"BandWidth\": \"1110000\",\"Audio\": \"audios\",\"Subtitles\": \"subtitles\"}}]}"
},
"Type" : "GenerateMasterPlayList"
}
MasterPlayList: the information about the master playlist. For more information, see the "MasterPlayList" section of the Parameter details topic.
MultiBitrateVideoStreams: the multi-bitrate video streams. For more information, see the "MasterPlayList" section of the Parameter details topic.
RefActivityName: the name of the activity that extracts video streams.
ExtXStreamInfo: the attributes of the multi-bitrate video streams. The Audio parameter specifies the audio group, and the Subtitles parameter specifies the caption group. For more information, see the "ExtXStreamInfo" section of the Parameter details topic.
Type: the activity type. In this example, this parameter is set to GenerateMasterPlayList.
Sample code of the complete topology:
{
"Activities" : {
"package-node" : {
"Name" : "package-node",
"Parameters" : {
"Output" : "{\"Bucket\": \"processedmediafile\",\"Location\": \"oss-cn-hangzhou\",\"MasterPlayListName\": \"{MediaId}/{RunId}/hls/master.m3u8\"}"
},
"Type" : "PackageConfig"
},
"audioGroupNode" : {
"Name" : "audioGroupNode",
"Parameters" : {
"GroupId" : "audios"
},
"Type" : "AudioGroup"
},
"subtitleGroupNode" : {
"Name" : "subtitleGroupNode",
"Parameters" : {
"GroupId" : "subtitles"
},
"Type" : "SubtitleGroup"
},
"video-extract-1" : {
"Name" : "video-extract-1",
"Parameters" : {
"Outputs" : "[{\"TemplateId\":\"1fe5393bdb7b2b883f0a0fc91e81344a\",\"Audio\":{\"Remove\":\"true\"}}]",
"MultiBitrateVideoStream" : "{\"URI\": \"sd/video1.m3u8\"}"
},
"Type" : "Transcode"
},
"video-extract-2" : {
"Name" : "video-extract-1",
"Parameters" : {
"Outputs" : "[{\"TemplateId\":\"1fe5393bdb7b2b883f0a0fc91e81344b\",\"Audio\":{\"Remove\":\"true\"}}]",
"MultiBitrateVideoStream" : "{\"URI\": \"sd/video2.m3u8\"}"
},
"Type" : "Transcode"
},
"audio-extract-1" : {
"Name" : "audio-extract-1",
"Parameters" : {
"Outputs" : "[{\"TemplateId\":\"S00000001-100020\",\"AudioStreamMap\":\"0:a:0\"}]",
"ExtXMedia" : "{\"URI\": \"sd/audio-en-1.m3u8\",\"Name\": \"audio-en\",\"Language\": \"en-US\"}"
},
"Type" : "Transcode"
},
"audio-extract-2" : {
"Name" : "audio-extract-2",
"Parameters" : {
"Outputs" : "[{\"TemplateId\":\"S00000001-100020\",\"AudioStreamMap\":\"0:a:1\"}]",
"ExtXMedia" : "{\"URI\": \"sd/audio-cn.m3u8\",\"Name\": \"audio-cn\",\"Language\": \"cn\"}"
},
"Type" : "Transcode"
},
"audio-extract-3" : {
"Name" : "audio-extract-3",
"Parameters" : {
"Outputs" : "[{\"TemplateId\":\"S00000001-100020\",\"AudioStreamMap\":\"0:a:2\"}]",
"ExtXMedia" : "{\"URI\": \"sd/audio-de.m3u8\",\"Name\": \"audio-de\",\"Language\": \"de\"}"
},
"Type" : "Transcode"
},
"subtitle-extract-1" : {
"Name" : "subtitle-extract-1",
"Parameters" : {
"WebVTTSubtitleURL" : "http://example-bucket-****.oss-test.aliyun-inc.com/1****.vtt",
"ExtXMedia" : "{\"URI\": \"zh/subtitle1-cn.m3u8\",\"Name\": \"subtitle-cn\",\"Language\": \"cn\"}"
},
"Type" : "Transcode"
},
"subtitle-extract-2" : {
"Name" : "subtitle-extract-2",
"Parameters" : {
"WebVTTSubtitleURL" : "http://example-bucket-****.oss-cn-hangzhou.aliyun-inc.com/ShawshankRedemption****.vtt",
"ExtXMedia" : "{\"URI\": \"zh/subtitle1-en.m3u8\",\"Name\": \"subtitle-en\",\"Language\": \"en-US\"}"
},
"Type" : "Transcode"
},
"masterPlayListGenerate" : {
"Name" : "masterPlayListGenerate",
"Parameters" : {
"MasterPlayList" : "{\"MultiBitrateVideoStreams\": [{\"RefActivityName\": \"video-extract-1\",\"ExtXStreamInfo\": {\"BandWidth\": \"1110000\",\"Audio\": \"audios\",\"Subtitles\": \"subtitles\"}}, {\"RefActivityName\": \"video-extract-2\",\"ExtXStreamInfo\": {\"BandWidth\": \"5000000\",\"Audio\": \"audios\",\"Subtitles\":\"subtitles\"}}]}"
},
"Type" : "GenerateMasterPlayList"
},
"activityEnd" : {
"Name" : "activityEnd",
"Parameters" : {
"PublishType" : "Manual"
},
"Type" : "Report"
},
"activityStart" : {
"Name" : "activityStart",
"Parameters" : {
"PipelineId" : "900ededca77641ecbecd4f44cc3a2965",
"Role" : "AliyunMTSDefaultRole",
"InputFile" : "{\"Bucket\":\"videouploaded\",\"Location\":\"oss-cn-hangzhou\",\"ObjectPrefix\":\"uploaded/\"}"
},
"Type" : "Start"
}
},
"Dependencies" : {
"video-extract-1" : [ "masterPlayListGenerate" ],
"video-extract-2" : [ "masterPlayListGenerate" ],
"audio-extract-1" : [ "masterPlayListGenerate" ],
"audio-extract-2" : [ "masterPlayListGenerate" ],
"audio-extract-3" : [ "masterPlayListGenerate" ],
"subtitle-extract-1" : [ "masterPlayListGenerate" ],
"subtitle-extract-2" : [ "masterPlayListGenerate" ],
"package-node" : [ "video-extract-1", "video-extract-2","subtitleGroupNode", "audioGroupNode" ],
"audioGroupNode" : [ "audio-extract-1", "audio-extract-2","audio-extract-3"],
"subtitleGroupNode" : [ "subtitle-extract-1", "subtitle-extract-2" ],
"masterPlayListGenerate" : [ "activityEnd" ],
"activityEnd" : [ ],
"activityStart" : [ "package-node" ]
}
}
Sample code
Create an HLS packaging workflow.
Add a media file.