Build an elastic audio and video processing system with CloudFlow

Updated at:

Build an elastic, highly available serverless audio and video processing system by using Function Compute and CloudFlow. This topic compares the serverless approach with traditional self-built solutions in terms of engineering efficiency, O&M, performance, and cost, and walks you through deploying a video processing workflow.

Background

For audio and video transcoding, you can use dedicated cloud transcoding services. However, in the following scenarios, you might prefer building your own transcoding service.

  • Auto scaling requirements

    • You need a more elastic video processing service.

      For example, you want to improve the elasticity and availability of an existing FFmpeg-based video processing service that is deployed on a virtual machine or container platform.

  • Engineering efficiency requirements

    • You need to process multiple video files in parallel.

    • You need to quickly process multiple large videos in batches.

      For example, hundreds of 1080p videos, each larger than 4 GB, are generated every Friday and must be processed within a few hours.

  • Custom processing requirements

    • You have advanced custom processing requirements.

      For example, after a video is transcoded, you want to record the transcoding details to a database or automatically preheat popular videos to CDN to reduce pressure on the origin server.

    • You need to convert audio formats, customize sample rates, or perform audio noise reduction.

    • You need to read and process source files directly.

      For example, if your video source files are stored on NAS or an ECS disk, you need to build your own service to directly read and process them, so you do not have to migrate them to OSS.

    • You need to convert videos to other formats and then add new features.

      For example, you transcode videos, add watermarks, and generate GIFs for video thumbnails, and you need to adjust transcoding parameters or deploy new features without disrupting online services.

  • Cost-effectiveness requirements

    • You have simple transcoding or lightweight requirements.

      For example, if you need to extract a GIF from the first few frames of a video on OSS or get the duration of an audio or video file, building your own service is more cost-effective.

You can build your own transcoding service by using a traditional self-built solution or a serverless solution. The following sections compare these two approaches and provide the deployment procedure for the serverless solution.

Solutions

Traditional self-built solution

You can purchase ECS instances to deploy your audio and video processing services and use OSS and CDN for storage and playback acceleration.

image

Serverless solution

Video processing workflow

If you need to accelerate the transcoding of large videos or perform complex combined operations, you can use CloudFlow to orchestrate functions and implement a powerful audio and video processing system. The following figure shows the solution architecture.

image

When a user uploads an MP4 video to OSS, OSS automatically triggers a CloudFlow workflow to transcode the video into one or more formats. This solution meets the following requirements:

  • A video file can be simultaneously transcoded into various formats and undergo other custom processing, such as adding watermarks or updating information in a database after processing.

  • When multiple files are uploaded to OSS at the same time, Function Compute automatically scales to process multiple files in parallel and transcodes the files into various formats in parallel.

  • You can use NAS and video segmentation to transcode oversized videos. This process involves segmenting the video, transcoding the segments in parallel, and then merging the results. By setting a reasonable segment duration, you can improve the transcoding speed of large videos.

    Note

    Video segmentation is the process of splitting a video stream into a series of segment files at specified time intervals and generating an index file that records information about the segments.

Serverless solution advantages

Improved engineering efficiency

Item

Serverless solution

Traditional self-built solution

Infrastructure

None

You must purchase and manage infrastructure.

Development efficiency

Focus on developing business logic, and use Serverless Devs to orchestrate and deploy resources.

Beyond business logic, you must provision and maintain the runtime environment, including software installation, service configuration, and security updates.

Parallel and distributed video processing

Use CloudFlow to orchestrate resources for parallel processing of multiple videos and distributed processing of a single large video. The cloud platform ensures stability and monitoring.

Requires strong development capabilities and a comprehensive monitoring system to ensure stability.

Learning curve

You only need to know how to write function code in a supported language and be familiar with FFmpeg.

Beyond programming skills and FFmpeg familiarity, you may also need Kubernetes and ECS expertise, adding more products, concepts, and parameters to learn.

Time to market

Estimated at 3 person-days (2 for development and debugging, 1 for stress testing and observation).

Requires significant effort beyond business logic development. A conservative estimate is 30 person-days, covering hardware procurement, software and environment configuration, system development, testing, monitoring and alerting, and canary release setup.

Auto scaling and O&M-free

Item

Serverless solution

Traditional self-built solution

Elasticity and high availability

Function Compute provides millisecond-level auto scaling and can quickly scale out underlying resources to handle traffic spikes. It requires no O&M and delivers excellent transcoding performance.

You must build your own auto scaling solution, typically using Server Load Balancer (SLB), which scales more slowly than Function Compute.

Monitoring, alerting, and queries

Provides granular visibility into CloudFlow workflow executions and function invocations, including per-invocation latency and logs, with a comprehensive monitoring and alerting system.

Provides metrics at the auto scaling or container level.

Build a video processing workflow

Prerequisites

Procedure

This solution uses CloudFlow to orchestrate multiple functions into a video processing workflow. The following steps use Serverless Devs to deploy the system.

  1. Go to the multimedia-process-flow template.

  2. Click Deploy to navigate to the application center of Function Compute and start deploying the application.

  3. On the Create Application page, configure the following parameters, and then click Create and Deploy Default Environment.

    The following table describes the key parameters. You can use the default values for other parameters.

    Parameter

    Description

    Basic Information

    Deployment Type

    Select Direct Deployment.

    Role Name

    • If you are using an Alibaba Cloud account and creating an application in the application center for the first time, click Authorize Now. This redirects you to the Role Templates page. Create the AliyunFCServerlessDevsRole role, and then click Confirm.

      When you grant the permissions, make sure that Function Compute is the trusted service.

    • If you are using a RAM user, follow the on-screen instructions to copy the authorization link and have your Alibaba Cloud account owner approve it. After the authorization is complete, click Authorized.

      The page then indicates that the primary account has not yet created a valid AliyunFCServerlessDevsRole role.

      Note

      If the Failed to obtain the role message appears, contact the Alibaba Cloud account owner to grant the AliRAMReadOnlyAccess and AliyunFCFullAccess permissions to the current RAM user. For more information, see Grant permissions to a RAM user by using an Alibaba Cloud account.

    Advanced Configuration

    Region

    Select the region where you want to deploy the application.

    Workflow RAM Role ARN

    The service role used for workflow execution. Create the role in advance and grant it the AliyunFCInvocationAccess policy.

    Function Compute service role

    The service role that Function Compute uses to access other cloud services. We recommend using the default AliyunFCDefaultRole unless you have specific requirements.

    Object Storage Bucket Name

    Enter the name of a bucket in the same region as the workflow and function.

    Prefix

    The prefix of the directory where source videos are stored. This example uses src.

    Directory for transcoded videos

    The destination directory for the transcoded videos. This example uses dst.

    OSS trigger RAM role ARN

    We recommend using the AliyunOSSEventNotificationRole role. If you are using it for the first time, click Create Role to authorize and create the role.

    The application deploys in 1 to 2 minutes and automatically creates five functions and a workflow named multimedia-process-flow-3oih. You can view these resources in the Function Compute console and the CloudFlow console. The five functions are:

    • oss-trigger-workflow: Configures a trigger to detect new video uploads in a specific directory and trigger the workflow.

    • split: Segments a video into parts of a specified length in the working directory.

    • transcode: Transcodes video segments into the specified video formats.

    • merge: Merges the transcoded video segments.

    • after-process: Cleans up the working directory.

  4. Verify the result.

    1. Log on to the CloudFlow console. In the top navigation bar, select a region.

    1. In the workflow list, click the target workflow multimedia-process-flow-3oih. On the workflow details page, click Start Execution. In the panel that appears, enter an Execution Name and the Execution Input. The following code provides an example input:

      {
        "oss_bucket_name": "buckettestfnf",
        "video_key": "source/test.mov",
        "output_prefix": "outputs",
        "segment_time_seconds": 15,
        "dst_formats": [
          "mp4",
          "flv"
        ]
      }

      A status of Succeeded means the execution is complete.

    2. Log on to the Object Storage Service (OSS) console. Open the target bucket and navigate to the /outputs directory to view the transcoded files.

      Transcoded files in this directory confirm that the video processing system is working correctly.

      The transcoded and merged files, merged_test.flv (19.905 MB) and merged_test.mp4 (4.516 MB), are stored in the /outputs/test/ directory.

FAQ

  • What should I do if I have advanced custom processing requirements, such as recording transcoding details to a database or preheating popular videos to CDN to reduce pressure on the origin server?

    For deployment instructions, see Build a video processing workflow. You can add custom processing steps to the workflow, such as adding post-processing or pre-processing steps.

  • My custom video processing workflow combines multiple operations, such as transcoding, watermarking, and generating GIFs from video thumbnails. What should I do if I want to add new features, like adjusting transcoding parameters, without affecting online services?

    For the deployment solution, see Build a video processing workflow. CloudFlow only orchestrates and invokes functions, so you only need to update the corresponding processing function. Functions support versions and aliases, which enable fine-grained canary releases. For more information, see Manage versions.

  • What should I do if I only have simple transcoding or lightweight needs, such as extracting a GIF from the first few frames of a video on OSS or getting the duration of an audio or video file, where building my own service is more cost-effective?

    Function Compute can handle custom requirements. You can accomplish this by running a few FFmpeg commands in your code to meet your needs. For a sample project, see fc-oss-ffmpeg.

  • My source video files are on File Storage NAS or ECS disks. What should I do if I want a self-built service to read the source files directly without migrating them to OSS?

    Function Compute can mount a NAS file system to directly process files stored in NAS. For more information, see Configure a NAS file system.