Skip to main content
Engineering & Delivery

How to Transcode MP4 to M3U8 using AWS Elemental MediaConvert

Learn how to configure AWS Elemental MediaConvert to transcode MP4 files into HLS (M3U8) adaptive bitrate streams. A technical step-by-step engineering guide.

Delivering high-quality video across varying network conditions requires transitioning from single MP4 files to adaptive streaming protocols like HTTP Live Streaming (HLS). AWS Elemental MediaConvert is an enterprise-grade file-based video transcoding service that seamlessly transforms static video assets into multi-bitrate HLS (.m3u8) manifests.

This comprehensive guide dives deep into the technical workflow for configuring an MP4 to M3U8 conversion using AWS Elemental MediaConvert, providing actionable engineering insights and troubleshooting tips.

Why Use AWS MediaConvert?

Unlike raw FFmpeg commands running on unmanaged EC2 instances, AWS MediaConvert provides a highly scalable, fully managed architecture. Features include:

  • Automated Scaling: Process thousands of files concurrently without managing worker queues.
  • Broad Codec Support: Support for AVC (H.264), HEVC (H.265), AV1, Apple ProRes, and more.
  • Enterprise Security: Integrated DRM (FairPlay, Widevine, PlayReady) and strict IAM access controls.
  • Advanced Video Processing: Frame rate conversion, HDR to SDR tone mapping, and automated cropping.

Prerequisites for Transcoding

Before launching a MediaConvert job, ensure you have the following infrastructure in place:

  • An AWS account with billing enabled.
  • An Amazon S3 bucket containing your source MP4 file.
  • A destination Amazon S3 bucket for the HLS outputs.
  • An IAM Role that grants MediaConvert read access to the input bucket and write access to the output bucket.

Step 1: Configuring the IAM Role

MediaConvert requires an IAM role with the AWSElementalMediaConvertServiceRolePolicy managed policy, along with S3 permissions. The trust relationship must allow mediaconvert.amazonaws.com to assume the role.

Pro Tip: Least Privilege

Avoid attaching broad AmazonS3FullAccess policies. Create a custom inline policy restricting access to only the specific input and output buckets required for transcoding.

Step 2: Defining Input Settings

In the AWS Management Console, navigate to MediaConvert and create a new Job. Under the Inputs section:

  • Select your source MP4 file from S3.
  • Leave Audio Selectors as default unless your MP4 contains multiple distinct audio tracks (e.g., dubs).
  • Enable Video Selector to automatically detect the primary video stream.

Step 3: Creating the Apple HLS Output Group

Navigate to Output Groups and click Add. Select Apple HLS.

In the HLS group settings, specify the S3 destination URI (e.g., s3://my-output-bucket/hls/). Set the Segment length to 6 seconds (the modern standard recommended by Apple for low-latency tradeoffs) rather than the older 10-second default.

For optimal caching and CDN distribution, ensure the Destination S3 bucket has CORS configured to allow GET requests from your web domain.

Step 4: Designing the Codec and Bitrate Ladder

Adaptive bitrate (ABR) streaming requires multiple outputs (renditions) of the video at different resolutions and bitrates. Under your Apple HLS group, add multiple outputs. A standard H.264 (AVC) ladder looks like this:

  • 1080p: 1920x1080, 5000 kbps, High Profile
  • 720p: 1280x720, 2500 kbps, Main Profile
  • 480p: 854x480, 1200 kbps, Main Profile
  • 360p: 640x360, 600 kbps, Baseline Profile

For each output, configure the Video settings with the exact resolution, bitrate, and a 2-pass encoding mode (Quality-Optimized) for better visual fidelity.

Step 5: Managing Audio Streams

Do not forget to define audio! It's best practice in HLS to multiplex the audio into the TS segments for simpler streams, or generate a separate audio-only M3U8 for advanced streams.

Configure the audio output for AAC, 128 kbps, 48 kHz stereo. If using separate audio tracks (e.g., for multi-language), ensure they are explicitly defined in the manifest settings.

Step 6: Content Protection and DRM

If your content requires DRM, navigate to the DRM encryption section within the HLS output group. You will need a SPEKE API endpoint (provided by services like EZDRM or BuyDRM) to negotiate keys. For basic AES-128 encryption, you can manually specify the key provider URI and Static Key value, though this is less secure than robust DRM.

Step 7: Executing and Monitoring the Job

Select your configured IAM Role in the Job Settings, then click Create. The job transitions from SUBMITTED to PROGRESSING. Once COMPLETE, check your S3 bucket.

You should see a master playlist (e.g., output.m3u8), multiple media playlists for each resolution (e.g., output_1080p.m3u8), and the segmented .ts files.

Troubleshooting Common Issues

Job Fails with S3 Error

Verify that your IAM Role has the correct permissions. A "Access Denied" error means MediaConvert cannot read the input MP4 or write to the output bucket.

Playback Stutters

Check your segment lengths and keyframe intervals. Ensure the GOP (Group of Pictures) size matches the segment length (e.g., for 30fps and 6s segments, use a GOP of 180).

CORS Blocked by Browser

M3U8 files load via XHR/Fetch. Ensure your S3 bucket's CORS configuration explicitly allows GET methods and exposes the necessary headers.

AWS Elemental MediaConvert abstracts away the immense complexity of video encoding. By configuring a proper ABR ladder, tuning segment lengths, and securing your output, you can deliver professional-grade HLS streams reliably to any device.