全部產品
Search
文件中心

Intelligent Media Services:配置剪輯完成時的回調

更新時間:Oct 30, 2024

在使用智能媒體服務雲剪輯服務時,您可通過設定回調事件及時擷取任務處理進展和狀態,以便進行其他業務操作。通過閱讀本文,您可以瞭解配置剪輯完成時回調的方法。

回調方式說明

智能媒體服務支援通過HTTP請求MNS隊列兩種回調方式擷取事件通知,詳情請參見回調方式說明

實現說明

配置剪輯完成時的回調可以通過自訂單任務回調地址配置全域回調地址來實現:

  • 自訂單任務回調地址:是在提交剪輯合成任務時指定的回調地址,您需要調用SubmitMediaProducingJob介面,傳入HTTP(S)回調地址或MNS回調地址。

    • 如果選擇HTTP(S)回調,您需要部署一個HTTP服務來接收回調訊息,並在剪輯合成請求中配置回調URL;當剪輯合成任務完成時,IMS服務端會對該URL發起HTTP POST請求,任務完成情況內容將通過HTTP Body送達給使用者。

    • 如果選擇MNS回調,則需要使用者在任務完成的地區(Region)配置以ice-callback開頭的訊息佇列,IMS伺服器會將訊息內容發送至您的MNS訊息佇列中。

  • 配置全域回調地址:適用於所有ApsaraVideo for Media Processing任務的回調通知。您可以在智能媒體服務的全域設定中指定一個MNS訊息佇列地址作為全域回調地址。當任何任務完成時,智能媒體服務會將回調通知發送到配置的全域回調地址。

    說明

    僅支援MNS訊息的配置。

回調事件的訊息結構

關於剪輯合成任務完成時的訊息結構,請參見事件列表

前提條件

如果回調方式為MNS隊列,您需要先授權MNS存取權限。具體操作,請參見開通輕量訊息佇列(原 MNS)並授權

自訂單任務回調地址

您可以使用自訂單任務回調地址來處理剪輯任務完成時的回調通知。在調用SubmitMediaProducingJob提交剪輯合成任務時,可以在請求中傳入一個包含HTTP(S)或MNS回調地址的UserData參數。樣本如下所示:

  • HTTP(S)回調地址

    {"NotifyAddress":"http(s)://**.**.***"}
  • MNS回調地址(必須以ice-callback開頭的訊息佇列才可以進行回調)

    {"NotifyAddress":"ice-callback-****"}

配置全域回調地址

  1. 輕量訊息佇列(原MNS)控制台建立回調訊息佇列,隊列名稱必須以ice-callback開頭。具體操作,請參見建立隊列

  2. 調用SetEventCallback設定事件回調,傳入CallbackQueueName,即步驟 1中的隊列名稱。

  3. 監聽步驟 1中的訊息佇列,並對訊息進行處理。範例程式碼如下所示:

    說明

    以下範例程式碼中引入的服務端SDK版本號碼僅供參考,擷取最新的版本請參見服務端SDK

    <dependencies>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.9</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun.mns</groupId>
        <artifactId>aliyun-sdk-mns</artifactId>
        <version>1.1.9</version>
    </dependency>
    </dependencies>
    import java.util.List;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    import com.aliyun.mns.client.CloudAccount;
    import com.aliyun.mns.client.CloudQueue;
    import com.aliyun.mns.client.MNSClient;
    import com.aliyun.mns.common.ClientException;
    import com.aliyun.mns.common.ServiceException;
    import com.aliyun.mns.common.utils.ServiceSettings;
    import com.aliyun.mns.model.Message;
    
    public class IceProduceMessageConsumerDemo {
    
        public static void main(String[] args) {
    
            // 讀取 ~/.aliyun-mns.properties  檔案中的mns 配置:
            // mns.accesskeyid, mns.accesskeysecret, mns.queue.endpoint
            // ak/sk 所屬子使用者擁有 MNS 的完全讀寫權限
            CloudAccount account = new CloudAccount(
                ServiceSettings.getMNSAccessKeyId(),
                ServiceSettings.getMNSAccessKeySecret(),
                ServiceSettings.getMNSAccountEndpoint());
            MNSClient client = account.getMNSClient(); //this client need only initialize once
    
            // Demo for receive message code
            try{
                CloudQueue queue = client.getQueueRef("ice-callback-test");// replace with your queue name
                List<Message> messages = queue.batchPopMessage(10, 30);
                for(Message popMsg : messages)
                {
                    if (popMsg != null){
                        System.out.println("message handle: " + popMsg.getReceiptHandle());
                        System.out.println("message body: " + popMsg.getMessageBodyAsString());
                        System.out.println("message id: " + popMsg.getMessageId());
                        System.out.println("message dequeue count:" + popMsg.getDequeueCount());
    
                        JSONObject jsonObject = JSON.parseObject(popMsg.getMessageBodyAsString());
    
                        // 只處理 ProduceMediaComplete 類型訊息:
                        if("ProduceMediaComplete".equals(jsonObject.getString("EventType"))){
                            JSONObject messageBody = jsonObject.getJSONObject("MessageBody");
    
                            System.out.println("ProjectId:" + messageBody.getString("ProjectId"));
                            System.out.println("JobId:" + messageBody.getString("JobId"));
                            System.out.println("Status:" + messageBody.getString("Status"));
                            System.out.println("MediaId:" + messageBody.getString("MediaId"));
                            System.out.println("MediaURL:" + messageBody.getString("MediaURL"));
    
                            //<<to add your special logic.>>
    
                            //remember to  delete message when consume message successfully.
                            //queue.deleteMessage(popMsg.getReceiptHandle());
                            System.out.println("delete message successfully.\n");
                        }
    
    
                    }
                }
            } catch (ClientException ce)
            {
                System.out.println("Something wrong with the network connection between client and MNS service."
                    + "Please check your network and DNS availability.");
                ce.printStackTrace();
            } catch (ServiceException se)
            {
                if (se.getErrorCode().equals("QueueNotExist"))
                {
                    System.out.println("Queue is not exist.Please create queue before use");
                } else if (se.getErrorCode().equals("TimeExpired"))
                {
                    System.out.println("The request is time expired. Please check your local machine timeclock");
                }
                /*
                you can get more MNS service error code in following link.
                https://www.alibabacloud.com/help/document_detail/mns/api_reference/error_code/error_code.html?spm=5176.docmns/api_reference/error_code/error_response
                */
                se.printStackTrace();
            } catch (Exception e)
            {
                System.out.println("Unknown exception happened!");
                e.printStackTrace();
            }
    
            client.close();
        }
    }