Automated process to convert a video file using AWS Mediaconvert

converting video file using AWS Media Convert
aws media convert

Objective

Setting Up an automated process, so that, once any file is uploaded into S3 bucket, if that file is a video file, we need to convert/optimize that file and need to put a logo [watermark] in video and upload into other output buckets.

AWS services used for this

  • Elemental MediaConvert Output Preset
  • Lambda function to execute that media convert queue
  • S3 Bucket [one bucket for input and another for output]
  • SQS/SNS [for notification and queue purpos]

Elemental MediaConvert & Preset File

AWS Elemental MediaConvert is a file-based video transcoding service with broadcast-grade features. It allows you to easily create video-on-demand (VOD) content for broadcast and multiscreen delivery at scale. The service combines advanced video and audio capabilities with a simple web services interface and pay-as-you-go pricing. 
Source : https://aws.amazon.com/mediaconvert/

Basically, you do not need to setup any convertor to the codec or any software like FFmpeg for this. This is all handled by AWS Elemental Media Convert and they provide this on-demand service.

preset

Lambda Function Code

  • This function will be triggered from S3.
  • This package has 23 files, function, job.json and magic.py
  • This will detect the file type, if file type is not video, then move the file without processing and add a message in SQS success queue
  • In the case of video, submit the file for transcoding utilizing job.json and pre-defined preset.
  • In case of success, move file to output bucket and add message into SQS success queue, other wise add message to SQS fail queue.
lambda function

This will trigger the lambda function , whenever a put request or multipart request is done on this bucket. You can put desirable filter for content type / folder structure so that this rule will apply on selected content.
** Lambda should be ready for this .

Environment Variable.

we are passing a media-convert role which should have s3 access to put the object

  • Lambda role should have permission to SQS, media convert and S3, following is the role policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*",
            "Effect": "Allow",
            "Sid": "Logging"
        },
        {
            "Action": [
                "iam:PassRole"
            ],
            "Resource": [
                "<arn for role :demo_mediaconvert>"
            ],
            "Effect": "Allow",
            "Sid": "PassRole"
        },
        {
            "Action": [
                "mediaconvert:*"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow",
            "Sid": "MediaConvertService"
        },
        {
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow",
            "Sid": "S3Service"
        },
 	{
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "sqs:ListQueues",
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "sqs:*",
            "Resource": <arn fail sqs queue : sqs-dc-success>
        },
       
        {
            "Sid": "VisualEditor3",
            "Effect": "Allow",
            "Action": "sqs:*",
            "Resource":  <arn success sqs queue :: sqs-dc-fail>
        }
    ]
}

S3 buckets Setup

let’s create 3 buckets, one for input and the second for output.

  • b2021 [for a static resources like Logo , that will be used in video]
  • b2021-dc-in [input bucket]
  • b2021-dc-out [output bucket]

Steps

  • Put a logo name as logo.png in b2021 bucket : s3://b2021/logo.png (location, filepath, can be changed in lambda job.json file)
  • On input bucket [b2021-dc-in], put a event trigger
    • Go to b2021-dc-in bucket in aws console.
    • Click on properties and scroll down for event notification section
    • cross-check the event type
input bucket event setup

Download Resources

Leave a Reply

Your email address will not be published. Required fields are marked *