在視頻制作中,可以將含有關鍵信息的視頻起始、結尾內容以畫中畫的形式拼接嵌入到正片視頻中,稱為開板、尾板。本文為您介紹媒體處理中Java SDK設置開板和尾板的示例代碼。
示例代碼
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mps.sdk.utils.InitClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.SubmitJobsRequest;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* ***** 使用須知 ******
* 本demo為不同場景的截圖作業示例,示例中均為異步截圖方式。
*
* ***** 方法介紹 ******
* mergrUrlListJob URL方式視頻拼接
* mergrConfigFileJob 配置文件方式視頻拼接
* openAndTailJob 拼接開板、尾板視頻
*
* 更多參數請參考 http://bestwisewords.com/document_detail/29253.html#section-ic9-6n4-ebc
*/
public class MergeTransCodeJobs {
/**管道ID, 可以在控制臺 【全局設置】->【管道】查看*/
private static String pipelineId = "bee7a5b9*********a0cbf";
/**模板ID, 預置模板參考 http://bestwisewords.com/document_detail/29256.html*/
private static String templateId = "S00000001-200010";
public static void main(String[] args) throws ClientException {
//初始化調用 client
DefaultAcsClient client = InitClient.initMpsClient();
SubmitJobsResponse response;
try {
response = mergrUrlListJob(client);
System.out.println("RequestId is:" + response.getRequestId());
System.out.println("JobId is:" + JSON.toJSON(response.getJobResultList().get(0).getJob().getJobId()));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 普通視頻拼接任務 URL方式
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse mergrUrlListJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構建輸出參數
JSONArray outputs = new JSONArray();
//構建input, 需要保證Location區域和服務client區域一致
JSONObject input = new JSONObject();
input.put("Bucket", "<your bucket name>");
input.put("Location", "oss-cn-beijing");
//構建一個輸出對象
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/merge-out.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
//構建一個素材截取數據
JSONObject clip = new JSONObject();
//代表從01秒000毫秒開始,截取到第5秒30毫秒為止
clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
//代表從01秒000毫秒開始,截取到距離片尾剩余5秒30毫秒為止
//clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
//true:先剪輯第一個片段,再拼接(轉碼)
clip.put("ConfigToClipFirstPart", true);
//構建一個拼接數據
JSONArray mergeList = new JSONArray();
JSONObject merge = new JSONObject();
merge.put("MergeURL", "http://bucket-name.oss-cn-beijing.aliyuncs.com/" + URLEncoder.encode("mps-test/demo/test2mp4", "utf-8"));
merge.put("Start", "00:00:03.000");
merge.put("Duration", "00:00:13.000");
mergeList.add(merge);
output.put("Clip", clip);
output.put("MergeList", mergeList);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
//指定輸出bucket
request.setOutputBucket("<your bucket name>");
//輸出bucket的所在區域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* 普通視頻拼接任務 ConfigFile方式
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse mergrConfigFileJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構建輸出參數
JSONArray outputs = new JSONArray();
//構建input, 需要保證Location區域和服務client區域一致
JSONObject input = new JSONObject();
input.put("Bucket", "<your bucket name>");
input.put("Location", "oss-cn-beijing");
//構建一個輸出對象
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/merge-out.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
//構建一個素材截取數據
JSONObject clip = new JSONObject();
//代表從01秒000毫秒開始,截取到第5秒30毫秒為止
//clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
//代表從01秒000毫秒開始,截取到距離片尾剩余5秒30毫秒為止
clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
//true:先剪輯第一個片段,再拼接(轉碼)
clip.put("ConfigToClipFirstPart", true);
output.put("Clip", clip);
//MergeConfigUrl的地址必須為HTTP地址
output.put("MergeConfigUrl", "http://bucket-name.oss-cn-beijing.aliyuncs.com/mps-test/demo/mergeConfigfile");
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
//指定輸出bucket
request.setOutputBucket("<your bucket name>");
//輸出bucket的所在區域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* 開板尾板拼接任務
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse openAndTailJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構建輸出參數
JSONArray outputs = new JSONArray();
//構建input, 需要保證Location區域和服務client區域一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//構建一個輸出對象
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/test-open-tail.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
JSONArray openingList = new JSONArray();
JSONObject opening = new JSONObject();
opening.put("OpenUrl", "http://bucket-name.oss-cn-beijing.aliyuncs.com/" + URLEncoder.encode("mps-test/demo/open.mp4", "utf-8"));
opening.put("Start", "3");
opening.put("Width", "680");
opening.put("Height", "480");
openingList.add(opening);
JSONArray tailSlateList = new JSONArray();
JSONObject tailSlate = new JSONObject();
tailSlate.put("TailUrl", "http://bucket-name.oss-cn-beijing.aliyuncs.com/" + URLEncoder.encode("mps-test/demo/tail.mp4", "utf-8"));
tailSlate.put("BlendDuration", "2");
tailSlate.put("Width", "680");
tailSlate.put("Height", "480");
tailSlate.put("IsMergeAudio", true);
tailSlate.put("BgColor", "White");
tailSlateList.add(tailSlate);
output.put("OpeningList", openingList);
output.put("TailSlateList", tailSlateList);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
//指定輸出bucket
request.setOutputBucket("<your bucket name>");
//輸出bucket的所在區域,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
}
文檔內容是否對您有幫助?