すべてのプロダクト
Search
ドキュメントセンター

ApsaraVideo Live:プロダクションスタジオを一般モードで使用するためにAPI操作を呼び出すデモ

最終更新日:Sep 04, 2024

このトピックでは、API操作を呼び出してプロダクションスタジオを一般モードで使用する方法について説明します。 要件に合わせてコードのロジックを変更できます。

手順

API操作を呼び出して、プロダクションスタジオを一般モードで使用するには、次の手順を実行します。

重要

レイアウトを追加する前に、ビデオソースを追加する必要があります。 そうしないと、レイアウトは有効になりません。

  1. プロダクションスタジオを作成する

  2. プロダクションスタジオを設定する

  3. ビデオソースの追加

  4. レイアウトの追加

  5. (オプション) コンポーネントの追加

    ビジネス要件に基づいてコンポーネントを追加して、リアルタイムの字幕、ロゴ画像、バナーテキストをビデオソースに適用できます。

  6. プロダクションスタジオを始める

  7. 再生シーンの設定

  8. プロダクションスタジオを停止する

  9. プロダクションスタジオの削除

サンプルコード

説明
  • プロダクションスタジオを作成する

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        uuid "github.com/satori/go.uuid"
        "testing"
    		"github.com/go-ini/ini"
    )
    
    // Create a production studio.
    func CreateCaster(t *testing.T) {
      	cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
    
        // Set the request parameters for creating a production studio.
        createCasterRequest := live.CreateCreateCasterRequest()
        createCasterRequest.ClientToken = uuid.NewV1().String()
        createCasterRequest.CasterName = "Production studio for testing" // If the settings do not take effect, specify this parameter by calling the SetCasterConfig operation.
        createCasterRequest.ChargeType = "PostPaid"
        createCasterRequest.NormType = "1"
        _, err = liveClient.CreateCaster(createCasterRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    CreateCaster操作のリクエストパラメーターの詳細については、「CreateCaster」をご参照ください。

  • プロダクションスタジオを设定する

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
      	"github.com/go-ini/ini"
    )
    
    // Set the production studio.
    func SetCasterConfig(t *testing.T) {
    		cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
    
        // Set the request parameters for configuring the production studio.
        createSetCasterConfig := live.CreateSetCasterConfigRequest()
        createSetCasterConfig.CasterId = "xxxxxx"
        createSetCasterConfig.CasterName = "Production studio for testing"
        createSetCasterConfig.ChannelEnable = "1"
        createSetCasterConfig.Delay = "0"
        createSetCasterConfig.DomainName = "xxxxxxxx"
        createSetCasterConfig.ProgramEffect = "1"
        createSetCasterConfig.ProgramName = "test loop play"
        // Configure the transcoding settings. You can specify the screen orientation and resolution of videos.
        // For more information, see SetCasterConfig.
        // CasterTemplate: the transcoding settings of the production studio. Valid values:
        // A value of lp_ld, lp_sd, lp_hd, or lp_ud indicates that the transcoded videos are in low definition, standard definition, high definition, or ultra-high definition. 
        // A value of lp_ld_v, lp_sd_v, lp_hd_v, or lp_ud_v indicates that the transcoded videos are in low definition, standard definition, high definition, or ultra-high definition in portrait mode. 
        createSetCasterConfig.TranscodeConfig = `{"CasterTemplate": "lp_ld"}`
        // Configure the recording settings.
        // For more information about the JSON field, see AddLiveAppRecordConfig.
        createSetCasterConfig.RecordConfig = fmt.Sprintf(`{ "endpoint": "oss-cn-shanghai.aliyuncs.com", "ossBucket": "test-record", "videoFormat": [{"format": "flv", "interval": 900, "prefix":"record/{AppName}/{StreamName}/{StartTime}_{EndTime}" }]}`)
        _, err = liveClient.SetCasterConfig(createSetCasterConfig)
        if err != nil {
            t.Fatal(err)
        }
    }

    SetCasterConfig操作の詳細については、「SetCasterConfig」をご参照ください。

  • ビデオソースの追加とビデオソースのチャンネルの指定

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
      	"github.com/go-ini/ini"
    )
    
    // Add a video source.
    func AddCasterVideoResource(t *testing.T) {
      	cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
    
        // Add a video source to the production studio.
        addCasterVideoResourceRequest := live.CreateAddCasterVideoResourceRequest()
        addCasterVideoResourceRequest.CasterId = "xxxx"
        addCasterVideoResourceRequest.ResourceName = "Video source for testing"
        addCasterVideoResourceRequest.LiveStreamUrl = "xxxxxxxxxxxx"
        addCasterVideoResourceRequest.PtsCallbackInterval = "1000"
        addCasterVideoResourceResp, err := liveClient.AddCasterVideoResource(addCasterVideoResourceRequest)
        if err != nil {
            t.Fatal(err)
        }
        resourceID := addCasterVideoResourceResp.ResourceId
    
        // Specify a channel for the vide source.
        setCasterChannelRequest := live.CreateSetCasterChannelRequest()
        setCasterChannelRequest.CasterId = "xxx"
        setCasterChannelRequest.PlayStatus = "1"
        setCasterChannelRequest.ChannelId = "RV01"
        setCasterChannelRequest.ResourceId = resourceID
        _, err = liveClient.SetCasterChannel(setCasterChannelRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    AddCasterVideoResource操作の詳細については、「AddCasterVideoResource」をご参照ください。

  • レイアウトの追加

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
    		"github.com/go-ini/ini"
    )
    
    // Add a layout to the production studio.
    func AddCasterLayout(t *testing.T) {
        cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
    
        // Create a layout.
        addCasterLayoutRequest := live.CreateAddCasterLayoutRequest()
        addCasterLayoutRequest.CasterId = "xxx"
        addCasterLayoutRequest.BlendList = &[]string{"RV01"} // The location ID of the video layer.
        addCasterLayoutRequest.MixList = &[]string{"RV01"} // The location ID of the audio layer.
        addCasterLayoutRequest.AudioLayer = &[]live.AddCasterLayoutAudioLayer{
            {
                VolumeRate:         "1",
                ValidChannel:       "all",
                FixedDelayDuration: "0",
            },
        }
        addCasterLayoutRequest.VideoLayer = &[]live.AddCasterLayoutVideoLayer{
            {
                FillMode:           "fit",
                HeightNormalized:   "1",
                WidthNormalized:    "1",
                PositionRefer:      "topLeft",
                PositionNormalized: &[]string{"0", "0"},
                FixedDelayDuration: "0",
            },
        }
        _, err = liveClient.AddCasterLayout(addCasterLayoutRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    AddCasterLayout操作の詳細については、「AddCasterLayout」をご参照ください。

  • リアルタイム字幕コンポーネントを追加してコンポーネントを適用

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
      	"github.com/go-ini/ini"
    )
    
    // Add a real-time subtitle component and apply the componen.
    func AddETComponent(t *testing.T) {
        cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
        // Before you apply a real-time subtitle component, make sure that the following conditions are met: A production studio is created, a live stream is added to the production studio as a video source, and the locationID parameter is set to RV01 for the video source.
        // Note: Real-time subtitle components are available only for live streams.
        casterID := "xxxxx"
        // Add a component.
        r := live.CreateAddCasterComponentRequest()
        r.CasterId = casterID
        r.ComponentType = "caption"
        r.ComponentName = "Real-time subtitles for testing"
        r.Effect = "none"
        r.LocationId = "RC01"
        r.ComponentLayer = `{"HeightNormalized":"1","WidthNormalized":"1","PositionRefer":"topLeft","PositionNormalized":["0.05", "0.7"]}`
        // For more information about relevant fields, see description about the CaptionLayerContent parameter in AddCasterComponent.
        // The sample code involves the following fields that are not described in related documents on the International site (alibabacloud.com):
        // BoxWidthNormalized: the normalized value of the background width of the text. The value of this field equals the background width divided by the font size. The maximum background width is 16, even if the background width calculated based on this field is greater than 16. Default value: 0. 
        // BoxColor: the background color of the text. The format is 0xRGBA. For example, the value 0xff0000ff specifies opaque red. The default value "" specifies that this parameter is invalid. 
        // ShadowxWidthNormalized: the normalized value of the X-coordinate of the text shadow. The value of this field equals the X-coordinate divided by the font size. The maximum X-coordinate is 16, even if the X-coordinate calculated based on this field is greater than 16. Default value: 0. 
        // ShadowyWidthNormalized: the normalized value of the Y-coordinate of the text shadow. The value of this field equals the Y-coordinate divided by the font size. The maximum Y-coordinate is 16, even if the Y-coordinate calculated based on this field is greater than 16. Default value: 0. 
        // ShadowColor: the color of the text shadow. The format is 0xRGBA. For example, the value 0xff0000ff specifies opaque red. The default value "" specifies that this parameter is invalid. 
        r.CaptionLayerContent = `{
            "SizeNormalized": 0.05,
            "Color": "0xFFFFFF",
            "LocationId": "RV01",
            "BorderColor": "0x696969",
            "BorderWidthNormalized": 0.1,
            "BoxColor": "0xffffff",
            "BoxWidthNormalized": 0.7,
            "ShadowColor": "0x3c3c3c",
            "ShadowxWidthNormalized": 0.4,
            "ShadowyWidthNormalized": 0.4,
            "SourceLan": "cn",
            "TargetLan": "en",
            "PtsOffset": -1000,
            "SourceLanPerLineWordCount": 28,
            "TargetLanPerLineWordCount": 60,
            "ShowSourceLan": true,
            "ShowTargetLan": true,
            "Truncation": false,
            "AppearDuration": 20000,
            "AppearMode": "Movie"
        }`
        addCasterComponentResp, err := liveClient.AddCasterComponent(r)
        if err != nil {
            t.Fatal(err)
        }
    
        // Apply the component to a specific scene.
        setCasterSceneConfigRequest := live.CreateUpdateCasterSceneConfigRequest()
        setCasterSceneConfigRequest.LayoutId = "xxxxx"
        setCasterSceneConfigRequest.CasterId = casterID
        setCasterSceneConfigRequest.SceneId = "xxxx"
        setCasterSceneConfigRequest.ComponentId = &[]string{addCasterComponentResp.ComponentId}
        _, err = liveClient.UpdateCasterSceneConfig(setCasterSceneConfigRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    AddCasterComponent操作の詳細については、「AddCasterComponent」をご参照ください。

    SetCasterSceneConfig操作の詳細については、「SetCasterSceneConfig」をご参照ください。

  • プロダクションスタジオを起動して再生シーンを設定

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
        "time"
    		"github.com/go-ini/ini"
    )
    
    // Start the production studio and set the playback scenes.
    func StartCaster(t *testing.T) {
      	cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
    
        // Start the production studio.
        startCasterRequest := live.CreateStartCasterRequest()
        startCasterRequest.CasterId = "xxx"
        startCasterResp, err := liveClient.StartCaster(startCasterRequest)
        if err != nil {
            t.Fatal(err)
        }
    
        // Suspend code execution before the required resources are loaded. If you immediately start the production studio, specific resources may fail to be loaded.
        time.Sleep(time.Second)
    
        // Set the playback scenes.
        // (Optional) Set a PVW scene.
        setCasterSceneConfigRequest := live.CreateSetCasterSceneConfigRequest()
        setCasterSceneConfigRequest.LayoutId = "xxx"
        setCasterSceneConfigRequest.CasterId = "xxx"
        setCasterSceneConfigRequest.SceneId = startCasterResp.PvwSceneInfos.SceneInfo[0].SceneId
        _, err = liveClient.SetCasterSceneConfig(setCasterSceneConfigRequest)
        if err != nil {
            t.Fatal(err)
        }
        // (Required) Set a PGM scene.
        setCasterSceneConfigRequest.SceneId = startCasterResp.PgmSceneInfos.SceneInfo[0].SceneId
        _, err = liveClient.SetCasterSceneConfig(setCasterSceneConfigRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    StartCaster操作の詳細については、「StartCaster」をご参照ください。

    SetCasterSceneConfig操作の詳細については、「SetCasterSceneConfig」をご参照ください。

  • レイアウトの切り替え

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
      	"github.com/go-ini/ini"
    )
    // Switch the layout.
    func ChangeLayout(t *testing.T) {
    		cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
        casterID := "xxxxxx"
    
        // Switch to another layout for a playback scene.
        setCasterSceneConfigRequest := live.CreateUpdateCasterSceneConfigRequest()
        setCasterSceneConfigRequest.LayoutId = "xxxxxxxxxxxx"
        setCasterSceneConfigRequest.CasterId = casterID
        setCasterSceneConfigRequest.SceneId = "xxxxxxxxxxxxxx"
        _, err = liveClient.UpdateCasterSceneConfig(setCasterSceneConfigRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    SetCasterSceneConfig操作の詳細については、「SetCasterSceneConfig」をご参照ください。

  • プロダクションスタジオを停止する

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
      	"github.com/go-ini/ini"
    )
    // Stop the production studio.
    func StopCaster(t *testing.T) {
      	cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
        // Stop the production studio.
        stopCasterRequest := live.CreateStopCasterRequest()
        stopCasterRequest.CasterId = "xxxx"
        _, err = liveClient.StopCaster(stopCasterRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    StopCaster操作の詳細については、「StopCaster」をご参照ください。

  • 制作スタジオの削除

    package caster
    
    import (
        "github.com/aliyun/alibaba-cloud-sdk-go/services/live"
        "testing"
      	"github.com/go-ini/ini"
    )
    // Delete the production studio.
    func DeleteCaster(t *testing.T) {
      	cfg, err := ini.Load("conf/config.ini")
      	if err != nil {
      		t.Fatal(err)
      	}
      
      	// The AccessKey pair of an Alibaba Cloud account has access permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. 
        // We recommend that you not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be compromised. 
        // In this example, the AccessKey pair is obtained from the configuration file to authenticate API accesses. For more information about the configuration file, see the following topic: ApsaraVideo Live > Developer Reference > SDK reference > Server SDKs > Use the server SDK for Go. 
      	accessKeyID := cfg.Section("").Key("access_key_id").String()
      	accessKeySecret := cfg.Section("").Key("access_key_secret").String()
      
      	liveClient, err := live.NewClientWithAccessKey("cn-shanghai", accessKeyID, accessKeySecret)
      	if err != nil {
      		t.Fatal(err)
      	}
        // Delete the production studio.
        deleteCasterRequest := live.CreateDeleteCasterRequest()
        deleteCasterRequest.CasterId = "xxxxxxx"
        _, err = liveClient.DeleteCaster(deleteCasterRequest)
        if err != nil {
            t.Fatal(err)
        }
    }

    DeleteCaster操作の詳細については、「DeleteCaster」をご参照ください。