oss 视频转码

一,账号需要有oss和mps的权限

java sdk通过 accessKeyId和accessKeySecret可以调用接口。

但是你这个授权用户,还需要oss和mps两个权限

 

二,创建一个管道

这个很方便,直接在下面创建

https://mps.console.aliyun.com/pipeline/list

 

三,代码

public ApiResult<FileVO> test2() {
        String accessKeyId = "xxx";
        String accessKeySecret = "xxx";
        String mpsRegionId = "cn-hangzhou";
        String pipelineId = "xxx";
//模板配置的是转码方案,你也可以直接通过代码实现例如:container.put("Format", "mp4");
        String templateId = "xxx";
        String ossLocation = "oss-cn-hangzhou";
//例如: myBucket-qa
        String ossBucket = "xxx";
//资源路径:具体的文件夹+文件(没有文件夹的话,自行去掉)
        String ossInputObject = "advertisement/00f89c4c623948d3b8a068d8b9de089c.mp4";
//转码后的资源路径
        String ossOutputObject = "advertisement/output.mp4";
        // 创建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
        JSONObject input = new 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");
        }
        JSONObject output = new JSONObject();
        output.put("OutputObject", outputOSSObject);
        // Ouput->Container
        JSONObject container = new JSONObject();
        container.put("Format", "mp4");
        output.put("Container", container.toJSONString());
        // Ouput->TemplateId
//        output.put("TemplateId", templateId);
        JSONArray outputs = new JSONArray();
        outputs.add(output);
        request.setOutputs(outputs.toJSONString());
        request.setOutputBucket(ossBucket);
        request.setOutputLocation(ossLocation);
        // PipelineId
        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();
        }
        return null;
    }

 

四,结果:

oss 视频转码

 

上一篇:Redis和MC的对比


下一篇:谷粒商城笔记-环境配置(2)——文件上传、java参数验证、递归,分页、事务