Video transcoding (Python SDK V1)

Updated at:

Video transcoding refers to the process of converting a coded video stream to another video stream to adapt to different network bandwidths and terminal processing capabilities. This topic describes how to transcode videos in asynchronous mode.

Prerequisites

Intelligent Media Management (IMM) is bound. For more information, see Getting started with IMM.

Example code

The following code provides an example on how to transcode a video in asynchronous mode.

# -*- coding: utf-8 -*-
import base64
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Get access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the bucket name.
bucket_name = 'examplebucket'
# Specify the name of the source video file.
source_key = 'srcexample.mp4'
# Specify the name of the destination video file.
target_key = 'destexample.mp4'

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Create a bucket instance. All file-related methods must be called on the bucket instance.
bucket = oss2.Bucket(auth, endpoint, bucket_name, region=region)

# Transcode the srcexample.mp4 video. The output file is an MP4 container with an H.265 video stream, 1920x1080 resolution, 30 fps frame rate, and 2 Mbps video bitrate. The audio uses the AAC format with a 100 Kbps bitrate. The caption stream is disabled.
style = 'video/convert,f_mp4,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1'
process = "{0}|sys/saveas,o_{1},b_{2}".format(style,
    oss2.compat.to_string(base64.urlsafe_b64encode(oss2.compat.to_bytes(target_key))).replace('=', ''),
    oss2.compat.to_string(base64.urlsafe_b64encode(oss2.compat.to_bytes(bucket.bucket_name))).replace('=', ''))

# Call the asynchronous stream processing API.
result = bucket.async_process_object(source_key, process)
print(result.status)

References

For more information about the video transcoding feature, see Video Transcoding.