本文為您介紹如何使用SubmitMediaProducingJob - 提交剪輯合成作業,通過來完成批量個性化混剪。如果您想使用更加智能快捷的方式進行視頻混剪,可使用智能一鍵成片進行創作,詳見批量智能一鍵成片說明。
場景介紹
進入5G時代,越來越多的商家選擇短視頻平臺做營銷推廣,將廣告制作成短視頻投放在多個KOL或營銷號。由于各大短視頻平臺有去重機制(防止視頻盜用),內容相同的營銷視頻會被屏蔽處理,這就要求投放在不同賬號的視頻,在內容、結構上要有差別。
前提條件
您已安裝IMS服務端SDK,詳情請參見準備工作。
批量個性化混剪
通過Timeline的方式,可以對視頻進行更多的調整。
結果示例
輸入為口播文案
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.*;
import com.aliyun.teaopenapi.models.Config;
import java.util.*;
// 成功視頻
// http://oushu-test-shanghai.oss-cn-shanghai.aliyuncs.com/ice_output/46b29eb5775f4f758846171ab79bfca7.mp4
/**
* 需要maven引入二方包依賴:
* <dependency>
* <groupId>com.aliyun</groupId>
* <artifactId>ice20201109</artifactId>
* <version>2.1.0</version>
* </dependency>
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>fastjson</artifactId>
* <version>1.2.9</version>
* </dependency>
*/
public class BatchProduceAlignment {
static final String regionId = "cn-shanghai";
static final String bucket = "<your-bucket>";
private Client iceClient;
public static void main(String[] args) throws Exception {
BatchProduceAlignment batchProduce = new BatchProduceAlignment();
batchProduce.initClient();
batchProduce.run();
}
public void initClient() throws Exception {
// 阿里云賬號AccessKey擁有所有API的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。
// 本示例以將AccessKey ID和 AccessKey Secret保存在環境變量為例說明。配置方法請參見:http://bestwisewords.com/zh/sdk/developer-reference/v2-manage-access-credentials?spm=a2c4g.11186623.0.0.423350fbOTFdOB#2a38e5c14b4em
com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
Config config = new Config();
config.setCredential(credentialClient);
// 如需硬編碼AccessKey ID和AccessKey Secret,代碼如下,但強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。
// config.accessKeyId = <第二步創建的AccessKey ID>;
// config.accessKeySecret = <第二步創建的AccessKey Secret>;
config.endpoint = "ice." + regionId + ".aliyuncs.com";
config.regionId = regionId;
iceClient = new Client(config);
}
public void run() throws Exception {
// 文字素材
String text = "人們懂得用五味雜陳形容人生,因為懂得味道是每個人心中固守的情懷。在這個時代,每一個人都經歷了太多的苦痛和喜悅,人們總會將苦澀藏在心里,而把幸福變成食物,呈現在四季的餐桌之上";
// 視頻素材
String[] videoArray = new String[]{
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f1.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f2.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f3.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f4.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f5.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f6.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f7.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f8.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f9.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f10.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f11.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f12.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f13.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f14.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f15.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f16.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f17.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f18.mp4"
};
// 背景音樂素材
String[] bgMusicArray = new String[]{
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m1.wav",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m2.wav",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m3.wav"
};
// 視頻標題
String title = "舌尖上的美食";
// 生成的成片數
int produceCount = 2;
// 批量提交任務
List<String> jobIds = new ArrayList<String>();
for (int i = 0; i < produceCount; i++) {
String jobId = produceSingleVideo(title, text, videoArray, bgMusicArray);
jobIds.add(jobId);
}
// 輪詢任務狀態直到全部結束
System.out.println("waiting job finished...");
while (true) {
Thread.sleep(3000);
boolean allFinished = true;
for (int i = 0; i < jobIds.size(); i++) {
String jobId = jobIds.get(i);
GetMediaProducingJobRequest req = new GetMediaProducingJobRequest();
req.setJobId(jobId);
GetMediaProducingJobResponse response = iceClient.getMediaProducingJob(req);
GetMediaProducingJobResponseBody.GetMediaProducingJobResponseBodyMediaProducingJob mediaProducingJob = response.getBody().getMediaProducingJob();
String status = mediaProducingJob.getStatus();
System.out.println("jobId: " + mediaProducingJob.getJobId() + ", status:" + status);
if ("Failed".equalsIgnoreCase(status)) {
System.out.println("jobfailed. jobInfo: " + JSONObject.toJSONString(mediaProducingJob));
throw new Exception("Produce failed. jobid: " + mediaProducingJob.getJobId());
}
if (!"Success".equalsIgnoreCase(status)) {
allFinished = false;
break;
}
}
if (allFinished) {
System.out.println("all job finished.");
break;
}
}
}
public String produceSingleVideo(String title, String text, String[] videoArray, String[] bgMusicArray) throws Exception {
text = text.replace(",", "。");
text = text.replace("\n", "。");
String[] sentenceArray = text.split("。");
JSONArray videoClipArray = new JSONArray();
JSONArray audioClipArray = new JSONArray();
List<String> videoList = Arrays.asList(videoArray);
Collections.shuffle(videoList);
for (int i = 0; i < sentenceArray.length; i++) {
String sentence = sentenceArray[i];
String clipId = "clip" + i;
String videoUrl = videoList.get(i);
String videoClip = "{\"MediaURL\":\""+videoUrl+"\",\"ReferenceClipId\":\""+clipId+"\",\"Effects\":[{\"Type\":\"Background\",\"SubType\":\"Blur\",\"Radius\":0.1}]}";
videoClipArray.add(JSONObject.parseObject(videoClip));
String audioClip = "{\"Type\":\"AI_TTS\",\"Content\":\"" + sentence + "\",\"Voice\":\"zhichu\",\"ClipId\":\""+clipId+"\",\"Effects\":[{\"Type\":\"AI_ASR\",\"Font\":\"Alibaba PuHuiTi\",\"Alignment\":\"TopCenter\",\"Y\":0.75,\"FontSize\":55,\"FontColor\":\"#ffffff\",\"AdaptMode\":\"AutoWrap\",\"TextWidth\":0.8,\"Outline\":2,\"OutlineColour\":\"#000000\"}]}";
audioClipArray.add(JSONObject.parseObject(audioClip));
}
String subtitleTrack = "{\"SubtitleTrackClips\":[{\"Type\":\"Text\",\"Font\":\"HappyZcool-2016\",\"Content\":\""+title+"\",\"FontSize\":80,\"FontColor\":\"#ffffff\",\"Y\":0.15,\"Alignment\":\"TopCenter\",\"EffectColorStyle\":\"CS0004-000005\",\"FontFace\":{\"Bold\":true,\"Italic\":false,\"Underline\":false}}]}";
int bgMusicIndex = (int)(Math.random() * bgMusicArray.length);
String bgMusicUrl = bgMusicArray[bgMusicIndex];
String timeline = "{\"VideoTracks\":[{\"VideoTrackClips\":"+videoClipArray.toJSONString()+"}],\"AudioTracks\":[{\"AudioTrackClips\":"+audioClipArray.toJSONString()+"},{\"AudioTrackClips\":[{\"MediaURL\":\""+bgMusicUrl+"\"}]}],\"SubtitleTracks\":[" + subtitleTrack + "]}";
//
String targetFileName = UUID.randomUUID().toString().replace("-", "");
String outputMediaUrl = "http://" + bucket + ".oss-" + regionId + ".aliyuncs.com/ice_output/" + targetFileName + ".mp4";
int width = 720;
int height = 1280;
String outputMediaConfig = "{\"MediaURL\":\"" + outputMediaUrl + "\",\"Width\":"+width+",\"Height\":"+height+"}";
SubmitMediaProducingJobRequest request = new SubmitMediaProducingJobRequest();
request.setTimeline(timeline);
request.setOutputMediaConfig(outputMediaConfig);
SubmitMediaProducingJobResponse response = iceClient.submitMediaProducingJob(request);
System.out.println("start job. jobid: " + response.getBody().getJobId() + ", outputMediaUrl: " + outputMediaUrl);
return response.getBody().getJobId();
}
}
輸入為口播音頻
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.*;
import com.aliyun.teaopenapi.models.Config;
import java.util.*;
/**
* 需要maven引入二方包依賴:
* <dependency>
* <groupId>com.aliyun</groupId>
* <artifactId>ice20201109</artifactId>
* <version>2.1.0</version>
* </dependency>
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>fastjson</artifactId>
* <version>1.2.9</version>
* </dependency>
*/
public class BatchProduce {
static final String regionId = "cn-shanghai";
static final String bucket = "<your-bucket>";
private Client iceClient;
public static void main(String[] args) throws Exception {
BatchProduce batchProduce = new BatchProduce();
batchProduce.initClient();
batchProduce.run();
}
public void initClient() throws Exception {
// 阿里云賬號AccessKey擁有所有API的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。
// 本示例以將AccessKey ID和 AccessKey Secret保存在環境變量為例說明。配置方法請參見:http://bestwisewords.com/zh/sdk/developer-reference/v2-manage-access-credentials?spm=a2c4g.11186623.0.0.423350fbOTFdOB#2a38e5c14b4em
com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
Config config = new Config();
config.setCredential(credentialClient);
// 如需硬編碼AccessKey ID和AccessKey Secret,代碼如下,但強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。
// config.accessKeyId = <第二步創建的AccessKey ID>;
// config.accessKeySecret = <第二步創建的AccessKey Secret>;
config.endpoint = "ice." + regionId + ".aliyuncs.com";
config.regionId = regionId;
iceClient = new Client(config);
}
public void run() throws Exception {
// 口播音頻素材
String inputSpeechAudio = "https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/food_speech.mp3";
// 視頻素材
String[] videoArray = new String[]{
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f1.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f2.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f3.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f4.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f5.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f6.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f7.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f8.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f9.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f10.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f11.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f12.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f13.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f14.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f15.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f16.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f17.mp4",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/food/f18.mp4"
};
// 背景音樂素材
String[] bgMusicArray = new String[]{
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m1.wav",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m2.wav",
"https://ice-document-materials.oss-cn-shanghai.aliyuncs.com/test_media/music/m3.wav"
};
// 視頻標題
String title = "舌尖上的美食";
// 生成的成片數
int produceCount = 2;
// 開始合成
// 步驟1:對口播進行語音識別
SubmitASRJobRequest asrJobRequest = new SubmitASRJobRequest();
asrJobRequest.setInputFile(inputSpeechAudio);
SubmitASRJobResponse asrJobResponse = iceClient.submitASRJob(asrJobRequest);
String asrJobId = asrJobResponse.getBody().getJobId();
String asrResult = null;
for (int i = 0; i < 10000; i++) {
Thread.sleep(3000);
GetSmartHandleJobRequest getSmartHandleJobRequest = new GetSmartHandleJobRequest();
getSmartHandleJobRequest.setJobId(asrJobId);
GetSmartHandleJobResponse asrResponse = iceClient.getSmartHandleJob(getSmartHandleJobRequest);
String jobStatus = asrResponse.getBody().getState();
System.out.println("asrJobId: " + asrJobId + ", status: " + jobStatus);
if ("Failed".equalsIgnoreCase(jobStatus)) {
System.out.println("asr job failed.");
return;
}
if ("Finished".equalsIgnoreCase(jobStatus)) {
asrResult = asrResponse.getBody().getJobResult().getAiResult();
break;
}
}
System.out.println("asr job finished. asr result: " + asrResult);
// 步驟2:批量提交任務
List<String> jobIds = new ArrayList<String>();
for (int i = 0; i < produceCount; i++) {
String jobId = produceSingleVideo(title, inputSpeechAudio, asrResult, videoArray, bgMusicArray);
jobIds.add(jobId);
}
// 步驟3:輪詢任務狀態直到全部結束
System.out.println("waiting job finished...");
while (true) {
Thread.sleep(3000);
boolean allFinished = true;
for (int i = 0; i < jobIds.size(); i++) {
String jobId = jobIds.get(i);
GetMediaProducingJobRequest req = new GetMediaProducingJobRequest();
req.setJobId(jobId);
GetMediaProducingJobResponse response = iceClient.getMediaProducingJob(req);
GetMediaProducingJobResponseBody.GetMediaProducingJobResponseBodyMediaProducingJob mediaProducingJob = response.getBody().getMediaProducingJob();
String status = mediaProducingJob.getStatus();
System.out.println("jobId: " + mediaProducingJob.getJobId() + ", status:" + status);
if ("Failed".equalsIgnoreCase(status)) {
System.out.println("jobfailed. jobInfo: " + JSONObject.toJSONString(mediaProducingJob));
throw new Exception("Produce failed. jobid: " + mediaProducingJob.getJobId());
}
if (!"Success".equalsIgnoreCase(status)) {
allFinished = false;
break;
}
}
if (allFinished) {
System.out.println("all job finished.");
break;
}
}
}
public String produceSingleVideo(String title, String inputSpeechAudio, String asrResult, String[] videoArray, String[] bgMusicArray) throws Exception {
JSONArray videoClipArray = new JSONArray();
JSONArray subtitleClipArray = new JSONArray();
List<String> videoList = Arrays.asList(videoArray);
Collections.shuffle(videoList);
JSONArray asrResultArray = JSONArray.parseArray(asrResult);
for (int i = 0; i < asrResultArray.size(); i++) {
JSONObject asrItem = asrResultArray.getJSONObject(i);
String content = asrItem.getString("content");
float from = asrItem.getFloat("from");
float to = asrItem.getFloat("to");
float duration = to - from;
String videoUrl = videoList.get(i);
String videoClip = "{\"MediaURL\":\"" + videoUrl + "\",\"Effects\":[{\"Type\":\"Background\",\"SubType\":\"Blur\",\"Radius\":0.1},{\"Type\":\"Clip\",\"SubType\":\"RandomClip\",\"ClipDuration\":" + duration + "}]}";
videoClipArray.add(JSONObject.parseObject(videoClip));
String subtitleClip = "{\"Content\":\"" + content + "\",\"TimelineIn\":" + from + ",\"TimelineOut\":" + to + ",\"Type\":\"Text\",\"Font\":\"Alibaba PuHuiTi\",\"Alignment\":\"TopCenter\",\"Y\":0.75,\"FontSize\":55,\"FontColor\":\"#ffffff\",\"AdaptMode\":\"AutoWrap\",\"TextWidth\":0.8,\"Outline\":2,\"OutlineColour\":\"#000000\"}";
subtitleClipArray.add(JSONObject.parseObject(subtitleClip));
}
String titleTrack = "{\"SubtitleTrackClips\":[{\"Type\":\"Text\",\"Font\":\"HappyZcool-2016\",\"Content\":\""+title+"\",\"FontSize\":80,\"FontColor\":\"#ffffff\",\"Y\":0.15,\"Alignment\":\"TopCenter\",\"EffectColorStyle\":\"CS0004-000005\",\"FontFace\":{\"Bold\":true,\"Italic\":false,\"Underline\":false}}]}";
String subtitleTrack = "{\"SubtitleTrackClips\":"+subtitleClipArray+"}";
int bgMusicIndex = (int)(Math.random() * bgMusicArray.length);
String bgMusicUrl = bgMusicArray[bgMusicIndex];
String timeline = "{\"VideoTracks\":[{\"VideoTrackClips\":"+videoClipArray.toJSONString()+"}],\"AudioTracks\":[{\"AudioTrackClips\":[{\"MediaURL\":\""+inputSpeechAudio+"\"}]},{\"AudioTrackClips\":[{\"MediaURL\":\""+bgMusicUrl+"\"}]}],\"SubtitleTracks\":[" + titleTrack + "," + subtitleTrack + "]}";
//
String targetFileName = UUID.randomUUID().toString().replace("-", "");
String outputMediaUrl = "http://" + bucket + ".oss-" + regionId + ".aliyuncs.com/ice_output/" + targetFileName + ".mp4";
int width = 720;
int height = 1280;
String outputMediaConfig = "{\"MediaURL\":\"" + outputMediaUrl + "\",\"Width\":"+width+",\"Height\":"+height+"}";
SubmitMediaProducingJobRequest request = new SubmitMediaProducingJobRequest();
request.setTimeline(timeline);
request.setOutputMediaConfig(outputMediaConfig);
SubmitMediaProducingJobResponse response = iceClient.submitMediaProducingJob(request);
System.out.println("start job. jobid: " + response.getBody().getJobId() + ", outputMediaUrl: " + outputMediaUrl);
return response.getBody().getJobId();
}
}
相關文檔
文檔內容是否對您有幫助?