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.
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.
After the transcoding application is deployed, the following resources will be created:
sls init transcode-app
Enter the project directory transcode-app
, and you will see the directory structure as follows:
transcode-app
|- .env # Environment configuration
|- serverless.yml # Application configuration
|- log/ # Log configuration
| └── serverless.yml
└──transcode/ # Transcoding function configuration
|- src/
| |- ffmpeg # FFmpeg transcoding tool
| └── 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.
# Application information
app: transcodeApp # You need to configure it as your application name
stage: dev # Environment name. Default value: dev
Environment variables are in the transcode-app/.env
file.
REGION=ap-shanghai # Application region
TENCENT_SECRET_ID=xxxxxxxxxxxx # Your Tencent Cloud `sercretId`
TENCENT_SECRET_KEY=xxxxxxxxxxxx # Your Tencent Cloud `sercretKey`
Note:
- You can log in to the Tencent Cloud console and get
SecretId
andSecretKey
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
andSercretKey
. For more information, please see Account and Permission Configuration.
CLS log definition is in the transcode-app/log/serverless.yml
file:
# Component information. For all configuration items, please visit https://github.com/serverless-components/tencent-cls/blob/master/docs/configure.md
component: cls # Name of the imported component
name: cls-video # Name of the created instance. Replace it with the name of your instance
# Component parameters
inputs:
name: cls-log # You need to configure a name as the name of your CLS logset
topic: video-log # You need to configure a topic as the name of your CLS log topic
region: ${env:REGION} # Region, which is uniformly defined in environment variables
period: 7 # Log retention period in days
SCF function and transcoding configurations are in the transcode-app/transcode/serverless.yml
file:
# Component information. For all configuration items, please visit https://github.com/serverless-components/tencent-scf/blob/master/docs/configure.md.
component: scf # Name of the imported component
name: transcode-video # Name of the created instance. Replace it with the name of your instance
# Component parameters
inputs:
name: transcode-video-${app}-${stage}
src: ./src
handler: index.main_handler
role: transcodeRole # Function execution role which has been granted full access to the corresponding COS bucket
runtime: Python3.6
memorySize: 3072 # Memory size in MB
timeout: 43200 # Function execution timeout period in seconds. This example indicates that the demo can be run for up to 12 hours
region: ${env:REGION} # Function region, which is uniformly defined in environment variables
asyncRunEnable: true # Enable prolonged execution
cls: # Function log
logsetId: ${output:${stage}:${app}:cls-video.logsetId} # CLS logset. `cls-video` is the instance name of the CLS component
topicId: ${output:${stage}:${app}:cls-video.topicId} # CLS log topic
environment:
variables: # Transcoding parameters
REGION: ${env:REGION} # Output bucket region
DST_BUCKET: test-123456789 # Output bucket name
DST_PATH: video/outputs/ # Output bucket path
DST_FORMAT: avi # Transcoding format
FFMPEG_CMD: ffmpeg -i {input} -y -f {dst_format} {output} # Basic transcoding command. You can customize the configuration, but it must include the FFmpeg configuration parameters and formatted part; otherwise, the transcoding task will fail
FFMPEG_DEBUG: 1 # Whether to output FFmpeg log. 0: no; 1: yes
TZ: Asia/Shanghai # CLS log output time zone
events:
- cos: # COS trigger
parameters:
bucket: test-123456789.cos.ap-shanghai.myqcloud.com # Input bucket
filter:
prefix: video/inputs/ # Bucket path
events: 'cos:ObjectCreated:*' # Triggering event
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.
In the transcode-app
project directory, run sls deploy
to deploy the project.
cd transcode-app && sls deploy
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.
If you need to adjust the transcoding configuration, you can modify the transcode/serverless.yml
file and redeploy the function:
cd transcode && sls deploy
Batch uploading files to COS will trigger concurrent transcoding executions.
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.
The default FFmpeg tool is provided in the sample transcoding application. If you want to customize FFmpeg, you can do the following:
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.
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.
Log in to the CAM console and create a role with Tencent Cloud Product Service as the role entity.
In the Enter role entity info step, select Serverless Cloud Function (scf) and click Next.
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.
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?