Application Overview
By using COS + SCF + CLS + FFmpeg, you can quickly build a highly available and customizable video transcoding service that features concurrent processing and real-time logging.
Architecture
Create an FFmpeg task process through SCF. The SCF process and FFmpeg task process carry out data transfer through pipe and FIFO. The two task threads in the SCF process respectively receive the FFmpeg log stream output by the FFmpeg task process to the SCF process and the transcoded file stream. The real-time logging thread outputs the log stream, and the upload task is responsible for caching the file stream and uploading it to the user-defined destination COS bucket.
Application advantages
Streaming transcoding
The source video file is pulled and the transcoded file is uploaded in a streaming manner, which breaks through the limitation of local storage and eliminates the need for additional deployment of CFS and other services.
Real-time logging
During video transcoding, you can view the transcoding progress in real time through CLS logs. In addition, this scheme supports outputting the complete logs of the FFmpeg application.
Prolonged execution
By leveraging the prolonged execution mechanism of SCF, this scheme can be run for 12 to 24 hours, making it suitable for transcoding large files that take a long time. Custom parameters
You can customize the FFmpeg command parameters.
Application resources
After the transcoding application is deployed, the following resources will be created:
SCF function: it reads COS files, outputs files transcoded with FFmpeg to COS, and outputs the real-time log of the transcoding process to CLS in a streaming manner.
CLS log: it stores the real-time log of the transcoding process. CLS logs may incur fees. For more information, please see CLS Billing Rules. Notes
The transcoding application relies on the prolonged function execution capability. For more information, please see Async Execution. We recommend you configure the transcoding output bucket and the function in the same region, because cross-region configuration will reduce the stability and efficiency of the transcoding application and incur traffic fees.
You need to create an execution role for the function of the transcoding application and grant it the read/write permissions of COS. For detailed configuration, please see Execution Role. FFmpeg command parameter configuration varies by transcoding scenario; therefore, you should have basic knowledge of FFmpeg. This document only provides a few samples for your reference. For more FFmpeg commands, please visit FFmpeg official website. Prerequisites
Directions
1. Download the transcoding application
Enter the project directory transcode-app
, and you will see the directory structure as follows:
transcode-app
|- .env
|- serverless.yml
|- log/
| └── serverless.yml
└──transcode/
|- src/
| |- ffmpeg
| └── index.py
└── serverless.yml
log/serverless.yml
defines a CLS logset and topic, which are used to store the logs output during the transcoding process. Currently, CLS is used for log storage. Each transcoding application will create related resources based on the configured CLS logset and topic. Using CLS will incur fees. For more information, please see CLS Billing Rules.transcode/serverless.yml
defines the basic function configuration and transcoding parameter configuration.
transcode/src/index.py
implements the transcoding feature.
transcode/src/ffmpeg
is the Ffmpeg transcoding tool.
Application parameters are in the transcode-app/serverless.yml
file.
app: transcodeApp
stage: dev
Environment variables are in the transcode-app/.env
file.
REGION=ap-shanghai
TENCENT_SECRET_ID=xxxxxxxxxxxx
TENCENT_SECRET_KEY=xxxxxxxxxxxx
Note:
You can log in to the Tencent Cloud console and get SecretId
and SecretKey
in API Key Management. If your account is the root account or a sub-account that has the permission to scan QR codes, you can also directly scan the QR code to deploy the application without configuring SercretId
and SercretKey
. For more information, please see Account and Permission Configuration. CLS log definition is in the transcode-app/log/serverless.yml
file:
component: cls
name: cls-video
inputs:
name: cls-log
topic: video-log
region: ${env:REGION}
period: 7
SCF function and transcoding configurations are in the transcode-app/transcode/serverless.yml
file:
component: scf
name: transcode-video
inputs:
name: transcode-video-${app}-${stage}
src: ./src
handler: index.main_handler
role: transcodeRole
runtime: Python3.6
memorySize: 3072
timeout: 43200
region: ${env:REGION}
asyncRunEnable: true
cls:
logsetId: ${output:${stage}:${app}:cls-video.logsetId}
topicId: ${output:${stage}:${app}:cls-video.topicId}
environment:
variables:
REGION: ${env:REGION}
DST_BUCKET: test-123456789
DST_PATH: video/outputs/
DST_FORMAT: avi
FFMPEG_CMD: ffmpeg -i {input} -y -f {dst_format} {output}
FFMPEG_DEBUG: 1
TZ: Asia/Shanghai
events:
- cos:
parameters:
bucket: test-123456789.cos.ap-shanghai.myqcloud.com
filter:
prefix: video/inputs/
events: 'cos:ObjectCreated:*'
enable: true
Note:
We recommend you configure the output bucket and the function in the same region, because cross-region configuration will reduce the stability and efficiency of the application and incur traffic fees.
The upper limit of memory size is 3,072 MB, and the upper limit of execution time is 43,200s. If you need to adjust them, please submit a ticket for application. The prolonged execution feature of SCF must be enabled for the transcoding application with asyncRunEnable: true
.
Please create and authorize the execution role according to Execution Role. The FFmpeg command configured in the example is only applicable to AVI transcoding. For more information, please see FFmpeg commands. 4. Deploy the project
In the transcode-app
project directory, run sls deploy
to deploy the project.
cd transcode-app && sls deploy
5. Upload the video file
Upload the video file to the specified path of the configured COS bucket, and it will be automatically transcoded, which is /video/inputs/
in the COS bucket test-123456789.cos.ap-shanghai.myqcloud.com
in this example.
After successful transcoding, the file will be saved in the output bucket path you configured, which is /video/outputs/
in the COS bucket test-123456789.cos.ap-shanghai.myqcloud.com
in this example.
6. Redeploy
If you need to adjust the transcoding configuration, you can modify the transcode/serverless.yml
file and redeploy the function:
cd transcode && sls deploy
Monitoring and Logging
Batch uploading files to COS will trigger concurrent transcoding executions.
1. Go to the Function Service page in the SCF console and click the function name to enter the function management page. 2. Click Log Query to view the log monitoring data.
3. Select Function Management > Function configuration and click the link of the log topic to redirect to the CLS console.
4. On the Search and Analysis page in the CLS console, select the logset and log topic to search for and analyze logs.
FFmpeg commands
DST_FORMAT
and FFMPEG_CMD
in the transcode-app/transcode/serverless.yml
file specify the transcoding commands of the transcoding application. You can customize the configuration according to your actual use case.
For example, to transcode videos to MP4 format, you can configure FFMPEG_CMD
as follows:
DST_FORMAT: mp4
FFMPEG_CMD: ffmpeg -i {input} -vcodec copy -y -f {dst_format} -movflags frag_keyframe+empty_moov {output}
Note:
FFMPEG_CMD
must include the FFmpeg configuration parameters and formatted part; otherwise, the transcoding task will fail.
FFmpeg command parameter configuration varies by transcoding scenario; therefore, you should have basic knowledge of FFmpeg. The above commands are only for the described use cases. For more FFmpeg commands, please visit FFmpeg official website. Customizing FFmpeg
The default FFmpeg tool is provided in the sample transcoding application. If you want to customize FFmpeg, you can do the following:
1. Replace the FFmpeg in the sample with your customized FFmpeg.
2. Run sls deploy
in the transcode-app/transcode
directory again to deploy the update.
cd transcode && sls deploy
Note:
If your FFmpeg compilation environment is different from the SCF runtime environment, FFmpeg permission issues may occur. We provide an official image of the SCF runtime environment, which can be used to compile your FFmpeg. Execution Role
When the transcoding function is running, it needs to read resources in COS for transcoding and write the transcoded resources back to COS; therefore, the function should be configured with an execution role that has read/write permissions of COS. For more information, please see Role and Authorization. 1. Log in to the CAM console and create a role with Tencent Cloud Product Service as the role entity. 2. In the Enter role entity info step, select Serverless Cloud Function (scf) and click Next.
3. In the "Configure role policy" step, select the policies required by the function and click Next.
Note:
You can directly select QcloudCOSFullAccess
(full access to COS). If you need more fine-grained permission control, please configure and select according to the actual situation.
4. Enter the role name to complete the role creation and authorization. This role will be used as the execution role of the function and configured in the transcode-app/transcode/serverless.yml
file.
Note:
As the maximum validity period of an execution role key is 12 hours, the timeout period configured for the function should not be greater than 12 hours. If you need a longer function execution time, you can change the COS access method in transcode-app/transcode/src/index.py
to configure a permanent key for full access to COS. However, this will expose your key in the code and should be used with caution.
Was this page helpful?