OSS 视频转码

阿里官网文档地址:https://help.aliyun.com/document_detail/67662.html?spm=a2c4g.11186623.6.746.47dc1490CeXkzQ

提交转码作业:

<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-mts -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-mts</artifactId>
    <version>2.7.2</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.5.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dysmsapi -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.0.0</version>
        </dependency>
@RequestMapping("/simpleTranscode")
    public void SimpleTranscode(HttpServletRequest req, HttpServletResponse res, String results) throws Exception {
        String accessKeyId = MultipartUploadSample.accessKeyId;//阿里云颁发给用户的访问服务所用的密钥ID。
        String accessKeySecret = MultipartUploadSample.accessKeySecret;//阿里云accessKeySecret
        String mpsRegionId = "cn-hangzhou";// 地域ID
        String pipelineId = "89e52fcd0d6a45d3ba4cf6e1e1c11151";//管道ID
        String templateId = "b222444b6ff195a1f08cc29ec1ec1d3f";//自定义模版
        String ossLocation = "oss-cn-hangzhou";
        String ossBucket = MultipartUploadSample.bucketName;
        String ossInputObject = "chaoxuncunchuyi/2019-05-13/20190513154747.mp4";//转码输入
        String ossOutputObject = "chaoxuncunchuyi/2019-05-13/ceshi10";//转码输出
        // 创建DefaultAcsClient实例并初始化
        DefaultProfile profile = DefaultProfile.getProfile(
                mpsRegionId,      // 地域ID
                accessKeyId,      // RAM账号的AccessKey ID
                accessKeySecret); // RAM账号Access Key Secret
        IAcsClient client = new DefaultAcsClient(profile);
        // 创建API请求并设置参数
        SubmitJobsRequest request = new SubmitJobsRequest();
        // Input转码参数
        com.alibaba.fastjson.JSONObject input = new com.alibaba.fastjson.JSONObject();
        input.put("Location", ossLocation);
        input.put("Bucket", ossBucket);
        try {
            input.put("Object", URLEncoder.encode(ossInputObject, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("input URL encode failed");
        }
        request.setInput(input.toJSONString());
        // Output
        String outputOSSObject;
        try {
            outputOSSObject = URLEncoder.encode(ossOutputObject, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("output URL encode failed");
        }
        com.alibaba.fastjson.JSONObject output = new com.alibaba.fastjson.JSONObject();
        output.put("OutputObject", outputOSSObject);
        // Ouput->Container容器类型(文件格式)。视频支持mp4、flv、ts、m3u8,音频支持mp3、mp4等。
        /*com.alibaba.fastjson.JSONObject container = new com.alibaba.fastjson.JSONObject();
        container.put("Format", "mp4");
        output.put("Container", container.toJSONString());
        // Ouput->Video视频
        com.alibaba.fastjson.JSONObject video = new com.alibaba.fastjson.JSONObject();
        video.put("Codec", "H.264");
        video.put("Bitrate", "1500");
        video.put("Width", "1280");
        video.put("Fps", "25");
        output.put("Video", video.toJSONString());
        // Ouput->Audio音频
        com.alibaba.fastjson.JSONObject audio = new com.alibaba.fastjson.JSONObject();
        audio.put("Codec", "AAC");
        audio.put("Bitrate", "128");
        audio.put("Channels", "2");
        audio.put("Samplerate", "44100");
        output.put("Audio", audio.toJSONString());*/
        // Ouput->TemplateId
        output.put("TemplateId", templateId);
        com.alibaba.fastjson.JSONArray outputs = new com.alibaba.fastjson.JSONArray();
        outputs.add(output);
        request.setOutputs(outputs.toJSONString());
        request.setOutputBucket(ossBucket);
        request.setOutputLocation(ossLocation);
        // PipelineId管道ID
        request.setPipelineId(pipelineId);
        // 发起请求并处理应答或异常
        SubmitJobsResponse response;
        try {
            response = client.getAcsResponse(request);
            System.out.println("RequestId is:" + response.getRequestId());
            if (response.getJobResultList().get(0).getSuccess()) {
                System.out.println("JobId is:" + response.getJobResultList().get(0).getJob().getJobId());
            } else {
                System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() +
                        " message:" + response.getJobResultList().get(0).getMessage());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

登录 媒体处理控制台:https://mps.console.aliyun.com/overview,查看转码管道与转码模板
OSS 视频转码_第1张图片
转码模板:

OSS 视频转码_第2张图片

你可能感兴趣的:(java)