Live transcoding generates a standard HLS m3u8 playlist for a video, enabling immediate playback without pre-generating TS files. The video is dynamically transcoded during playback, which significantly reduces wait times and lowers transcoding and storage costs through on-demand processing.
Introduction
Unlike video transcoding, which requires the entire video to be transcoded before playback, live transcoding starts playback immediately after the source video is uploaded by transcoding only the required segments on the fly. Live transcoding provides the following benefits:
-
Transcode during playback, which eliminates waiting time.
-
Optimized transcoding enables fast start-up and seeking, which provides a playback experience similar to local playback.
-
Transcoding does not occur if the video is not played. Transcoded files can be deleted and regenerated on demand. This significantly saves transcoding and storage costs.
-
Supports dozens of transcoding parameters for a high degree of customization.
-
Provides high compatibility with support for over 300 audio and video formats.
Scenarios
-
Network drives: After a user uploads a video, the client can select the optimal resolution for playback based on network conditions. This ensures real-time playback and device compatibility. Videos that are not frequently accessed are not transcoded if they are not played, which reduces storage costs.
-
Video previews in chat applications: In instant messaging or social media applications, a video is playable immediately after it is sent. This improves real-time performance. Videos in chat history can be periodically deleted and remain playable on demand.
-
Online forums and blogs: When you share videos on forums and blogs, live transcoding allows users to watch videos without waiting for transcoding. This ensures smooth playback and high definition.
Features
The following table summarizes the key features of live transcoding.
|
Feature |
Description |
|
Standardization |
|
|
Low cost |
|
|
High efficiency |
|
Supported audio and video formats
Live transcoding supports over 300 audio and video formats. The following table lists some common supported formats.
|
Input video format |
All mainstream formats, such as AVI, MOV, FLV, MKV, WebM, MPEG, WMV, RM, VOB, and TS. |
|
Input audio format |
All mainstream formats, such as MP3, WAV, AAC, FLAC, and WMA. |
|
Output container format |
TS |
Prerequisites
-
The Intelligent Media Management (IMM) service is activated. For more information, see Activate products.
-
An IMM project is attached. For more information about how to attach a project in the Object Storage Service (OSS) console, see Quick Start. For more information about how to attach a project using an API, see AttachOSSBucket - Attach an Object Storage Service bucket.
-
You have the required permissions for IMM to perform processing. For more information, see Permissions.
Usage notes
-
Anonymous access will be denied.
-
For more information, see Live transcoding and Generate a live transcoding playlist.
Parameter description
Operation: hls/m3u8
The following table describes the parameters.
|
Parameter |
Type |
Required |
Description |
|
ss |
int |
No |
The start time for generating the playlist. Unit: milliseconds (ms). Valid values:
Note
Use this parameter with the t parameter to generate a playlist for a specific part of the source video. |
|
t |
int |
No |
The transcoding duration for the playlist. Unit: ms. Valid values:
Note
If the specified time exceeds the end of the source video, the default value is used. |
|
ta |
int |
No |
The number of TS files to pre-transcode when live transcoding is triggered. By default, a 2-minute video segment is pre-transcoded. Example: If st is 10000, the default value of ta is 12. You can specify this parameter to control the number of asynchronous pre-transcoded segments. Valid values: [10,30]. |
|
st |
int |
No |
The duration of a single TS file segment. Unit: ms. Default: 10000. Valid values: [5000,15000]. |
|
initd |
int |
No |
The duration of the initial transcoding when the playlist is generated. Unit: ms. Default: 30000.
Note
This parameter is mainly used to reduce the waiting time for the first playback and improve the user experience. To replace a traditional VOD service, you can try to initially transcode the entire video. |
|
vcodec |
string |
No |
The video codec (encoding format). Valid values:
|
|
fps |
float |
No |
The video frame rate. Default: same as the source video. |
|
fpsopt |
int |
No |
The video frame rate option. Valid values:
Note
This parameter must be set together with the fps parameter. |
|
pixfmt |
string |
No |
The pixel format. Default: same as the source video. Valid values:
|
|
s |
string |
No |
The resolution of the output video, in
|
|
sopt |
string |
No |
The resolution option. Valid values:
|
|
scaletype |
string |
No |
The scaling mode. Valid values:
Note
This parameter must be set together with the s parameter. |
|
arotate |
int |
No |
Adaptive resolution orientation. Valid values:
|
|
vb |
int |
No |
The video stream bitrate. Unit: bit/s. If you do not specify crf or vb, the default value of crf is 23. If you set vbopt, the vb parameter is required. |
|
vbopt |
int |
No |
The video bitrate option. Valid values:
|
|
crf |
float |
No |
The constant quality mode. Mutually exclusive with the vb parameter. Valid values: [0,51]. A larger value indicates lower quality. Recommended range: [18,38]. If you do not specify crf or vb, the default value is 23. |
|
maxrate |
int |
No |
The maximum bitrate for dynamic bitrate mode. If you use this parameter, you must also specify the bufsize parameter. Note
This parameter is valid only when used with the crf parameter. |
|
bufsize |
int |
No |
The decoder buffer size for dynamic bitrate mode. Unit: bit/s. Note
This parameter is valid only when used with the crf parameter. |
|
an |
int |
No |
Whether to disable the audio stream. Valid values:
|
|
acodec |
string |
No |
The audio encoding method. The value is `aac`. |
|
ar |
int |
No |
The audio sampling rate. Unit: Hertz (Hz). Default: same as the source audio. Valid values:
|
|
ac |
int |
No |
The number of sound channels. Default: same as the source audio. Valid values: [1,8]. |
|
aq |
int |
No |
The audio quality. This parameter is mutually exclusive with the ab parameter. Valid values: 0 to 100. A larger value indicates higher quality. |
|
ab |
int |
No |
The audio bitrate. This parameter is mutually exclusive with the aq parameter. Unit: bit/s. Valid values: 1000 to 10000000. |
|
abopt |
int |
No |
The audio bitrate option. Valid values:
|
The sys/saveas parameter is also used when you generate a live transcoding playlist. For more information, see Save as.
Steps
Live transcoding involves the following two steps:
After you generate a playlist, you can also find playback instructions in the live transcoding topic of the Intelligent Media Management documentation.
1. Generate a live transcoding playlist for a video
Transcoding information
-
Before transcoding
-
Video format: AVI
-
Video name: oss://video-demo/example.avi
-
Start position: 15 seconds from the beginning of the video
-
Transcoding duration: 1800 seconds
-
-
Processing method: Generate a playlist
-
After transcoding
-
Segment size: 10 seconds
-
Pre-transcoding duration: 30 seconds
-
Video information
-
Video stream format: H.264
-
Video resolution: 1280 × 720
-
Video frame rate: 25 fps
-
Video bitrate: 2 Mbps
-
-
Audio information
-
Audio stream format: AAC
-
Audio bitrate: 128 Kbps
-
-
File storage path prefix: oss://outbucket/outobjprefix/media
-
Request example
POST /example.avi?x-oss-process HTTP/1.1
Host: video-demo.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e
x-oss-process=hls/m3u8,ss_15000,t_1800000,vcodec_h264,fps_25,fpsopt_1,s_1280x720,sopt_1,scaletype_fit,arotate_1,vb_2000000,vbopt_1,acodec_aac,ar_44100,ac_2,ab_128000,abopt_1,st_10000,initd_30000|sys/saveas,o_b3V0b2JqcHJlZml4L21lZGlh,b_b3V0YnVja2V0
Response example
HTTP/1.1 200 OK
Server: AliyunOSS
Date: Wed, 25 May 2022 12:43:57 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 161
Connection: keep-alive
x-oss-request-id: 628E2481184E20F26C000009
x-oss-transfer-acc-type: acc-none
x-oss-data-location: oss-cn-hangzhou-a
ETag: "D0F162350DA037F4DC2A142B2E116BD0"
Last-Modified: Wed, 25 May 2022 12:20:34 GMT
x-oss-object-type: Normal
x-oss-hash-crc64ecma: 2040549661341440100
x-oss-storage-class: Standard
x-oss-server-time: 12437
{"Duration":1800,"RequestId":"********-37E6-5996-8425-********","VideoPlaylist":[{"FrameRate":"25","Resolution":"1280x720","Token":"f93c43079**********1269608ebc86e","URI":"oss://outbucket/outobjprefix/media.m3u8"}]}
2. Use hls/sign to sign the live transcoding stream
Object Storage Service (OSS) provides a dynamic signature mechanism to access audio and video data. When you first access an m3u8 file, add x-oss-process=hls/sign,live_1 to the URL. OSS then automatically signs all TS addresses in the returned playlist using the same signing method that was used for the m3u8 file.
-
hls/sign signing method:
# -*- coding: utf-8 -*-
import os
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Specify the endpoint of the region where the bucket is located. Replace the value with the actual endpoint.
endpoint = 'yourEndpoint'
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# The name of the destination bucket.
bucket_name = 'your-oss-bucket-name'
# Set key to the name of the generated playlist, for example, output/media.m3u8.
key = 'output/media.m3u8'
# Specify the bucket instance. All file-related methods must be called through the bucket instance.
# You must use the oss2.AuthV2 signing method.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', bucket_name)
# The processing method for x-oss-process is hls/sign,live_1.
params = {}
params.update({oss2.Bucket.PROCESS: 'hls/sign,live_1'})
# The signed URL.
# When a signed URL is generated, OSS escapes the forward slashes (/) in the full path of the object by default. This makes the signed URL unusable.
# Set slash_safe to True. OSS does not escape the forward slashes (/) in the full path of the object. The generated signed URL can be used directly.
url = bucket.sign_url('GET', key, 7200, params=params, slash_safe=True)
# The generated URL can be played directly in an HLS player.
print(url)
-
Original m3u8 content
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:10.0,
media-c14709808479b31566b50f2f8b93fe1a-0.ts
#EXTINF:10.0,
media-c14709808479b31566b50f2f8b93fe1a-1.ts
#EXTINF:10.0,
media-c14709808479b31566b50f2f8b93fe1a-2.ts
#EXTINF:10.0,
media-c14709808479b31566b50f2f8b93fe1a-3.ts
#EXT-X-ENDLIST
-
Content returned after signing with hls/sign:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:10.000,
media-c14709808479b31566b50f2f8b93fe1a-0.ts?x-oss-process=if_status_eq_404{hls/ts,from_b3V0cHV0L21lZGlhLm0zdTg}&x-oss-expires=1710457284&x-oss-signature-version=OSS2&x-oss-access-key-id=****fEAub****&x-oss-signature=****VR3gy****
#EXTINF:10.000,
media-c14709808479b31566b50f2f8b93fe1a-1.ts?x-oss-process=if_status_eq_404{hls/ts,from_b3V0cHV0L21lZGlhLm0zdTg}&x-oss-expires=1710457284&x-oss-signature-version=OSS2&x-oss-access-key-id=****fEAub****&x-oss-signature=****VR3gy****
#EXTINF:10.000,
media-c14709808479b31566b50f2f8b93fe1a-2.ts?x-oss-process=if_status_eq_404{hls/ts,from_b3V0cHV0L21lZGlhLm0zdTg}&x-oss-expires=1710457284&x-oss-signature-version=OSS2&x-oss-access-key-id=****fEAub****&x-oss-signature=****VR3gy****
#EXTINF:10.000,
media-c14709808479b31566b50f2f8b93fe1a-3.ts?x-oss-process=if_status_eq_404{hls/ts,from_b3V0cHV0L21lZGlhLm0zdTg}&x-oss-expires=1710457284&x-oss-signature-version=OSS2&x-oss-access-key-id=****fEAub****&x-oss-signature=****VR3gy****
#EXT-X-ENDLIST
Use an SDK
Generating a live transcoding playlist is a synchronous process. For SDK usage, see Use an SDK.
FAQ
What output files are included?
An m3u8 file and TS files are generated based on the specified output path prefix. The m3u8 file is generated immediately. If you specify a pre-transcoding duration, TS files for that duration are generated asynchronously. For example, if the pre-transcoding duration is 30 seconds and the segment length is 10 seconds, three TS files are generated. Remaining segments are transcoded on demand during playback. If a video is never played, no additional TS files are generated. If you start playing at the 15-minute mark, transcoding starts from that point. The directory tree of the generated files is as follows:
.
├── outobjprefix.m3u8
├── outobjprefix-92376fbb-171f-4259-913f-705f7ee02f2s-0.ts
├── outobjprefix-92376fbb-171f-4259-913f-705f7ee02f2s-1.ts
├── outobjprefix-92376fbb-171f-4259-913f-705f7ee02f2s-2.ts
├── outobjprefix-92376fbb-171f-4259-913f-705f7ee02f2s-3.ts
Can the video still play normally after the generated TS files are manually deleted?
Yes, it can. As long as the source video file and the m3u8 playlist are not deleted, the video remains playable. When the m3u8 playlist is requested again, the TS files are regenerated. This method can reduce storage costs without affecting video playback.
Can I use an m3u8 file not generated by live transcoding for live transcoding?
No, you cannot. Only m3u8 files generated by the live transcoding feature can be used for live transcoding.