Play videos

更新时间:
复制 MD 格式

ApsaraVideo Media Processing (MPS) supports two video playback methods: by playback URL and by media ID.

Playback methods

  • Play videos based on their playback URLs

    • Scenario: Transcoded videos have specific output URLs. Pass the OSS URL or CDN URL directly to ApsaraVideo Player SDK.

    • Advantage: The server generates playback URLs and the client uses them directly.

    • Disadvantage: The server must implement OSS or CDN authentication and concatenate playback URLs with domain names. The client cannot switch between resolutions automatically or manually.

  • Play videos based on their media IDs

    • Advantage: Each input media file has a unique media ID. You can associate multiple output formats and resolutions with a media ID, enabling multi-format playback and automatic resolution switching.

    • Playback method: Use Security Token Service (STS) of Resource Access Management (RAM) to play videos by media ID. STS does not use the permissions of your Alibaba Cloud account. Instead, STS grants limited, time-bound permissions to RAM users or RAM roles, preventing permission abuse and data leaks.

    Note
    • Encrypted videos must be played by media ID for security. HTML5 players cannot play encrypted videos. Use a Flash player for encrypted videos on web clients.

    • You can use a media workflow to create a video-on-demand (VOD) file from a live stream. To play the VOD file, call the QueryMediaListByURL operation to query the media ID by specifying the VOD file URL. For more information, see Overview.

Play videos by playback URL

Implement OSS or CDN authentication and concatenate playback URLs with domain names on the server. Then pass the OSS URL or CDN URL to ApsaraVideo Player SDK. For more information about authentication, see the Playback authentication section of this topic.

Play videos by media ID

To play encrypted videos by media ID, use STS of RAM. For more information about STS, see the STS section of this topic.

To encrypt a video:

  1. Build a token service to issue MtsHlsUriToken tokens.

    Important

    The token service issues MtsHlsUriTokens.

  2. Build a decryption service that calls the KMS Decrypt API to obtain and return the DK to the player.

    Important

    KMS returns the Base64-encrypted data key. Your service must Base64-decode it before returning it to the player.

  3. Call QueryMediaList to get the M3U8 file URL. Append the MtsHlsUriToken and return the URL to the player.

  4. The player sends a request with the MtsHlsUriToken to CDN to retrieve the M3U8 file. CDN rewrites the file to include your Key URI and the EDK, then returns it. The player requests the decryption key and plays the video.

STS

  1. To use STS, attach the required policies to a RAM role when authorizing a RAM user. For more information, see Create and authorize a RAM role.

  2. After you create and authorize the RAM user, request a security token. For more information, see Request a security token. For STS SDK examples in other languages, see STS SDK for Java, STS SDK for .NET, STS SDK for Python, STS SDK for Node.js, and STS SDK for Go.

Playback authentication

The following table describes the ApsaraVideo Player SDK parameters.

Parameter

Description

Type

Required

vid

The media ID.

String

Yes

source

The playback URL.

String

Yes

accId

The AccessKey ID used to request a security token.

String

Yes

accSecret

The AccessKey secret used to request a security token.

String

Yes

stsToken

The security token.

String

Yes

domainRegion

The region in which the media workflow resides, such as China (Shanghai) or China (Hangzhou).

String

Yes

authInfo

The authentication information. The value is a JSON string. For more information about the content of the JSON string, see the following description.

String

Yes

The authInfo parameter is a JSON string with three fields:

  • ExpireTime: the expiration time for authentication. Ensure authentication is valid when you obtain the playback URL. Otherwise, you cannot play the video. The time is in UTC. Non-UTC time must be converted into UTC. Use the ISO 8601 format: yyyy-MM-ddTHH:mm:ssZ. Example: 2021-03-25T23:59:59Z.

  • MediaId: the media ID of the file processed by MPS.

  • Signature: the authentication signature for filtering forged requests.

    To calculate the signature:

    • Set the parameters as key-value pairs.

      ExpireTime="2017-03-25T23:59:59Z"
        MediaId="5aa0276ff6204ace950f75acf9e6187b"
    • Create a signature string.

      Sort the key-value pairs alphabetically and join them with ampersands (&) to create the signature string. Values must use RFC 2396-compliant UTF-8 URL encoding. For more information, see URL encoding.

      ExpireTime=2017-03-25T23%3A59%3A59Z&MediaId=5aa0276ff6204ace950f75acf9e6187b
    • Calculate the signature.

      Compute the binary HMAC_SHA1 of the signature string, then Base64-encode the result.

      On UNIX-like systems, use the OpenSSL base64 command. If the authentication key is secret, run the following command:

      $echo -n 'ExpireTime=2017-03-25T23%3A59%3A59Z&MediaId=5aa0276ff6204ace950f75acf9e6****' | openssl sha1 -binary -hmac 'secret' | base64
        z7mmSRuTXo4mydiWhRtbu8JKDpM=
    • Generate the authentication information.

      {
          "ExpireTime":"2017-03-25T23:59:59Z",
          "MediaId":"5aa0276ff6204ace950f75acf9e6****",
          "Signature":"z7mmSRuTXo4mydiWhRtbu8JKDpM="
        }

Player

MPS Player is integrated with ApsaraVideo Player. For more information, see Introduction to player SDK.

Note

You can upgrade to ApsaraVideo Player Pro as needed.