All Products
Search
Document Center

ApsaraVideo Media Processing:Set opening and ending scenes during video merging

Last Updated:Aug 25, 2023

Complete sample code

www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-awww.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-awww.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-awww.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-aimport 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;

/**
 * *****   Usage notes     ******
 * This sample code provides examples on how to set opening and ending scenes during video merging in multiple scenarios. 
 *
 * *****   Methods     ******
 * mergrUrlListJob: merges multiple video clips in sequence.
 * mergrConfigFileJob: merges video clips by using the Object Storage Service (OSS) path of a configuration file.
 * openAndTailJob: embeds opening parts at the beginning of the input video or adds ending parts to the end of the input video.
 *
 * For more information about parameters, see "Parameter details" at https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a.
 */
public class MergeTransCodeJobs {


    /** The ID of the ApsaraVideo Media Processing (MPS) queue. To view the ID of an MPS queue, log on to the MPS console and choose Global Settings > MPS Queue and Callback in the left-side navigation pane. */
    private static String pipelineId = "bee7a5b9*********a0cbf";
    /** The ID of the template. For more information about preset templates, see "Preset template details" at https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqing. */
    private static String templateId = "S00000001-200010";

    public static void main(String[] args) throws ClientException {

        // Initialize the 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();
        }

    }

    /**
     * Merge multiple video clips in sequence and set opening and ending scenes.
     * @param client
     * @return
     * @throws Exception
     */
    public static SubmitJobsResponse mergrUrlListJob(DefaultAcsClient client) throws Exception {

        SubmitJobsRequest request = new SubmitJobsRequest();
        // Construct output parameters.
        JSONArray outputs = new JSONArray();

        // Configure a job input. Make sure that the media file used as the job input resides in the same region as the client.
        JSONObject input = new JSONObject();
        input.put("Bucket", "<your bucket name>");
        input.put("Location", "oss-cn-beijing");

        // Configure a job output.
        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);

        // Construct data for video clipping.
        JSONObject clip = new JSONObject();
        // Cut the video from 1.000s to 5.030s.
        clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
        // Cut the video from 1.000s to 5.030s that remains from the end of the video.
        //clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
        // true: The video clips are merged for a transcoding job after editing.
        clip.put("ConfigToClipFirstPart", true);

        // Construct data for video merging.
        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());
        // The OSS bucket that stores the output file.
        request.setOutputBucket("<your bucket name>");
        // The region in which the output OSS bucket resides. Specify the region in the oss-cn-**** format. Example: oss-cn-beijing.
        request.setOutputLocation("oss-cn-beijing");
        // PipelineId
        request.setPipelineId(pipelineId);

        return client.getAcsResponse(request);
    }

    /**
     * Merge video clips by using the OSS path of a configuration file and set opening and ending scenes.
     * @param client
     * @return
     * @throws Exception
     */
    public static SubmitJobsResponse mergrConfigFileJob(DefaultAcsClient client) throws Exception {

        SubmitJobsRequest request = new SubmitJobsRequest();
        // Construct output parameters.
        JSONArray outputs = new JSONArray();

        // Configure a job input. Make sure that the media file used as the job input resides in the same region as the client.
        JSONObject input = new JSONObject();
        input.put("Bucket", "<your bucket name>");
        input.put("Location", "oss-cn-beijing");

        // Configure a job output.
        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);

        // Construct data for video clipping.
        JSONObject clip = new JSONObject();
        // Cut the video from 1.000s to 5.030s.
        //clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
        // Cut the video from 1.000s to 5.030s that remains from the end of the video.
        clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
        // true: The video clips are merged for a transcoding job after editing.
        clip.put("ConfigToClipFirstPart", true);

        output.put("Clip", clip);
        // The OSS path of the configuration file must be an HTTP URL.
        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());
        // The OSS bucket that stores the output file.
        request.setOutputBucket("<your bucket name>");
        // The region in which the output OSS bucket resides. Specify the region in the oss-cn-**** format. Example: oss-cn-beijing.
        request.setOutputLocation("oss-cn-beijing");
        // PipelineId
        request.setPipelineId(pipelineId);

        return client.getAcsResponse(request);
    }


    /**
     * Embed opening parts at the beginning of the input video as the opening scene and add ending parts to the end of the input video as the ending scene.
     * @param client
     * @return
     * @throws Exception
     */
    public static SubmitJobsResponse openAndTailJob(DefaultAcsClient client) throws Exception {

        SubmitJobsRequest request = new SubmitJobsRequest();
        // Construct output parameters.
        JSONArray outputs = new JSONArray();

        // Configure a job input. Make sure that the media file used as the job input resides in the same region as the client.
        JSONObject input = new JSONObject();
        input.put("Location", "oss-cn-beijing");
        input.put("Bucket", "<your bucket name>");

        // Configure a job output.
        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());
        // The OSS bucket that stores the output file.
        request.setOutputBucket("<your bucket name>");
        // The region in which the output OSS bucket resides. Specify the region in the oss-cn-**** format. Example: oss-cn-beijing.
        request.setOutputLocation("oss-cn-beijing");
        // PipelineId
        request.setPipelineId(pipelineId);

        return client.getAcsResponse(request);
    }

}