Convert uploaded videos to MP4 with AWS MediaConvert

AI-generated summary

This guide details how administrators can set up automatic video conversion to MP4 using AWS Elemental MediaConvert. This ensures uploaded videos play across all supported browsers, regardless of their original format (e.g., HEVC, VP9). The process requires uploads to be stored in Amazon S3 and an AWS account with permissions to create IAM roles. Note that S3-compatible storage from other providers is not supported.

The workflow begins when a user uploads a video. Discourse triggers a MediaConvert job that re-encodes the source into an H.264 MP4 with 2 Mbps constant bitrate and AAC audio, preserving original resolution. Discourse polls the job status every 30 seconds; upon completion, it stores the new MP4 and updates posts and chat messages to use the converted file. The original file remains untouched. Until conversion completes, the post plays the original format.

Setup involves creating a specific IAM policy and service role for MediaConvert to read the S3 bucket and write to a transcoded subdirectory. Users should verify functionality by uploading a test video and checking that the video source URL changes to a .mp4 file after a few minutes. If conversion fails, errors are logged in /logs but not shown to the user; common issues include missing iam:PassRole permissions or regional incompatibility.

To convert videos uploaded before enabling the setting, administrators can use the Rails console to enqueue conversion jobs for specific uploads. However, there are significant limitations: all videos are re-encoded, even those already in compatible formats, potentially reducing quality for high-res files or increasing size for small ones. Settings for bitrate, resolution, and codec are fixed. Storage usage doubles as both original and converted files are kept. Additionally, AWS charges for MediaConvert usage, so costs should be evaluated for high-traffic sites.

Converted copies inherit the security settings of the original upload. Disabling video_conversion_enabled stops new conversions, though previously converted posts continue to use the MP4 copies, while chat messages revert to playing originals.

:bookmark: This guide explains how to set up automatic conversion of uploaded videos to MP4 with AWS Elemental MediaConvert.

:person_raising_hand: Required user level: Administrator, with access to the AWS account that holds your uploads bucket

Videos recorded on phones and cameras come in many formats. Some of them, such as HEVC or VP9 video, don’t play in every browser. Discourse can send each uploaded video to AWS Elemental MediaConvert and play a converted MP4 copy instead, so videos play in all supported browsers.

How it works

  1. A member uploads a video (mov, mp4, webm, ogv, m4v, 3gp, avi or mpeg) to a post or chat message.
  2. Discourse starts a MediaConvert job that reads the original from your S3 bucket.
  3. MediaConvert writes an MP4 with H.264 video (2 Mbps constant bitrate, original resolution, rotation corrected) and AAC stereo audio (96 kbps).
  4. Discourse checks the job every 30 seconds. When it finishes, Discourse stores the MP4 as a new upload and rebakes every post and chat message that uses the video.
  5. The video player then loads the converted MP4. The original file and the post’s Markdown are unchanged.

Until the conversion finishes, the post plays the original file.

Requirements

  • Uploads stored in Amazon S3. See Set up file and image uploads to S3. S3-compatible storage from other providers won’t work.
  • An AWS account where you can create IAM roles and policies.
  • MediaConvert available in the region of your uploads bucket. Discourse uses the s3_region setting for MediaConvert.

Create the MediaConvert role

MediaConvert needs a service role that lets it read your uploads and write converted files. Create the role in the IAM console:

  1. Create a policy with the following JSON. Replace =BUCKET= with your uploads bucket name. transcoded is the default value of the mediaconvert_output_subdirectory setting; change it here too if you use a different folder.

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "s3:GetObject",
          "Resource": "arn:aws:s3:::=BUCKET=/*"
        },
        {
          "Effect": "Allow",
          "Action": "s3:PutObject",
          "Resource": "arn:aws:s3:::=BUCKET=/transcoded/*"
        }
      ]
    }
    
  2. Go to “Roles” and click “Create role”.

  3. For “Trusted entity type”, choose “AWS service”. For “Service or use case”, choose “MediaConvert”.

  4. Attach the policy you created in step 1.

  5. Name the role, for example discourse-mediaconvert, and click “Create role”.

  6. Copy the role’s ARN. It looks like arn:aws:iam::123456789012:role/discourse-mediaconvert.

Allow Discourse to use MediaConvert

Discourse uses its S3 credentials (access keys or the instance’s IAM profile) to create and check MediaConvert jobs. Add the following permissions to the IAM user or instance role Discourse uses for S3. Replace the role ARN with the one you copied.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "mediaconvert:CreateJob",
        "mediaconvert:GetJob",
        "mediaconvert:DescribeEndpoints"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "iam:PassRole",
      "Resource": "arn:aws:iam::123456789012:role/discourse-mediaconvert",
      "Condition": {
        "StringEquals": { "iam:PassedToService": "mediaconvert.amazonaws.com" }
      }
    }
  ]
}

Discourse also copies each converted file from the transcoded folder to its final location and deletes the temporary copy. The S3 policy in Set up file and image uploads to S3 already allows this.

Enable video conversion

  1. Go to “Admin” > “All site settings” and search for mediaconvert.
  2. Paste the role ARN into mediaconvert_role_arn and save.
  3. Search for video_conversion and enable video_conversion_enabled. Leave video_conversion_service set to aws_mediaconvert.
  4. Save the setting.

:information_source: Discourse refuses to enable video_conversion_enabled until mediaconvert_role_arn is set and S3 credentials are configured, either access keys or s3_use_iam_profile.

Check that it works

  1. Upload a short video to a post.
  2. Wait a minute or two, then reload the page.
  3. Right-click the video and copy its address. When the conversion has finished, the address points to a different .mp4 file than the one you uploaded.

If the address doesn’t change, check the error logs at /logs for messages that mention MediaConvert or video conversion. Common causes are a missing iam:PassRole permission, a role that can’t read the bucket, or MediaConvert not being available in your bucket’s region.

Convert existing videos

Only videos uploaded after you enable the setting are converted. To convert an older video, use the Rails console and pass the video’s URL:

upload = Upload.get_from_url("https://discourse.example.com/uploads/default/original/1X/example.mov")
Jobs.enqueue(:convert_video, upload_id: upload.id)

Discourse skips videos that already have a converted copy.

Limitations

  • Every video is re-encoded, including MP4 files that already play everywhere. The output is always 2 Mbps H.264 at the original resolution, so high-resolution videos lose detail and very small files can get larger.
  • The settings are fixed. You can’t change the bitrate, resolution or codec.
  • Storage grows. Discourse keeps the original and stores the converted copy alongside it.
  • Failures are only logged. If a conversion fails, the post keeps playing the original video and nothing is shown to the member who uploaded it.
  • MediaConvert costs money. AWS charges per minute of converted video. Check MediaConvert pricing before enabling this on a site with a lot of video.

Converted copies follow the original upload’s security settings, so videos in secure categories stay secure.

If you turn off video_conversion_enabled, Discourse stops converting new videos. Posts that were already converted keep playing the converted copies. Chat messages go back to playing the originals.

Additional resources