This guide explains how to set up automatic conversion of uploaded videos to MP4 with AWS Elemental MediaConvert.
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
- A member uploads a video (
mov,mp4,webm,ogv,m4v,3gp,aviormpeg) to a post or chat message. - Discourse starts a MediaConvert job that reads the original from your S3 bucket.
- MediaConvert writes an MP4 with H.264 video (2 Mbps constant bitrate, original resolution, rotation corrected) and AAC stereo audio (96 kbps).
- 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.
- 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_regionsetting 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:
-
Create a policy with the following JSON. Replace
=BUCKET=with your uploads bucket name.transcodedis the default value of themediaconvert_output_subdirectorysetting; 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/*" } ] } -
Go to “Roles” and click “Create role”.
-
For “Trusted entity type”, choose “AWS service”. For “Service or use case”, choose “MediaConvert”.
-
Attach the policy you created in step 1.
-
Name the role, for example
discourse-mediaconvert, and click “Create role”. -
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
- Go to “Admin” > “All site settings” and search for
mediaconvert. - Paste the role ARN into
mediaconvert_role_arnand save. - Search for
video_conversionand enablevideo_conversion_enabled. Leavevideo_conversion_serviceset toaws_mediaconvert. - Save the setting.
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
- Upload a short video to a post.
- Wait a minute or two, then reload the page.
- Right-click the video and copy its address. When the conversion has finished, the address points to a different
.mp4file 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.