Stop Worrying About Video Formats: A Developer’s Guide to AWS MediaConvert
If you’ve ever built a feature that involves users uploading videos, you know the pain. One user uploads a 4GB .mov file from an iPhone, another uploads a .webm from a desktop, and your manager wants all of them to play smoothly on a 3G connection in a subway tunnel.
You don't want to manage a fleet of FFMPEG servers. You just want the video to work.
Enter AWS Elemental MediaConvert.
In this guide, we’ll look at what MediaConvert is, how to use it to generate streaming-ready HLS (using .ts segments), and how to build a fully automated pipeline using Lambda and EventBridge to notify your API when the job is done.
Part 1: What is MediaConvert?
Think of MediaConvert as "FFMPEG as a Service," but with enterprise-grade scaling. It is a file-based video transcoding service.
Input: You give it a raw video file in an S3 bucket.
Processing: It converts (transcodes) that file into new formats (MP4, HLS, DASH, etc.).
Output: It dumps the result into another S3 bucket.
Unlike the older "Elastic Transcoder," MediaConvert supports modern standards like HEVC (H.265), 4K/8K, and HDR. It’s serverless, meaning you pay only for the minutes of video you convert.
Part 2: Cooking up HLS (The ".ts" Files)
To stream video efficiently, you shouldn't just serve a single massive MP4 file. You want HLS (HTTP Live Streaming).
HLS breaks the video into tiny chunks (usually .ts files, or Transport Stream segments) and creates a "menu" file called a Manifest (.m3u8). The player reads the menu and downloads chunks one by one.
The Recipe (Console Walkthrough)
Create a Job: Go to the MediaConvert Console and click Create Job.
Input: Select your source file from S3 (e.g.,
s3://my-bucket/raw/video.mp4).Output Groups: This is the important part. Do not click "Add Output" yet. Instead, click Add next to "Output groups" and select Apple HLS.
Why? This tells AWS to package the video specifically for streaming, creating the playlist and segments for you.
Settings:
Segment Length: Default is 10 seconds. For faster seeking, you might lower this to 4 or 6.
Outputs: Inside this group, you can add multiple outputs (renderings).
Output 1: Low resolution (480p) for bad internet.
Output 2: High resolution (1080p) for Wi-Fi.
The "TS" Part: MediaConvert automatically handles the creation of the
.tssegment files. You just need to ensure the Container setting in your output is set toMPEG-2 Transport Stream(which is standard for HLS).Run: Hit "Create".
Result in S3:
playlist.m3u8(The Master Manifest)playlist_1080p.m3u8(Variant Manifest)segment_001.ts,segment_002.ts... (The video chunks)
Part 3: The Automation (Lambda & EventBridge)
MediaConvert jobs take time. A 4K video might take 20 minutes to process. Do not make your API poll the status.
Instead, we use an Event-Driven Architecture.
The Flow
MediaConvert finishes the job.
It emits an event:
MediaConvert Job State Change.Amazon EventBridge catches this event.
EventBridge triggers a Lambda Function.
The Lambda updates your database or sends an email.
Step 1: Create the Lambda
Create a Node.js or Python Lambda function. This function will receive the event JSON, which contains the Job ID and status (COMPLETE or ERROR).
Example Payload (What Lambda receives):
{
"source": "aws.mediaconvert",
"detail-type": "MediaConvert Job State Change",
"detail": {
"status": "COMPLETE",
"jobId": "12345-abcde",
"outputGroupDetails": [
{
"outputDetails": [
{
"outputFilePaths": ["s3://my-bucket/output/playlist.m3u8"]
}
]
}
]
}
}
Step 2: Configure EventBridge
Go to Amazon EventBridge > Rules > Create Rule.
Event Pattern:
Source:
aws.mediaconvertDetail Type:
MediaConvert Job State ChangeStatus:
COMPLETE(You can also make a separate rule forERROR).
Target: Select the Lambda function you created in Step 1.
Part 4: Closing the Loop (Notify API & Email)
Now that your Lambda is triggered, you have two common requirements: notify your backend API (so your app knows the video is ready) and notify the admin via email.
Scenario A: Notify your API (Webhook)
In your Lambda function, use standard HTTP libraries (axios for Node, requests for Python) to call your backend.
Node.js Example:
const axios = require('axios');
exports.handler = async (event) => {
const jobId = event.detail.jobId;
const status = event.detail.status;
const playlistUrl = event.detail.outputGroupDetails[0].outputDetails[0].outputFilePaths[0];
// Tell your Backend API that the video is ready
try {
await axios.post('https://api.my-app.com/webhooks/video-processed', {
aws_job_id: jobId,
status: status,
url: playlistUrl
});
console.log("API Notified");
} catch (error) {
console.error("Failed to notify API", error);
throw error; // Cause Lambda to retry
}
};
Scenario B: Send an Email (SNS)
If you just want a simple email notification, you don't even need Lambda code for the email part. You can add SNS (Simple Notification Service) as a second target in EventBridge.
Create an SNS Topic (e.g.,
VideoTranscodingAlerts).Create a Subscription for that topic: Protocol =
Email, Endpoint =your-email@example.com.Go back to your EventBridge Rule.
Add a Target: Select
SNS topicand chooseVideoTranscodingAlerts.
Now, whenever a video finishes processing, you get an email and your API gets a webhook update.
Summary
Building a video platform doesn't have to be a nightmare of codecs and servers.
MediaConvert handles the heavy lifting of HLS/TS creation.
EventBridge listens for the "Job Complete" signal.
Lambda acts as the glue, telling your API that the video is live.
This architecture is completely serverless, scales to zero when you aren't uploading videos, and handles massive spikes in traffic without you lifting a finger.

Comments
Post a Comment