CDN alicloud video accelerated play solution in edusoho network school system

Keywords: PHP SDK network JSON

Edusoho online school connects Alibaba cloud video vod to realize CDN cloud video accelerated playback

Many people click to open the video course after uploading the video through edusoho online school to view the video, which has been loading and cannot be played. They have changed the format strictly according to the method described in the tutorial, and even found many related edusoho posts on the Internet without a solution. In fact, in the final analysis, in general, the video files we upload are saved in the edusoho program's \ app\data\udisk file. The video we play in the foreground accesses our own server. For example, the bandwidth of the server you buy is 4M, If the movie source is stored in its own server, it needs a lot of bandwidth, because when the client plays, it needs to cache the movie from the server to the local area. If there are multiple people watching online at the same time, the bandwidth must be large enough to ensure no card. The bandwidth shall be at least 10M. The relationship between bandwidth and download speed is the current speed of 1M bandwidth = 128K/S. But the cost of 10M bandwidth server is too high, few people will buy it.
Demo address: http://vod.shop1a.cn/
In order to solve the problem of slow video playback of edusoho and control the cost, an effective solution is to develop a video interface for the second time and connect it to the video acceleration server of alicloud, qiniu, Tencent cloud and other large domestic service providers.
Use the account AccessKey to initialize

/**
 * Technical support QQ2446503032
 * Demo address: http://vod.shop1a.cn/
 */
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // Assume that your source file and aliyun PHP SDK are in the same directory
use vod\Request\V20170321 as vod;
function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // On demand service access area
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

Get video upload address and credentials

/**
 * Get video upload address and credentials
 * Technical support QQ2446503032
 * Demo address: http://vod.shop1a.cn/
 * @param client Send request client
 * @return CreateUploadVideoResponse Get video upload address and voucher response data
 */
function createUploadVideo($client) {
    $request = new vod\CreateUploadVideoRequest();
    $request->setTitle("Sample Title");        
    $request->setFileName("videoFile.mov"); 
    $request->setDescription("Video Description");
    $request->setCoverURL("http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"); 
    $request->setTags("tag1,tag2");
    $request->setAcceptFormat('JSON');
    return $client->getAcsResponse($request);
}
try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
    $uploadInfo = createUploadVideo($client);
    var_dump($uploadInfo);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Refresh video upload certificate

/**
 *Technical support QQ2446503032
 *Demo address: http://vod.shop1a.cn/
 * Refresh video upload voucher
 * @param client Send request client
 * @return RefreshUploadVideoResponse Refresh video upload voucher response data
 */
function refreshUploadVideo($client, $videoId) {
    $request = new vod\RefreshUploadVideoRequest();
    $request->setVideoId($videoId);
    $request->setAcceptFormat('JSON');
    return $client->getAcsResponse($request);
}
try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
    $refreshInfo = refreshUploadVideo($client, 'videoId');
    var_dump($refreshInfo);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}


Modify the import-video.html.twig file

Alibaba cloud video on demand (VoD) is a full link VoD service integrating audio and video upload, automatic transcoding, media resource management and distribution acceleration. With the help of flexible and scalable storage, processing and content distribution services, enterprises and developers can quickly build a secure, flexible and highly customizable on-demand platform and application to provide a complete end-to-end solution.
Configure Alibaba cloud's access key ID and access_key_id, access K eyi d and access_key_secret

Through secondary development, the interface for uploading video courses in the background becomes "import alicloud video",

Through your alicloud account, you can clearly see the saved record of the video we just uploaded in the background of edusoho

It can be seen that the video on the computer can be opened and loaded automatically at an extremely fast speed, which has fully practiced the effect of smooth playback

Let's take a look at the playback effect of the mobile phone. Once it is opened, it will load and play directly. There is almost no waiting time

<?php
/**Technical support QQ2446503032
*Demo address: http://vod.shop1a.cn/
 */
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
// Test upload local video
function testUploadLocalVideo($accessKeyId, $accessKeySecret, $filePath)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($filePath, 'testUploadLocalVideo via PHP-SDK');
        //$uploadVideoRequest->setCateId(1);
        //$uploadVideoRequest->setCoverURL("http://xxxx.jpg");
        //$uploadVideoRequest->setTags('test1,test2');
        //$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
        //$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289326');
        $userData = array(
            "MessageCallback"=>array("CallbackURL"=>"https://demo.sample.com/ProcessMessageCallback"),
            "Extend"=>array("localId"=>"xxx", "test"=>"www")
        );
        $uploadVideoRequest->setUserData(json_encode($userData));
        $res = $uploader->uploadLocalVideo($uploadVideoRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}
// Test upload network video
function testUploadWebVideo($accessKeyId, $accessKeySecret, $fileURL)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($fileURL, 'testUploadWebVideo via PHP-SDK');
        $res = $uploader->uploadWebVideo($uploadVideoRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadWebVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}
// Test upload local m3u8 video
function testUploadLocalM3u8($accessKeyId, $accessKeySecret, $m3u8FilePath)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($m3u8FilePath, 'testUploadLocalM3u8 via PHP-SDK');
        // Call the interface to resolve the partition address list of m3u8. If the resolution result is not accurate, please splice the address list by yourself (the default partition file and m3u8 file are in the same directory)
        $sliceFiles = $uploader->parseM3u8File($m3u8FilePath);
        //print_r($sliceFiles);
        $res = $uploader->uploadLocalM3u8($uploadVideoRequest, $sliceFiles);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadLocalM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}
// Test and upload network m3u8 video
function testUploadWebM3u8($accessKeyId, $accessKeySecret, $m3u8FileUrl)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($m3u8FileUrl, 'testUploadWebM3u8 via PHP-SDK');
        // Call the interface to resolve the partition address list of m3u8. If the resolution result is not accurate, please splice the address list by yourself (the default partition file and m3u8 file are in the same directory)
        $sliceFileUrls = $uploader->parseM3u8File($m3u8FileUrl);
        //print_r($sliceFileUrls);
        $res = $uploader->uploadWebM3u8($uploadVideoRequest, $sliceFileUrls);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadWebM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}
####  Execute test code   ####
$accessKeyId = '<AccessKeyId>';
$accessKeySecret = '<AccessKeySecret>';
//$localFilePath = 'C:\test\sample.mp4';
$localFilePath = '/opt/video/sample.mp4';
//testUploadLocalVideo($accessKeyId, $accessKeySecret, $localFilePath);
$webFileURL = 'http://vod-test1.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc95c8/daa30814c0c340cf8199926f78aa5c0e-a0bc05ba62c3e95cc672e88b828148c9-ld.mp4?auth_key=1608774986-0-0-c56acd302bea0c331370d8ed686502fe';
testUploadWebVideo($accessKeyId, $accessKeySecret, $webFileURL);
$localM3u8FilePath = '/opt/video/m3u8/sample.m3u8';
//testUploadLocalM3u8($accessKeyId, $accessKeySecret, $localM3u8FilePath);
$webM3u8FileURL = 'http://vod-test1.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc95c8/daa30814c0c340cf8199926f78aa5c0e-195a25af366b5edae324c47e99a03f04-ld.m3u8?auth_key=1608775606-0-0-9fb038deaecd009dadd86721c5855629';
//testUploadWebM3u8($accessKeyId, $accessKeySecret, $webM3u8FileURL);

Common problem
How to achieve intranet upload?
View the media storage area in the on demand console storage management, deploy the upload script on the ECS of the corresponding area, and then after initializing the AliyunVodUploader class instance, call the setEcsRegionId interface to set the corresponding area, such as CN Shanghai.

How to know the upload progress?
The uploadProgressCallback interface of the AliyunVodUploader class receives the upload progress callback. It can obtain the uploaded media ID (video ID), uploaded size, total size and other information, and rewrite this function to realize its own upload progress callback.

How to turn off log printing?
In the AliyunVodLog class of the AliyunVodUtils.php file, $logSwitch is the log printing switch. If it is changed to false, log printing can be turned off.

Is upload synchronous or asynchronous?
The SDK uploads synchronously, which will block the corresponding process or thread. If you want to achieve asynchronous, it is recommended to use a separate thread to upload.

Support breakpoint resume?
For the time being, it is not supported to renew the breakpoint. When uploading again, a new video (or picture, etc.) will be created; later versions will support it, with the time to be determined.

Does it support execution under Windows?
This SDK supports multiple platforms such as Linux/Unix/Mac/Windows, but PHP and corresponding extensions (such as PHP mbstring) need to be installed.

How to deal with the connection failure?
Please confirm whether there is a problem connecting to the Internet first. ping vod.cn-shanghai.aliyuncs.com to see if you can connect to the alicloud on demand server. If there is a problem connecting, confirm your network configuration, such as whether you can connect to the Internet. Secondly, confirm whether SSL is enabled. If HTTPS request is used, the corresponding extension (such as PHP? OpenSSL) may not be installed correctly, or there is a problem with the configuration.

Published 4 original articles, praised 0, visited 94
Private letter follow

Posted by project3 on Thu, 20 Feb 2020 04:21:19 -0800