全部產品
Search
文件中心

Intelligent Media Services:剪輯合成

更新時間:Nov 01, 2024

通過閱讀本文,您可以瞭解通過服務端SDK調用OpenAPI建立智能媒體服務剪輯合成任務的範例程式碼。

使用說明

建立合成任務時,您可以根據實際需求通過Timeline、TemplateId或ProjectId其中之一建立。

範例程式碼

您可以通過阿里雲OpenAPI開發人員門戶線上調試。

package com.aliyun.ice.sample;

import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.*;
import com.aliyun.teaopenapi.models.Config;

public class MediaProducingJobSample {

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

        // 阿里雲帳號AccessKey擁有所有API的存取權限,建議您使用RAM使用者進行API訪問或日常營運。
        // 本樣本以將AccessKey ID和 AccessKey Secret儲存在環境變數為例說明。
        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 = <yourAccessKeyId>;
        // config.accessKeySecret = <yourAccessKeySecret>;

        config.endpoint = "ice.cn-shanghai.aliyuncs.com";
        Client client = new Client(config);

        // 通過Timeline建立合成任務
        SubmitMediaProducingJobRequest submitMediaProducingJobRequest1 = new SubmitMediaProducingJobRequest();
        submitMediaProducingJobRequest1.setTimeline("{\"VideoTracks\":[{\"VideoTrackClips\":[{\"MediaId\":\"****9b4d7cf14dc7b83b0e801cbe****\"},{\"MediaId\":\"****9b4d7cf14dc7b83b0e801cbe****\"}]}]}");
        submitMediaProducingJobRequest1.setOutputMediaConfig("{\"MediaURL\":\"http://example-bucket.oss-object.com/example.mp4\"}");
        submitMediaProducingJobRequest1.setProjectMetadata("{\"coverURL\":\"http://test.sample.com/mediaid.jpg\",\"description\":\"description\",\"title\":\"title\",\"tags\":\"Tag1,Tag2,Test\"}");
        SubmitMediaProducingJobResponse submitMediaProducingJobResponse1 = client.submitMediaProducingJob(submitMediaProducingJobRequest1);
        System.out.println("requestId : " + submitMediaProducingJobResponse1.getBody().getRequestId());
        System.out.println("jobId : " + submitMediaProducingJobResponse1.getBody().getJobId());
        System.out.println("projectId : " + submitMediaProducingJobResponse1.getBody().getProjectId());

        // 通過TemplateId建立合成任務
        SubmitMediaProducingJobRequest submitMediaProducingJobRequest2 = new SubmitMediaProducingJobRequest();
        submitMediaProducingJobRequest2.setTemplateId("IceSys_VideoMerge");
        submitMediaProducingJobRequest2.setClipsParam("{\"VideoArray\":[\"****05512043f49f697f7425****\",\"****2788e810116a45109f2e****\",\"****67f44f4964e6c998dee8****\",]}");
        submitMediaProducingJobRequest2.setOutputMediaConfig("{\"MediaURL\":\"http://example-bucket.oss-object.com/example.mp4\"}");
        SubmitMediaProducingJobResponse submitMediaProducingJobResponse2 = client.submitMediaProducingJob(submitMediaProducingJobRequest2);
        System.out.println("requestId : " + submitMediaProducingJobResponse2.getBody().getRequestId());
        System.out.println("jobId : " + submitMediaProducingJobResponse2.getBody().getJobId());
        System.out.println("projectId : " + submitMediaProducingJobResponse2.getBody().getProjectId());

        // 通過ProjectId建立合成任務
        SubmitMediaProducingJobRequest submitMediaProducingJobRequest3 = new SubmitMediaProducingJobRequest();
        submitMediaProducingJobRequest3.setProjectId("****67f44f4964e6c998dee8****");
        submitMediaProducingJobRequest3.setOutputMediaConfig("{\"MediaURL\":\"http://example-bucket.oss-object.com/example.mp4\"}");
        SubmitMediaProducingJobResponse submitMediaProducingJobResponse3 = client.submitMediaProducingJob(submitMediaProducingJobRequest3);
        System.out.println("requestId : " + submitMediaProducingJobResponse3.getBody().getRequestId());
        System.out.println("jobId : " + submitMediaProducingJobResponse3.getBody().getJobId());
        System.out.println("projectId : " + submitMediaProducingJobResponse3.getBody().getProjectId());

        // 擷取單個合成任務
        GetMediaProducingJobRequest request = new GetMediaProducingJobRequest();
        request.setJobId("****67f44f4964e6c998dee8****");
        GetMediaProducingJobResponse reqponse = client.getMediaProducingJob(request);
        System.out.println("jobId : " + reqponse.getBody().getMediaProducingJob().getJobId());
        System.out.println("status : " + reqponse.getBody().getMediaProducingJob().getStatus());

        // 通過直播流區間提交合成
        SubmitLiveEditingJobRequest submitLiveEditingJobRequest = new SubmitLiveEditingJobRequest();
        submitLiveEditingJobRequest.setClips("[{\"StartTime\":\"2021-06-21T08:01:00Z\",\"EndTime\":\"2021-06-21T08:03:00Z\",\"AppName\":\"app\",\"DomainName\":\"domain.com\",\"StreamName\":\"stream\"},{\"StartTime\":\"2021-06-21T08:05:00Z\",\"EndTime\":\"2021-06-21T08:09:00Z\"}]");
        submitLiveEditingJobRequest.setProjectId("****fddd7748b58bf1d47e95****");
        submitLiveEditingJobRequest.setLiveStreamConfig("{ \"AppName\": \"app\", \"DomainName\": \"domain.com\", \"StreamName\": \"stream\" }");
        submitLiveEditingJobRequest.setOutputMediaConfig("{ \"MediaURL\": \"https://example-bucket.oss-cn-shanghai.aliyuncs.com/testfile.mp4\" }");
        submitLiveEditingJobRequest.setMediaProduceConfig("{ \"Mode\": \"AccurateFast\"}");
        submitLiveEditingJobRequest.setUserData("{\"key\": \"value\"}");
        submitLiveEditingJobRequest.setOutputMediaTarget("oss-object");
        SubmitLiveEditingJobResponse submitLiveEditingJobResponse = client.submitLiveEditingJob(submitLiveEditingJobRequest);
        System.out.println("jobId:"+submitLiveEditingJobResponse.getBody().getJobId());
        System.out.println("requestid:"+submitLiveEditingJobResponse.getBody().getRequestId());

        // 通過直播JobId擷取單個資訊
        GetLiveEditingJobRequest getLiveEditingJobRequest = new GetLiveEditingJobRequest();
        getLiveEditingJobRequest.setJobId("****d80e4e4044975745c14b****");
        GetLiveEditingJobResponse getLiveEditingJobResponse = client.getLiveEditingJob(getLiveEditingJobRequest);
        System.out.println("state:"+getLiveEditingJobResponse.getBody().getLiveEditingJob().getStatus());

    }
}

相關介面