Basic introduction

Youku, IQiyi, Tencent and other mainstream video apps all have the function of offline video download. The main purpose is to offline the video locally under wifi, and then watch the offline video under the condition of no Internet or 4G. So Ali cloud player also provides the function of video download. This feature is mainly for video on demand, i.e. the download of videos played with VID.

The main problem

  1. M3u8 how to download? We know that M3U8 is an index file, and the real video file is a fragment of each TS, so how to download a completed video?
  2. How to multithreading control video download? In some apps, downloading multiple videos at the same time is considered a function for advanced VIPs.
  3. How to implement resumable breakpoint? If there is a sudden break in the download process, then the next time you start up, you should be able to continue.
  4. What should I do if STS information expires during download?
  5. How to ensure the security of encrypted video download to the local?

Realize the principle of

The download process

Ali cloud player supports mp4 files and M3U8 video files in two formats to download. The download process is basically the same. The flow chart is as follows:



As can be seen from the above flow chart, the download of M3U8 files will be mux into an MP4 file. Firstly, each TS file will be downloaded separately, and then MUX will be performed in the last process.

Multithread control

/* Set the number of simultaneous downloads, Max 4 parameters: count: number of simultaneous downloads */ -(void)setMaxDownloadOperationCount:(int)count;

Copy the code

Through the above interface, you can set the number of parallel downloads. When the number of videos added to the queue exceeds the preset number, the system waits in the queue and automatically downloads the next video after the previous download is completed.

abort

It is often the case that the app is accidentally killed, or the phone runs out of power and shuts down, and other interruption factors. At this point, the next download should be able to restore the previous scene. We provide a callback to notify the app:

/* Function: incomplete callback, abnormal interrupt caused download not complete, the next startup will receive this callback. AliyunDownloadMediaInfo */ -(void) onUnFinished:(NSArray<AliyunDataSource*>*)mediaInfos;Copy the code

Date processing

You can download the video using VID and play it in vid+playAuth, VID + STS, and VID + MPS mode. Add the information to the queue. If the previous video takes a long time to download, the input information of the queued video may expire. In this case, we added an out-of-date callback to reenter the information:

/* Function: Start download after receiving a callback, update the latest playAuth. The main scenario is that when multiple downloads are started, the tasks waiting for the download automatically start after the download, playAuth may have expired, need to update parameters through this callback: Return current data Return: use the proxy method, set playAuth to update data. Note: If you want to get playAuth by requesting data, use the synchronization method. This proxy method does not have stuck threads in other threads. */ -(NSString*)onGetPlayAuth:(NSString*)vid format:(NSString*)format quality:(AliyunVodPlayerVideoQuality)quality; /* Function: start download after receiving a callback, update the latest stsData The main scenario is that the stsData may have expired after the tasks waiting to be downloaded automatically start downloading. Use this callback to update parameters: Return current data Return: Use the proxy method to set AliyunStsData to update data. Note: If stsData is obtained by requesting data, use the synchronization method. This proxy method does not have stuck threads in other threads. */ - (AliyunStsData*)onGetAliyunStsData:(NSString *)videoID format:(NSString*)format quality:(AliyunVodPlayerVideoQuality)quality; /* Update the latest MtsData after receiving a callback. The MtsData may have expired after the tasks waiting for the download start automatically. The parameters need to be updated using this callback: Return current data Return: Set AliyunMtsData to update data using the proxy method. Note: If mtsData is obtained by requesting data, use the synchronization method. This proxy method does not have stuck threads in other threads. */ - (AliyunMtsData*)onGetAliyunMtsData:(NSString *)videoID format:(NSString*)format quality:(NSString *)quality;Copy the code

Encrypted downloading

Encrypted download to the local, how to ensure security? There are several problems that we need to re-encrypt after downloading to prevent the key from being leaked. Another is to prevent videos from being copied to other apps for playback. For example, the following scenario exists.



Therefore, we bind the user key to the user APP to ensure security.

So how do you do that?

Console configuration

If you want to achieve encrypted download, you need to configure the download option as in ali Cloud consoleSafe to download. At the same time, fill in the verification and encryption information. Screenshot below:



After filling in, the console will generate a DAT verification file. This verification file needs to be configured in the aliyun download module for verification.

Use dat file

With the DAT file, we set the file to the player via the following interface:

EncrptyFile is the encrypted file path */ -(void)setEncrptyFile:(NSString*)encrptyFile;

Copy the code

Download function Examples

Take Android for example, Android provides the singleton class AliyunDownloadManager to implement the download function.

Configuration AliyunDownloadConfig

Before downloading, AliyunDownloadConfig needs to be configured. The following parameters need to be set in AliyunDownloadConfig:

  • SetMaxNums: Sets the maximum number of simultaneous downloads.
  • SetDownloadDir: Sets the location where the downloaded files are saved.
  • SetSecretImagePath: Sets the path of the verification file. SetSecretImagePath only needs to be set during encrypted download. The other two parameters need to be set.

Get video information and download it

Ali cloud player supports STS, AUTH, MPS and other ways to download. Take STS for example. 1. On the STS information, call the prepare interface to obtain the video item that can be downloaded.

/ / 1. Set the download the listening downloadManager = AliyunDownloadManager. GetInstance (getContext ()); downloadInfoListener = new MyDownloadInfoListener(this); downloadManager.addDownloadInfoListener(downloadInfoListener); //2. Use Vidsts to prepare the download resource. AliyunVidSts adb = new AliyunVidSts(); adb.setVid(mVid); adb.setAcId(akid); adb.setAkSceret(akSecret); adb.setSecurityToken(token); downloadManager.prepareDownloadMedia(adb);Copy the code

  1. After preparing successfully, add an item to AliyunDownloadManager and start downloading:
@ Override void onPrepared (List < AliyunDownloadMediaInfo > infos) {/ / to end downloadManager addDownloadMedia (infos. Get (0)); downloadManager.startDownloadMedia(info); }Copy the code

  1. Accept the download callback to update the interface:
@override public void onPrepared(List<AliyunDownloadMediaInfo> infos) {// Ready to complete} @override public void onPrepared Override public void onProgress(AliyunDownloadMediaInfo info, Int percent) {// Download progress} @override public void onStop(AliyunDownloadMediaInfo info) {// Download stop} @override public void onStop(AliyunDownloadMediaInfo info) onCompletion(AliyunDownloadMediaInfo info) { DemoDownloadActivity downloadActivity = weakActivity.get();if(downloadActivity ! = null) { downloadActivity.onCompletion(info); } } @Override public void onError(AliyunDownloadMediaInfo info, int code, String msg, String reuqestId) {Override public void onWait(AliyunDownloadMediaInfo outMediaInfo) {Copy the code

Remove downloads

AliyunDownloadManager provides a remove interface for removing downloads from download management. After removal, downloaded files will also be deleted.

downloadManager.removeDownloadMedia(info);
Copy the code

For specific usage examples, see demo on the official website

The original link: yq.aliyun.com/articles/68…