This document uses the video upload and transcoding process as an example to describe how to use the event notification mechanism of VOD.
An HTTP service is built based on SCF in the demo to receive event notification requests from VOD. It initiates video transcoding and gets the transcoding result by processing
NewFileUpload (video upload completion) and
ProcedureStateChanged (task flow status change) event notifications.
The system mainly involves four components: console, API Gateway, SCF, and VOD. Here, API Gateway and SCF are the deployment objects of this demo.
The specific business process is as follows:
NewFileUploadevent notification request to the demo.
100020preset transcoding templates.
ProcedureStateChangedevent notification request to the demo.
The SCF code in the demo is developed based on Python 3.6. SCF also supports other programming languages such as Python 2.7, Node.js, Go, PHP, and Java for your choice as needed. For more information, please see Development Guide.
The VOD event notification receipt service demo provided in this document is open-source and free of charge, but it may incur the following fees during service building and use:
The business logic of the event notification receipt service demo uses the VOD event notification mechanism; therefore, you need to set the event notification address during deployment. If there is already a VOD-based production environment under your account, changing the event notification address may cause business exceptions. Before doing so, please confirm that this will not affect the production environment. If you are not sure, please use a new account to deploy the demo.
The deployment script needs to be executed on a CVM instance meeting the following requirements:
Ubuntu Server 16.04.1 LTS 64-bitor
Ubuntu Server 18.04.1 LTS 64-bit.
For detailed directions on how to purchase a CVM instance and reinstall the system, please see Operation Guide - Creating Instances via CVM Purchase Page and Operation Guide - Reinstalling System, respectively.
- The event notification receipt service demo itself does not depend on CVM but only uses CVM to run the deployment script.
- If you do not have a CVM instance satisfying the above conditions, you can also run the script on another Linux (such as CentOS or Debian) or macOS server with public network access, but you need to modify certain commands in the deployment script based on the operating system. Please search for the specific modification method by yourself.
Please activate the VOD service as instructed in Getting Started - Step 1.
Your API key (i.e.,
APPID are required for deploying and running the event notification receipt service demo.
APPIDon the Account Information page in the console as shown below:
Log in to the CVM instance prepared in step 1 as instructed in Logging into Linux Instance in Standard Login Method and enter and run the following command on the remote terminal:
ubuntu@VM-69-2-ubuntu:~$ export SECRET_ID=AKxxxxxxxxxxxxxxxxxxxxxxx; export SECRET_KEY=xxxxxxxxxxxxxxxxxxxxx;export APPID=125xxxxxxx;git clone https://github.com/tencentyun/vod-server-demo.git ~/vod-server-demo; bash ~/vod-server-demo/installer/callback_scf.sh
Please assign the corresponding values obtained in step 3 to
APPIDin the command.
This command will download the demo source code from GitHub and automatically run the installation script. The installation process will take several minutes (subject to the CVM network conditions), during which the remote device will print the following information:
[2020-06-05 17:16:08] Start installing pip3. [2020-06-05 17:16:12] pip3 is successfully installed. [2020-06-05 17:16:12] Start installing Tencent Cloud SCF. [2020-06-05 17:16:13] SCF is successfully installed. [2020-06-05 17:16:13] Start configuring SCF. [2020-06-05 17:16:14] SCF configuration is completed. [2020-06-05 17:16:14] Start deploying the event notification receipt service of VOD. [2020-06-05 17:16:24] The event notification receipt service of VOD is deployed. [2020-06-05 17:16:26] Service address: https://service-xxxxxxxx-125xxxxxxx.gz.apigw.tencentcs.com/release/callback
Copy the address of the event notification receipt service in the output log (which is
https://service-xxxxxxxx-125xxxxxxx.gz.apigw.tencentcs.com/release/callback in this example).
If the following warning is displayed in the output log, it is generally because the CVM instance cannot immediately parse the service domain name deployed just now. You can ignore this warning.
[2020-04-25 17:18:44] Warning: the event notification receipt service failed the test.
As described in Avoiding affecting production environment, please confirm that your business in the production environment does not depend on VOD event notifications before performing the following operations:
Log in to the VOD console, click Settings, select Normal Callback as the callback mode, enter the event notification receipt service address obtained in step 4 as the callback URL, check all callback events, and click OK as shown below:
If two callback URL configuration items (v2.0 format and v3.0 format) are displayed at the same time in the console, please configure the v3.0 one
Upload a test video to VOD as instructed in Uploading Video - Local Upload. Select the default option "No Processing After Upload" during the upload. After the upload is completed, you can see that the video status is "Processing" on the "Uploaded" tab, which indicates that the demo has received the
NewFileUpload event notification and initiated a transcoding request.
Wait for the video processing to complete (indicated by the "Normal" status), click Quick View, and you can see that there are two output videos for the uploaded video on the right of the page as shown below:
Log in to the SCF console and enter the log page to view the SCF logs. In the latest log, you can see that the URLs of the two output files have been printed out. In actual use cases, you can use SCF to record the URLs in your database or publish them to viewers through other channels.
SCF log generation may have a certain delay. If no logs are displayed on the page, please wait for one or two minutes and click Reset to refresh.
The event notification receipt function uses API Gateway to provide APIs. For the specific API protocols, please see Video Upload Completion and Task Flow Status Change.
main_handler()is the entry function.
parse_conf_file()and read the configuration information from the
config.jsonfile. The configuration items are as described below:
|region||String||TencentCloud API request region, which can be any region for VOD|
|definitions||Integer Array||Transcoding template|
|subappid||Integer||Whether the event notification is from a VOD subapplication|
NewFileUploadevent notifications, call
deal_new_file_event()to parse the request and get the
FileIdof the newly uploaded video from it.
if event_type == "NewFileUpload": fileid = deal_new_file_event(body) if fileid is None: return ERR_RETURN
trans_media()to initiate transcoding, output the TencentCloud API's response packets to SCF logs, and send the response packets to the event notification service of VOD.
rsp = trans_media(configuration, fileid) if rsp is None: return ERR_RETURN print(rsp)
trans_media(), call the TencentCloud API SDK to initiate the
cred = credential.Credential(conf["secret_id"], conf["secret_key"]) client = vod_client.VodClient(cred, conf["region"]) method = getattr(models, API_NAME + "Request") req = method() req.from_json_string(json.dumps(params)) method = getattr(client, API_NAME) rsp = method(req) return rsp
ProcedureStateChangedevent notifications, call
deal_procedure_event()to parse the request to get the transcoding output video URL and print it to SCF logs:
elif event_type == "ProcedureStateChanged": rsp = deal_procedure_event(body) if rsp is None: return ERR_RETURN
Contact our sales team or business advisors to help your business.
Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.
Was this page helpful?