- Learn
- Video hosting and storage
- How to stream video from your own S3 bucket
How to stream video from your own S3 bucket
Serve video on demand from Amazon S3 through CloudFront: convert it to HLS with FFmpeg, keep the bucket private, and play it in the browser. Then see what a video API takes off your plate.
Can you do video on demand with just S3 and CloudFront? Yes. Convert each video to HLS, a set of small files at several quality levels, store them in a private S3 bucket, serve them through CloudFront, and play them with an HLS player. The storage and delivery parts are a few hours of AWS setup. The part that takes real work is the conversion: running FFmpeg for every upload, choosing quality levels, and keeping that pipeline running. That is the part a video API replaces, and some, like Chunkify, write the result back to your own bucket.
The quick version: an MP4 in S3
If you only have a few short clips, you can upload an MP4 and point a <video> tag at its CloudFront URL. Browsers request the file in byte ranges, and S3 and CloudFront both support range requests, so playback starts before the whole file downloads and seeking works.
Prepare the file so its index sits at the start, otherwise the browser has to fetch the end of the file before it can play:
ffmpeg -i input.mov \
-c:v libx264 -crf 23 -preset medium \
-c:a aac -b:a 128k \
-movflags +faststart \
output.mp4
This breaks down as your videos get longer and your viewers more varied. There is one quality for everyone: a viewer on a weak mobile connection gets the same 1080p file as a desktop on fiber, and either buffers or wastes bandwidth. For anything beyond short clips, use adaptive streaming.
Stream HLS from S3 yourself
HLS splits each video into short segments, encoded at several resolutions and bitrates, plus playlists that list them. The player measures the connection and switches between quality levels as it plays. Safari plays HLS natively; other browsers use a small JavaScript library such as hls.js.
1. Encode an HLS ladder with FFmpeg
This command produces three renditions, 1080p, 720p and 480p, from one source, with a master playlist that points to each:
mkdir -p hls
ffmpeg -i input.mp4 \
-filter_complex "[0:v]split=3[v1][v2][v3];[v1]scale=-2:1080[v1out];[v2]scale=-2:720[v2out];[v3]scale=-2:480[v3out]" \
-map "[v1out]" -c:v:0 libx264 -b:v:0 5000k -maxrate:v:0 5350k -bufsize:v:0 7500k \
-map "[v2out]" -c:v:1 libx264 -b:v:1 2800k -maxrate:v:1 3000k -bufsize:v:1 4200k \
-map "[v3out]" -c:v:2 libx264 -b:v:2 1400k -maxrate:v:2 1500k -bufsize:v:2 2100k \
-map 0:a -map 0:a -map 0:a -c:a aac -b:a 128k -ac 2 \
-preset veryfast -g 60 -keyint_min 60 -sc_threshold 0 \
-f hls -hls_time 6 -hls_playlist_type vod -hls_flags independent_segments \
-hls_segment_filename "hls/stream_%v_%03d.ts" \
-master_pl_name master.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" \
hls/stream_%v.m3u8
A few choices in it matter more than they look:
scale=-2:1080keeps the source's aspect ratio and rounds the width to an even number, which H.264 requires.-g 60 -keyint_min 60 -sc_threshold 0puts a keyframe exactly every 2 seconds at 30 fps. Use twice your frame rate. Fixed keyframes line up the segments across renditions, so the player can switch quality between segments.-maxrateand-bufsizecap bitrate peaks, so a complex scene does not suddenly need more bandwidth than the rendition advertises.- The command expects an audio track. For a silent video, remove the
-map 0:aoptions and the,a:Nparts of-var_stream_map.
The output goes to the hls folder: master.m3u8, one playlist per rendition, and the 6-second .ts segments they list. The bitrates are a fixed ladder for every video. That is the usual starting point, and also the first thing you will want to improve: simple content such as a screen recording looks fine at a fraction of these bitrates, while fast sports footage needs more.
2. Upload the files to S3
Upload the folder with explicit content types, so players and CDNs recognize the playlists and segments:
aws s3 cp ./hls s3://YOUR_BUCKET/videos/my-video/ --recursive \
--exclude "*" --include "*.m3u8" \
--content-type application/vnd.apple.mpegurl
aws s3 cp ./hls s3://YOUR_BUCKET/videos/my-video/ --recursive \
--exclude "*" --include "*.ts" \
--content-type video/mp2t
Keep Block Public Access turned on. Viewers never read from the bucket directly; CloudFront does.
3. Put CloudFront in front of the bucket
Create a CloudFront distribution with the bucket as its origin and origin access control (OAC), then add a bucket policy that lets only that distribution read your video prefix. The bucket stays private and every request goes through the CDN, where segments are cached close to viewers.
Two settings catch most people out:
- CORS. hls.js loads playlists and segments with
fetch, so the bucket must return CORS headers for your website's origin, and CloudFront must forward theOriginheader and include it in the cache key. - Caching. Segments never change once written and can be cached for a long time. If you re-encode a video, upload it to a new path, or invalidate its old playlists and segments in CloudFront; otherwise viewers keep getting the old files.
Our CloudFront setup guide walks through the distribution, the scoped bucket policy, CORS rules and a cache policy step by step. It is written for Chunkify outputs, but the AWS settings are the same for files you encode yourself.
4. Play it in the browser
<video id="video" controls playsinline></video>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script>
<script>
const video = document.getElementById('video');
const src = 'https://your-distribution.cloudfront.net/videos/my-video/master.m3u8';
if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = src; // Safari and iOS play HLS natively
} else if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(src);
hls.attachMedia(video);
}
</script>
Open the browser's Network panel and check that the master playlist, a rendition playlist and the segments all load from CloudFront with a 200 status.
Where the do-it-yourself setup gets harder
Storage and delivery stay simple. Everything around the FFmpeg command is what grows:
| You need | What it involves |
|---|---|
| Encoding capacity | A worker and a queue for every upload. A 3-rendition ladder is several encodes at once, and long videos tie up a machine for a long time. |
| User uploads | Signed upload URLs, large files, and a way to know when a file has arrived and can be processed. |
| Quality per video | One fixed ladder wastes bandwidth on simple videos and looks poor on complex ones. Tuning it per video means analyzing each one. |
| Posters and previews | A poster image, and thumbnail sprites for the seek bar. |
| Knowing when it is ready | Status tracking and retries, so your app can show the video once playback works and report failures. |
| Keeping it running | FFmpeg upgrades, codec changes, odd input files and capacity spikes. |
If your app takes a handful of videos a month from your own team, a script on a server and the setup above may be all you need. If your users upload videos, this list becomes a product of its own.
Use a video API that writes to your bucket
A video API takes over the encoding pipeline. The main difference between them is where the files end up:
- Hosted platforms such as Mux and Cloudflare Stream store the videos in their own storage and bill you for delivery, usually per minute watched.
- AWS Elemental MediaConvert writes HLS to your S3 bucket. You define the job settings and build the upload handling and job tracking around it. See Chunkify vs AWS MediaConvert.
- Chunkify prepares the video and writes the HLS files to your own S3 bucket, then returns a CloudFront URL for playback. Storage and delivery stay on your AWS bill, at your rates.
With Chunkify, you connect the bucket with an IAM access key once and set your CloudFront domain as the CDN base URL. After that, each video is one API call:
import Chunkify from '@chunkify/chunkify';
const client = new Chunkify({
projectAccessToken: process.env.CHUNKIFY_PROJECT_TOKEN,
});
const source = await client.sources.create({
url: 'https://example.com/path/to/video.mp4',
});
const asset = await client.assets.create({
source_id: source.id,
});
Chunkify chooses the streaming ladder for each video from its content, writes the playlists, segments, a poster and timeline previews under your output prefix, and sends an asset.playback_ready webhook when the video can play. The asset's cdn_url is the CloudFront address of its master playlist, so the player code above works unchanged. Playback can start before every rendition has finished encoding.
For user uploads, the Uploader sends files from the browser straight to storage, and the Next.js guide shows the full flow in an app.
What it costs
Take a 10-minute 1080p video encoded with the ladder above and watched 10,000 times in full, each time at the top quality. That is an upper bound: real viewers drop to lower renditions and stop early.
| Amount | Cost | |
|---|---|---|
| Stored files (3 renditions) | About 0.72 GB | About $0.02 per month on S3 Standard |
| Delivered (10,000 views at 1080p) | About 3.85 TB and 1 million requests | About $240 on CloudFront pay-as-you-go, or $15 on the flat-rate Pro plan |
| Preparing the video with Chunkify | 10 input minutes, HD | $0.30 once on Pay as you go, $0.20 on Team |
The stored size is the ladder's total bitrate, 9.6 Mbps including audio, over 10 minutes. Each view at 1080p is about 0.38 GB and about 100 segment requests. S3 Standard costs $0.023 per GB-month in US East. CloudFront pay-as-you-go includes 1 TB of data transfer each month, then costs $0.085 per GB in the United States, Canada and Europe; the Pro flat-rate plan costs $15 per month and includes 50 TB and 10 million requests (Amazon S3 pricing and CloudFront pricing, checked November 18, 2026). Chunkify rates are on the streaming pricing page; transferring outputs to Amazon S3 is free.
Delivery is the cost that grows with your audience, and it is billed by AWS whichever way you encode. For a full comparison with per-minute platforms and Cloudflare R2, see Cut your video bill with your own S3 or R2 bucket.
FAQ
Can I serve video straight from S3 without CloudFront?
You can, with a public bucket or presigned URLs, but every request then goes to one AWS Region with no edge caching. Viewers far from it wait longer for each segment, and you lose the option to keep the bucket private behind OAC. CloudFront is the usual choice for anything public.
Should my S3 bucket be public?
No. Keep Block Public Access on and give CloudFront read access to your video prefix with origin access control. Keep original uploads outside that prefix so they cannot be read through the CDN.
Does HLS stop people from downloading my videos?
No. HLS makes downloading less convenient, but anyone who can play a video can save it. CloudFront signed URLs or signed cookies control who can request the files. Preventing copies requires DRM, which needs a license service; Chunkify does not provide either.
Should I use MPEG-TS or fMP4 segments?
Both play in current browsers with hls.js and on Apple devices. The .ts segments above are the most widely compatible. fMP4 segments (-hls_segment_type fmp4) are needed for HEVC and AV1 in HLS, and can be shared with DASH.
Does this work with Cloudflare R2 instead of S3?
Yes. The same HLS files work from any storage behind a CDN. R2 charges no egress fees, which changes the delivery line in the table above; the cost guide runs the numbers for both.
Related
- Video hosting and storage: more answers on where to keep video and what it costs.
- Amazon S3 storage and Amazon CloudFront: connect your bucket and CDN to Chunkify.
- Chunkify vs AWS MediaConvert: when to build on MediaConvert, and when to use a video API.
- Video hosting cost calculator: compare Mux, Cloudflare, Bunny, AWS and R2 at your own numbers.