Skip to content

AWS Serverless Video Transcoding

Dylan Fisher edited this page Nov 13, 2024 · 17 revisions

CloudFormation

  • Go to CloudFormation Stacks.
  • Make sure you are in the correct region before importing the template
  • Create a stack with new resources using the template file located at /config/aws/forest_transcoder.yaml. Set the stack name to forestTranscoder. Specify your app's S3 bucket name as the bucketName parameter.

MediaConvert

  • Go to MediaConvert job templates list, make sure you are in N. Virginia (us-east-1) region.
  • Import new job template named forest-transcode-video using the file located at /config/aws/forest-transcode-video-job-settings.json.
  • Click the "Account"* link, then copy the API endpoint URL. Add this URL to your Rails credentials:
    aws_media_convert_endpoint: https://abc123.mediaconvert.us-east-1.amazonaws.com
    • *This "Account" link seemed to be gone now. Instead, you can use the aws CLI to get it:
      aws mediaconvert describe-endpoints --profile my-aws-profile --region us-east-1
      

ffprobe Lambda Function

This function should be refactored and created in the CloudFormation step.

  • Go to Lambda Layers.
  • Create a new layer named ffprobe. Upload the zip file located at /config/aws/ffprobe.zip
    • Compatible architectures: x86_64
    • Compatible runtimes: select Ruby 3.2 and the latest versions Python and Node.js.
  • Go to Lambda Functions.
  • Create a new function named ffprobe.
    • Runtime: Ruby 3.2, x86_64 architecture.
    • Click "Change default execution role" and create a new role from AWS policy templates. Role name: lambda-s3-read-only. Select the pre-configured Amazon S3 object read-only permissions permission policy.
    • Specify the general configuration timeout to 2 minutes or more. Set the memory to 256mb or more.
    • Edit the function and replace code with /config/aws/ffprobe_lambda_function.rb. Press deploy to save the changes.
    • Add a layer to the function. Select custom layers and choose the ffprobe (version 1) layer we just created.

IAM User Policy

You'll need to update your IAM S3 user with additional permissions to access MediaConvert and Lambda. Use the following JSON to add these permissions to your S3 user. For the lambda:InvokeFunction you'll need the Lambda function resource identifier. You can use the permissions GUI to select the function, or find the resource identifier and paste it into the JSON.

{
    "Effect": "Allow",
    "Action": "lambda:InvokeFunction",
    "Resource": [
        "arn:aws:lambda:us-east-1:123456789:function:TranscodeVideo",
        "arn:aws:lambda:us-east-1:123456789:function:ffprobe"
    ]
},
{
    "Effect": "Allow",
    "Action": [
        "mediaconvert:GetJob"
    ],
    "Resource": "*"
}

Interacting with transcoded videos

Videos that are uploaded to the media library will be automatically transcoded using the resources specified above. Once transcoded, you can interact with the videos using the video_list method, which returns a Forest::VideoList object. Interact with individual videos contained in the Forest::VideoList object with the videos method.

  • MediaItem.videos.last.video_list.videos -> Return a list of transcoded videos at various sizes for this media item.
  • MediaItem.videos.last.video_list.high_res -> Return the high res video for this transcoded video.