Different devices, such as smartphones, tablets, and smart TVs, support different video formats. Use the video transcoding feature to convert a source video file into a format that plays on your target device or application. This lets you distribute and share your video content across multiple platforms. During transcoding, you can adjust parameters such as video resolution and frame rate. This reduces the video file size, which enables faster video transmission and saves storage space.
Prerequisites
IMM is attached. For more information, see Attach IMM.
Notes
Before you use video transcoding, make sure that you understand the feature. For more information, see Video transcoding.
Only Java SDK 3.17.0 and later support video transcoding.
This topic uses the public Endpoint for China (Hangzhou) as an example. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal network Endpoint. For more information about the mappings between OSS regions and Endpoints, see Regions and Endpoints.
The example in this topic reads access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
The example in this topic creates an OSSClient using an OSS domain name. If you want to create an OSSClient using a custom domain name or STS, see Configure a client.
To execute a video transcoding task, you must have the required OSS and IMM permissions. For more information, see Permissions.
Sample code
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Throwable {
// The China (Hangzhou) Endpoint is used as an example. Specify the actual Endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the bucket name. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name.
String key = "src.mp4";
// Specify the name of the processed video file.
String saveAsKey = "dest.avi";
// Specify the region where the bucket is located. For example, if the region is China (Hangzhou), set Region to cn-hangzhou.
String region = "cn-hangzhou";
// Create an OSSClient instance.
// When the OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
StringBuilder styleBuilder = new StringBuilder();
// Transcode the source video file. The output media container is avi, the video stream format is H.265, the resolution is 1920x1080, the frame rate is 30 fps, and the video bitrate is 2 Mbps. The audio coding format is aac, the audio bitrate is 100 Kbps, and the caption stream is disabled.
styleBuilder.append("video/convert,f_avi,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1");
styleBuilder.append("|sys/saveas,");
styleBuilder.append("o_" + BinaryUtil.toBase64String(saveAsKey.getBytes()).replaceAll("=", ""));
styleBuilder.append(",");
styleBuilder.append("b_" + BinaryUtil.toBase64String(bucketName.getBytes()).replaceAll("=", ""));
AsyncProcessObjectRequest request = new AsyncProcessObjectRequest(bucketName, key, styleBuilder.toString());
AsyncProcessObjectResult asyncProcessObject = ossClient.asyncProcessObject(request);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}