All Products
Search
Document Center

ApsaraVideo Media Processing:Manage transcoding jobs

Last Updated:Nov 19, 2024

Transcoding is to convert an audio or video file into one or more audio or video files to adapt to different network bandwidths, terminal devices, and user needs. If the transcoding jobs and workflows created in the ApsaraVideo Media Processing (MPS) console cannot meet your business requirements, you can call the SubmitJobs operation to submit transcoding jobs. This topic provides examples on how to use MPS SDK for Go V2.0 to manage transcoding jobs. For example, you can submit a transcoding job, query one or more transcoding jobs, cancel a transcoding job, and traverse transcoding jobs.

Prerequisites

An SDK client is initialized. For more information, see Initialize a client.

Submit a transcoding job

You can call the SubmitJobs operation to submit a transcoding job.

Note
  • When you submit a transcoding job by using the SDK, you must perform URL encoding on the file path in which your file resides. Otherwise, the transcoding job fails to be submitted. For more information, see URL encoding.

  • Make sure that the name of your file is valid. Otherwise, the file cannot be found and the transcoding job fails to be submitted. For more information, see Parameter details.

  • We recommend that you record the ID of a transcoding job. This facilitates subsequent operations such as querying the information about a transcoding job and traversing transcoding jobs.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
// 
// Use your AccessKey ID and AccessKey secret to initialize a client.
// 
// @return Client
// 
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }

  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  submitJobsRequest := &mts20140618.SubmitJobsRequest{
    Input: tea.String("{\"Bucket\":\"exampleBucket\",\"Location\":\"oss-cn-hangzhou\",\"Object\":\"example.flv\",\"Referer\": \"The parameter that you set in the Object Storage Service (OSS) console to enable the hotlink protection feature\"}"),
    Outputs: tea.String("[{\"OutputObject\":\"exampleOutput.mp4\",\"TemplateId\":\"6181666213ab41b9bc21da8ff5ff****\",\"WaterMarks\":[{\"InputFile\":{\"Bucket\":\"exampleBucket\",\"Location\":\"oss-cn-hangzhou\",\"Object\":\"image_01.png\"},\"WaterMarkTemplateId\":\"9b772ce2740d4d55876d8b542d47****\"}],\"UserData\":\"testid-001\"}]"),
    OutputBucket: tea.String("exampleBucket"),
    PipelineId: tea.String("dd3dae411e704030b921e52698e5****"),
  }
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // Write your own code to display the response of the API operation if necessary.
    _, _err = client.SubmitJobsWithOptions(submitJobsRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The URL of the corresponding error diagnostics page.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}

Query transcoding jobs

You can call the QueryJobList operation to query one or more transcoding jobs.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
// 
// Use your AccessKey ID and AccessKey secret to initialize a client.
// 
// @return Client
// 
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }
  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  queryJobListRequest := &mts20140618.QueryJobListRequest{}
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // Write your own code to display the response of the API operation if necessary.
    _, _err = client.QueryJobListWithOptions(queryJobListRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The URL of the corresponding error diagnostics page.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}

Cancel a transcoding job

You can call the CancelJob operation to cancel a transcoding job.

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


// Description:
// 
// Use your AccessKey ID and AccessKey secret to initialize a client.
// 
// @return Client
// 
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

  config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
    AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
  }

  config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
  _result = &mts20140618.Client{}
  _result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  client, _err := CreateClient()
  if _err != nil {
    return _err
  }

  cancelJobRequest := &mts20140618.CancelJobRequest{
    JobId: tea.String("d1ce4d3efcb549419193f50f1fcd****"),
  }
  runtime := &util.RuntimeOptions{}
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // Write your own code to display the response of the API operation if necessary.
    _, _err = client.CancelJobWithOptions(cancelJobRequest, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The URL of the corresponding error diagnostics page.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}

Traverse transcoding jobs

You can call the ListJob operation to traverse transcoding jobs.

package main

import (
        "encoding/json"
        "strings"
        "fmt"
        "os"
mts20140618  "github.com/alibabacloud-go/mts-20140618/v6/client"
openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util  "github.com/alibabacloud-go/tea-utils/v2/service"
        "github.com/alibabacloud-go/tea/tea"
        )


// Description:
//
// Use your AccessKey ID and AccessKey secret to initialize a client.
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *mts20140618.Client, _err error) {

config := &openapi.Config{
    // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
    AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
            // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
            AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}

config.Endpoint = tea.String("mts.cn-hangzhou.aliyuncs.com")
_result = &mts20140618.Client{}
_result, _err = mts20140618.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
client, _err := CreateClient()
  if _err != nil {
    return _err
}

listJobRequest := &mts20140618.ListJobRequest{
    // The state of the transcoding jobs.
    State: tea.String("All"),
    // The beginning of the time range within which the transcoding jobs to be traversed were created.
    StartOfJobCreatedTimeRange: tea.String("2014-01-10T12:00:00Z"),
    // The end of the time range within which the transcoding jobs to be traversed were created.
    EndOfJobCreatedTimeRange: tea.String("2014-01-11T12:00:00Z"),
    // The ID of the MPS queue.
    PipelineId: tea.String("88c6ca184c0e424d5w5b665e2a12****"),
    // The pagination token that is used in the next request to retrieve a new page of results.
    NextPageToken: tea.String("16f01ad6175e4230ac42bb5182cd****"),
    // The maximum number of media workflow execution instances to return on each page.
    MaximumPageSize: tea.Int64(10),
}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
    if r := tea.Recover(recover()); r != nil {
        _e = r
    }
}()
// Write your own code to display the response of the API operation if necessary.
_, _err = client.ListJobWithOptions(listJobRequest, runtime)
    if _err != nil {
    return _err
}

    return nil
  }()

          if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
        error = _t
    } else {
        error.Message = tea.String(tryErr.Error())
    }
    // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
    // The error message.
    fmt.Println(tea.StringValue(error.Message))
    // The URL of the corresponding error diagnostics page.
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
        recommend, _ := m["Recommend"]
        fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
        return _err
    }
}
  return _err
}


func main() {
    err := _main(tea.StringSlice(os.Args[1:]))
    if err != nil {
        panic(err)
    }
}

References