1. 程式人生 > 其它 >oss 視訊轉碼

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; }

四,結果: