批量营销混剪-批量个性化混剪

本文为您介绍如何使用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保存在环境变量为例说明。配置方法请参见:https://help.aliyun.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保存在环境变量为例说明。配置方法请参见:https://help.aliyun.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();
    }

}

相关文档