The author | west flow Ali cloud technology experts

preface

With the rapid development of computer technology and Internet, video-on-demand technology is favored by education, entertainment and other industries due to its good man-machine interaction and streaming media transmission technology. At present, the product line of cloud computing platform manufacturers is constantly mature and perfect. If you want to build video-on-demand applications, say no to slash-and-burn, Directly on the cloud will clear hardware procurement, technology and other obstacles, take Ali Cloud as an example:

This is a very typical solution. The object storage OSS can support mass video storage. The collected and uploaded videos are transcoded for various terminals, and the CDN accelerates the video playback speed of terminal devices. In addition, there are some content security review requirements, such as yellow, terror, etc.

In voD solutions, video transcoding is a subsystem that consumes the most computing power. Although you can use the special transcoding service on the cloud, in many cases, you will choose to build your own transcoding service. Such as:

  • You already have a video processing service deployed on a VIRTUAL machine/container platform based on FFmpeg. Can you make it more flexible and available?

  • You have the need to process large amounts of video concurrently;

  • You have a lot of large videos that need to be processed quickly in batches. For example, you regularly generate hundreds of 1080P videos with 4G or more every Friday, but you want to process them all in a few hours.

  • You have more advanced customized processing requirements, for example, after the video transcoding is completed, the transcoding details need to be recorded to the database, or after the transcoding is completed, the video with high heat will be automatically preheated to the CDN to relieve the pressure of the source station.

  • There may be many combinations of operations in the custom video processing process, such as transcoding, watermarking, and generating a GIF on the video’s front page. Subsequently, we added new requirements for the video processing system, such as adjusting transcoding parameters, hoping that the release and launch of new functions would have no impact on online services;

  • Your requirements are simple transcoding requirements, or some extremely lightweight requirements, such as capturing the GIF of the first few frames of the video on OSS, capturing the duration of the video or audio, it is cheaper to build your own;

  • Various formats of audio conversion or a variety of sampling rate customization, audio noise reduction and other functions;

  • Your video source files are stored on the NAS or ECS cloud disk. The self-built service can directly read and process the source files without migrating them to OSS.

If your video processing system has these requirements, or you want to achieve a flexible, highly available, low-cost, o&M free video processing system, flexible support for arbitrary processing logic, then this is the best practice you are looking for.

Serverless Custom audio and video processing

Before introducing specific solutions, we will first introduce two products:

  • Function computing: Ali Cloud function computing is an event-driven, fully managed computing service. With functional computation, you don’t need to manage infrastructure such as servers; you just write code and upload it. Functional computing prepares the computing resources for you, runs your code in an elastic, reliable manner, and provides logging queries, performance monitoring, alarms, and more.

  • Functional workflow: Function Flow (FnF) is a fully managed cloud service for coordinating the execution of multiple distributed tasks. You can orchestrate distributed tasks in order, branch, parallel, and so on, and FnF reliably coordinates the execution of tasks according to set steps, tracks state transitions for each task, and performs user-defined retry logic when necessary to ensure smooth workflow completion.

Free open function calculation, pay by volume, function calculation has a large free amount.

Open function workflow for free, pay by volume, function workflow has a large free quota.

The function computes reliably to perform any logic, which can be any processing operation on the video using FFmpeg, or update the meta data on the video to the database, etc.

The function workflow organizes the corresponding functions, for example, the function in the first step is transcoding, and the function in the second step is writing the corresponding meta database into the database after the transcoding succeeds.

At this point, you should have a preliminary understanding of the function computing custom processing capability + function workflow orchestration capability can meet almost any of your custom processing needs. Next, this article uses a concrete example to show a flexible and highly available Serverless video processing system based on function computing and function workflow. The performance, cost and engineering efficiency are compared with those of traditional schemes.

1. Simple video processing system

Assume that you are simply processing video, and the architecture diagram is as follows:

As shown in the figure above, when a user uploads a video to OSS, the OSS trigger automatically triggers the execution of the function, which calls FFmpeg to transcode the video, and saves the transcoded video back to OSS.

OSS event triggers, Ali Cloud object storage and function computing seamless integration. You can set up handlers for various types of events, and when the OSS system catches an event of a specified type, it automatically calls the handler. For example, you can set up functions to handle PutObject events, and when you call the OSS PutObject API to upload a video to OSS, the associated functions are automatically triggered to process the video.

Simple video processing system example project address

Powerful monitoring system:

You can deploy your Simple audio and video processing system service directly based on the sample project, but when you try to process large videos (such as test_vove.mov) or multiple combinations of small videos, you will find that the function fails. The reason is that the execution environment of the function calculation has a maximum execution time of 10 minutes. If the maximum execution time of 10 minutes cannot meet your requirements, you can choose:

  • Video fragmentation -> transcoding -> composite processing. For details, refer to FC-FNF-video-processing, which will be described in detail below.

  • Contact function calculation team (pin group No. : 11721331) or work order:

  • The time limit for execution will be appropriately relaxed

  • Request to use higher function memory 12G (8vCPU)

In order to break through the limitation of function computing execution environment (or speed up the transcoding speed of large video) and carry out various complex combined operations, it is a good scheme to introduce FnF to arrange functions and realize a powerful video processing workflow system.

2. Video processing workflow system

As shown in the figure above, if a user uploads a video in MOV format to OSS, the OSS trigger automatically triggers the execution of the function, which calls FnF, and transcodes one or more formats simultaneously (controlled by the function environment variable DST_FORMATS parameter that you trigger). So you can implement the following requirements:

  • A video file can be transcoded into various formats at the same time as well as a variety of other custom processes, such as adding watermarks or updating information to the database in after-process.

  • When multiple files are uploaded to OSS at the same time, the function computations automatically scale and process multiple files in parallel, as well as transcoding each file into multiple formats in parallel.

  • In combination with NAS + video slicing, transcoding of super-large videos (greater than 3G) can be solved. For each video, slicing is performed first, then parallel transcoding slicing, and finally compositing. By setting reasonable slicing time, transcoding speed of large videos can be greatly accelerated.

The so-called video slicing is to cut the video stream into a series of fragments at a specified time interval, and generate an index file to record the information of the fragments.

Video processing workflow system example project address

Example effects:

3. Functional computation + functional workflow Serverless vs. traditional

1) Excellent engineering efficiency

2) Elastic expansion without operation and maintenance, excellent performance

Function calculation + Function workflow Serverless scheme transcoding performance table:

The experimental video was a mov file of 89s. 4K video: 4K.mov, mov -> MP4 in the cloud service, the general transcoding time was 188s, and this reference time was marked as T.

Performance acceleration = T/FC transcoding time

As can be seen from the above table, the shorter the video slicing time is, the shorter the video transcoding time is. Function calculation can automatically and instantly schedule more computing resources to complete the video transcoding together, with excellent transcoding performance.

3) Lower costs

  • Video processing scenarios with significant peaks and troughs (such as only some times with video processing requests and other times with few or no video processing requests) choose to pay on demand and only pay for the computing resources actually used.

  • Video processing scenarios with no obvious peaks and troughs can be used for pre-paid (annual and monthly packages) and the cost remains extremely competitive.

Function calculates cost optimization best practice documentation

Assume that there is a video transcoding service built based on ECS. Since it is CPU intensive, the average CPU utilization is taken as the core reference index to evaluate the cost. Take the total computing power of 10 C5 ECS as an example in a one-month cycle. When the total computing load is about 30%, the CPU resource usage of the two solutions is as follows:

The following billing model can be estimated from the figure above:

  1. Function calculation prepaid 3CU a month: 246.27 yuan, the computing power is equivalent to ECS computing TYPE C5;

  2. ECS computing C5 (2vCPU,4GB)+ cloud disk: 219 yuan per month;

  3. Function calculation of payment by volume accounted for the whole calculation of less than = 10%, the cost is about 3×864×10% = 259.2 yuan, (3G specification function full load running a full month cost: 0.00011108×3×30×24×3600 = 863.8, see the details of charging)

In the prediction of this model, it can be seen that FC scheme has strong cost competitiveness. In actual scenarios, the CPU utilization of video transcoding service based on ECS can hardly even reach 20%, for the following reasons:

  1. There may be video transcoding requests only in part of the period;

  2. For user experience, video transcoding speed has certain requirements. A video transcoding may require 10 ECS for parallel processing, so only many ECS can be prepared.

Therefore, in actual scenarios, THE cost competitiveness of FC in video processing is much stronger than the above model.

  • Even with the cloud vendors video transcoding service unit PK, the scheme still has a strong cost competitiveness.

Mp4 and FLV are the two formats most commonly used in video on demand. By authority authority, the function memory will be set to 3G. By authority authority, mp4 will be converted to FLV.

The experimental video is in 89S MP4 and FLV format. The test video address is:

480p.mp4, 720p.mp4, 1080p.mp4, 4K.mp4

480p.flV, 720p.FLV, 1080p.flV, 4K.flV

Test commands: ffmpeG-i test.flv test.mp4 and ffmPEg-I test.flv test.mp4

Mp4 to FLV:

Turn FLV mp4:

Cost reduction percentage = (Tencent cloud video processing fee – FC transcoding fee)/Tencent cloud video processing fee Tencent cloud video processing fee, charging uses common transcoding, transcoding duration is less than one minute, calculated according to one minute, charging uses 2 minutes, even if 1.5 minutes is calculated, The percentage of cost reduction fluctuates within 10%.

As can be seen from the above table, the scheme based on functional computation + functional workflow has strong cost competitiveness in computing resource cost for both high computational complexity FLV to MP4 and low computational complexity MP4-FLV to FLV. Based on actual experience, cost decreases are often more obvious than those listed in the above table for the following reasons:

  • The bit rate of the test video is high. In fact, most of the scenes are standard Definition or smooth video transcoding scenes, and the bit rate is lower than that of the test video. At this time, the calculation quantity is reduced, the FC execution time is short, and the cost will be reduced, but the universal cloud transcoding service billing remains unchanged.

  • Many video resolution in the general cloud transcoding service is very poor, such as transcoding video is 856*480 or 1368*768, will enter the next billing price of cloud transcoding service, such as 856*480 into 1280*720 HD transcoding billing file, 1368*768 into 1920*1080 ultra clear transcoding billing file, the unit price is basically a leap up, but the actual real calculation increase may be less than 30%, and function calculation is really able to do according to the calculation of the amount of payment;

  • For example, some transcoding requirements are simply transpackaging (not involving audio and video codec), or only to audio, the amount of calculation decreased by more than 95%.

Operational deployment

Free open function calculation, pay by volume, function calculation has a large free amount.

Open function workflow for free, pay by volume, function workflow has a large free quota.

Free access to file storage service NAS, charge by volume.

See the README for each sample project for details:

  • Simple video processing system example project address

  • Video processing workflow system example project address

conclusion

Elastic high availability video processing systems based on functional computation FC and functional workflow FnF naturally inherit the advantages of both products:

  • No need to purchase and manage servers and other infrastructure, just focus on the development of video processing business logic, greatly shorten the project delivery time and labor costs;

  • Provides log query, performance monitoring, and alarm functions to quickly rectify faults.

  • Trigger responses to user requests in an event-driven manner;

  • O&m free, elastic expansion at millisecond level, rapid expansion at the bottom to cope with peak pressure, excellent performance;

  • The cost is very competitive.

1. Compared with general transcoding services:

  • Super custom, transparent to users, based on FFmpeg or other audio and video processing tools command quickly develop the corresponding audio and video processing logic;

  • The original audio and video processing service based on FFmpeg can be migrated with one key;

  • More flexible, it can ensure sufficient computing resources for transcoding services. For example, hundreds of 1080P videos of more than 4G are generated regularly every Friday, but they are expected to be processed in a few hours on the same day.

  • Various formats of audio conversion or a variety of sampling rate customization, audio noise reduction and other functions, such as professional audio processing tools AACGain and MP3Gain;

  • It can complete more complex and customized task arrangement with Serverless workflow. For example, after video transcoding is completed, the transcoding details are recorded to the database, and the videos with high heat are automatically preheated to the CDN to relieve the pressure of the source station.

  • More event-driven methods, such as OSS automatic triggering (rich triggering rules) or MNS message triggering (support tag filtering) can be selected according to business;

  • Very cost competitive in most scenarios.

2. Compared with other self-built services:

  • Millisecond elastic expansion, super elastic capacity, support large-scale resource call, elastic support tens of thousands of cores. Hours of calculation, such as 10,000 classes in half an hour to complete transcoding;

  • Only need to focus on business logic code, native event-driven mode, simplify the development programming model, and can achieve the priority of message (audio and video tasks) processing, can greatly improve the efficiency of development operation and maintenance;

  • Function computing is deployed in 3AZ with high security and computing resources are obtained from multiple AZs to ensure the peak computing power required by each user.

  • The out-of-the-box monitoring system, as shown in the GIF above, can monitor the execution of functions in a multi-dimensional manner, quickly locate problems according to the monitoring, and provide users with analytical capabilities, such as video format distribution, size distribution, etc.;

  • It is very cost competitive in most scenarios because the calculation in the function is truly pay-as-you-go (billing granularity in hundreds of milliseconds), which can be interpreted as 100% CPU utilization.

Finally, answer the questions listed above:

Q1: You have already deployed a video processing service based on FFmpeg on a VIRTUAL machine/container platform. Can you make it more flexible and available?

A1: As shown in the engineering example, ffMPEg-based services on the VIRTUAL machine/container platform can be easily switched to function computing, FFmpeg commands can be directly moved to function computing, low transformation cost, and natural inheritance of the elastic high availability of function computing.

Q2: Your requirements are simple transcoding requirements, or something extremely lightweight, such as getting a GIF of the first few frames of a video on OSS, etc. It costs less to build your own.

A2: Function computation is designed to solve these custom problems. Your code is your own, and a few quick FFmpeg commands in the code can accomplish the requirements.

Typical example: FC-OSS-FFMPEG

Q3: You have more advanced customized processing requirements, such as recording the transcoding details to the database after the video transcoding is completed, or automatically preheating the videos with high heat to the CDN after the transcoding is completed, so as to relieve the pressure of the source station.

A3: See video Processing Workflow system (function calculation + function workflow solution) for details. You can do some custom operations in after-Process, and you can also do some additional processing based on this process, such as:

  • Add follow-up processes

  • Add pre-process at the beginning

Q4: You have the need to process a large number of videos simultaneously.

A4: See the video Processing Workflow System (functional computing + Functional Workflow solution) for details. When multiple files are uploaded to OSS at the same time, functional computing will automatically scale and process multiple files in parallel. For details, refer to the video Processing Workflow System (FnF + FC) pressure measurement

Q5: You have a lot of large videos that need to be processed quickly in batches. For example, you regularly generate hundreds of 1080P videos with 4G or more every Friday, but you hope to finish processing them all in a few hours on the same day.

A5: For details, please refer to video Processing Workflow System (FnF + FC) pressure measurement. By controlling the size of fragments, each large video can have enough computing resources to participate in transcoding calculation, which greatly improves transcoding speed.

Q6: There may be a variety of operation combinations in the customized video processing process, such as transcoding, watermarking and generating GIF on the video home page. New requirements will be added to the video processing system later, such as adjusting transcoding parameters. It is hoped that the release and launch of new functions will have no impact on online services.

A6: See video processing workflow system (function calculation + function workflow scheme) for details. FnF is only responsible for arranging calling functions, so it only needs to update the corresponding processing functions. Meanwhile, functions have version and alias functions to better control grayscale on-line, and function calculation version management.

Q7: Your video source files are stored on the NAS or ECS cloud disk. The self-built service can directly read and process the source files without migrating them to OSS.

A7: Function calculation can mount NAS and directly process files in NAS.