After you enable the preview feature, users can preview a predefined duration of the audio or video file. In most cases, this feature is used for paid media assets, such as lessons and TV series. This topic describes how to enable the preview feature and obtain the preview URL.
How it works
Alibaba Cloud ApsaraVideo VOD provides the preview feature. You can call the GetPlayInfo operation on the server side to specify the preview duration. This operation returns the preview URL. Alternatively, you can construct a signed preview URL that contains the preview information.
If the preview duration that you specified exceeds the total duration of the video, you can use the preview URL that is returned by ApsaraVideo VOD to play the complete video.
The preview feature of Alibaba Cloud ApsaraVideo VOD is implemented based on Alibaba Cloud CDN. The basic principle of the preview feature is that the player uses a CDN-accelerated preview URL that contains information about the specified preview duration to access ApsaraVideo VOD. Before a file is returned, the system must verify the URL. If the preview URL passes the verification, the specified file is returned. Otherwise, access is denied and the HTTP 403 status code is returned. The following figure shows how the preview feature of ApsaraVideo VOD works.
You configure an accelerated domain name for CDN and enable the preview feature.
ImportantBefore you can use the preview feature, you must configure a domain name and enable the preview feature. For more information, see Enable the preview feature.
The player sends a preview request to ApsaraVideo VOD.
ApsaraVideo VOD generates a preview URL based on the domain name settings and the preview duration settings in the preview request.
NoteApsaraVideo VOD allows you to call an API operation on the server side to generate a preview URL or construct a preview URL. For more information, see Call the GetPlayInfo operation to obtain the preview URL or Construct a preview URL.
The player uses the preview URL to request the video from CDN.
Limits
ApsaraVideo VOD allows you to preview video files in the MP4 and HLS formats. The metadata of MP4 files must be contained in the file header. MP4 files whose metadata is contained in the file tail cannot be previewed. When ApsaraVideo VOD transcodes a video and converts its container format to MP4, the system places the metadata in the file header.
The preview duration is determined based on keyframes. We recommend that you do not use the preview feature for short videos. For long videos, we recommend that you set the preview duration to at least 30 seconds. By default, the keyframe interval of transcoded videos is 10 seconds.
NoteYou can modify the keyframe interval in transcoding templates. For more information, see Configure regular transcoding templates.
The preview granularity of HLS files is the duration of each TS segment. The specified preview duration and the actual preview duration of the returned video may be different. The actual preview duration of the returned video is rounded up based on the specified preview duration. For example, if the TS segment duration is 10 seconds and the specified preview duration is 15 seconds, the preview duration of the returned video is 20 seconds.
Enable the preview feature
Before you call an operation to obtain the preview URL or you construct a preview URL, you must configure the domain name and enable the preview feature. To enable the preview feature, perform the following steps:
Configure an accelerated domain name for CDN. For more information, see Add a domain name for CDN.
ImportantTo use the preview feature, an accelerated domain name for CDN must be configured.
Enable URL signing and turn on Support Previewing. For more information, see URL signing.
ImportantIf the preview feature is not enabled for a domain name, requests for video playback cannot include preview parameters and the returned URL cannot be accessed. You must turn on Support Previewing when you configure URL signing.
The following figure shows how to enable the preview feature in the VOD console.
NoteTo manually construct a preview URL, you must specify the privateKey parameter to calculate the authentication value. The value is the Primary Key or Secondary Key value that is generated or entered in the console. Record the preceding values for subsequent use.
Turn on Back-to-origin of Range and Drag/Drop Playback for the domain name. For more information, see Object chunking and Video seeking.
Call the GetPlayInfo operation to obtain the preview URL
ApsaraVideo VOD allows you to call the GetPlayInfo operation to obtain the preview URL of a video. For more information, see GetPlayInfo. You can integrate ApsaraVideo VOD SDKs and use the SDKs to call this operation to obtain the preview URL. To obtain the preview URL, perform the following steps.
Before you call this operation, make sure that the preview feature is enabled. For more information, see Enable the preview feature.
Integrate ApsaraVideo VOD SDKs based on your business requirements. For more information, see ApsaraVideo VOD SDK Quick Start.
Use the SDKs to call the GetPlayInfo operation. You can specify the preview duration by using PreviewTime in PlayConfig when you call this operation. The server returns the preview URL based on the preview duration settings. For more information about the PlayConfig parameter, see Request parameters.
Construct a preview URL
ApsaraVideo VOD allows you to construct a signed URL that contains the preview information. To construct a signed URL, perform the following steps:
Before you construct a preview URL, make sure that the preview feature is enabled. For more information, see Enable the preview feature.
Construct a signed URL that contains the preview parameters. When you calculate the MD5 hash value for a signed preview URL, you must add
previewTime
that is used to calculate the preview duration.MD5 hash calculation for the URL of a video
MD5 hash calculation for a preview URL
MD5(uri-timestamp-rand-uid-PrivateKey)
MD5(uri-timestamp-rand-uid-PrivateKey-previewTime)
NoteFor more information about parameters in MD5 hash calculation and the methods that are used to construct a signed URL, see Configure URL signing. If you want to play the complete video, do not configure the previewTime parameter.
Append
&end=
, followed bypreviewTime
, to the end of the constructed signed URL.