1.视频封面图:
截取视频视频首帧为封面图,传入视频存储地址和截取后存储封面地址
public String snapshotVideo(String inputObject, String outputObject) throws ServerException, com.aliyuncs.exceptions.ClientException {
DefaultProfile.addEndpoint(regionId, regionId, "Mts", "mts." + regionId + ".aliyuncs.com");
// 创建DefaultAcsClient实例并初始化
DefaultProfile profile = DefaultProfile.getProfile(regionId, // 地域ID
accessId, // RAM账号的AccessKey ID
accessKey); // RAM账号Access Key Secret
DefaultAcsClient client = new DefaultAcsClient(profile);
SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
JSONObject input = new JSONObject();
input.put("Location", location);
input.put("Bucket", bucket);
input.put("Object", inputObject);
request.setInput(input.toJSONString());
JSONObject snapshotConfig = new JSONObject();
JSONObject output = new JSONObject();
output.put("Location", location);
output.put("Bucket", bucket);
output.put("Object", outputObject);
snapshotConfig.put("OutputFile", output);
snapshotConfig.put("Time", 0);
request.setSnapshotConfig(snapshotConfig.toJSONString());
SubmitSnapshotJobResponse response = client.getAcsResponse(request);
SnapshotJob job = response.getSnapshotJob();
if (job.getState().equals("Success")) {
return "success";
} else {
System.out.println("snapshotVideo Failed code:" + job.getCode() + " message:" + job.getMessage());
return "error";
}
}
2.视频转码
传入三个参数:视频存储地址、转码后视频存储地址、管道id(阿里云后台获取)
public String transferVideo(String inputObject, String outputObject, String templateId) throws UnsupportedEncodingException, ClientException, com.aliyuncs.exceptions.ClientException {
DefaultProfile.addEndpoint(regionId, regionId, "Mts", "mts." + regionId + ".aliyuncs.com");
// 创建DefaultAcsClient实例并初始化
DefaultProfile profile = DefaultProfile.getProfile(regionId, // 地域ID
accessId, // RAM账号的AccessKey ID
accessKey); // RAM账号Access Key Secret
IAcsClient client = new DefaultAcsClient(profile);
// 创建API请求并设置参数
SubmitJobsRequest request = new SubmitJobsRequest();
// Input
JSONObject input = new JSONObject();
input.put("Location", location);
input.put("Bucket", bucket);
input.put("Object", URLEncoder.encode(inputObject, "utf-8"));
request.setInput(input.toJSONString());
// Output
String outputOSSObject = URLEncoder.encode(outputObject, "utf-8");
JSONObject output = new JSONObject();
output.put("OutputObject", outputOSSObject);
output.put("TemplateId", templateId);
JSONArray outputs = new JSONArray();
outputs.add(output);
request.setOutputs(outputs.toJSONString());
request.setOutputBucket(bucket);
request.setOutputLocation(location);
// PipelineId
request.setPipelineId(pipelineId);
// 发起请求并处理应答或异常
SubmitJobsResponse response = client.getAcsResponse(request);
System.out.println("RequestId is:" + response.getRequestId());
if (response.getJobResultList().get(0).getSuccess()) {
return response.getJobResultList().get(0).getJob().getJobId();
} else {
System.out.println("SubmitJobs Failed code:" + response.getJobResultList().get(0).getCode() + " message:" + response.getJobResultList().get(0).getMessage());
return "error";
}
}
转码后返回转码结果,如转码失败可以利用返回id,再次转码
public boolean checkJobDone(String jobIds) throws ServerException, com.aliyuncs.exceptions.ClientException {
DefaultProfile.addEndpoint(regionId, regionId, "Mts", "mts." + regionId + ".aliyuncs.com");
// 创建DefaultAcsClient实例并初始化
DefaultProfile profile = DefaultProfile.getProfile(regionId, // 地域ID
accessId, // RAM账号的AccessKey ID
accessKey); // RAM账号Access Key Secret
DefaultAcsClient client = new DefaultAcsClient(profile);
QueryJobListRequest request = new QueryJobListRequest();
request.setJobIds(jobIds);
QueryJobListResponse response = client.getAcsResponse(request);
List jobList = response.getJobList();
boolean res = true;
for (Job job : jobList) {
logger.info("转码结果,jobId={},message={},code={},percent={},state={}", job.getJobId(), job.getMessage(), job.getCode(), job.getPercent(), job.getState());
if (!job.getState().equals("TranscodeSuccess")) {
res = false;
logger.error("转码失败,jobId={},message={},code={}", job.getJobId(), job.getMessage(), job.getCode());
break;
}
}
return res;
}
如果转码失败,可以重复调用,直到转码成功。