# How to stream video from Cloudflare R2 with zero egress fees

> Serve HLS video from an R2 bucket through a Cloudflare custom domain: encode it, upload it, set up CORS and cache rules, and see what delivery costs when egress is free.

Is Cloudflare R2 a good place to host video? Yes, and delivery is where it pays off. R2 charges nothing for egress, so the bandwidth your viewers use does not appear on your storage bill. Encode each video to HLS, upload the files to an R2 bucket, serve them through a custom domain on Cloudflare, and add cache rules for the playlists and segments. R2 stores files; it does not encode them, so you still need FFmpeg or a video API to prepare the streams.

The simplest setup we know is Chunkify with R2: Chunkify encodes each video into your bucket, and its Cloudflare setup configures the domain, cache and CORS for you. Delivery stays on R2, with no egress fees.

<images light="/learn/assets/images/video-hosting/r2-streaming-path.svg" dark="/learn/assets/images/video-hosting/r2-streaming-path-dark.svg" alt="The path from encoding to playback with R2: FFmpeg or a video API writes HLS files to an R2 bucket through the S3 API, a Cloudflare custom domain caches the segments, and the player streams them. Egress from R2 to viewers costs $0."></images>

## Why R2 for video

Video is heavy to deliver. One 10-minute video watched at 1080p is about 0.38 GB per view, so 10,000 views is close to 4 TB of traffic. On most storage providers, that traffic is the largest line on the bill.

R2 Standard storage costs $0.015 per GB-month, writes (Class A operations) cost $4.50 per million, and reads (Class B operations) cost $0.36 per million. Each month includes 10 GB of storage, 1 million writes and 10 million reads for free. Cloudflare states that there are no charges for egress bandwidth for any storage class ([R2 pricing](https://developers.cloudflare.com/r2/pricing/), checked November 30, 2026).

So the costs that grow with your audience are read requests, not gigabytes. And requests that Cloudflare answers from its cache do not need to read from the bucket at all.

## Stream HLS from R2 yourself

### 1. Encode the video to HLS

Use the same FFmpeg ladder as for any other storage. [How to stream video from your own S3 bucket](/learn/video-hosting/stream-video-from-s3#1-encode-an-hls-ladder-with-ffmpeg) explains each option of this command:

```bash
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
```

The `hls` folder gets `master.m3u8`, one playlist per rendition, and 6-second `.ts` segments.

### 2. Upload the files with the S3 API

R2 speaks the S3 API, so the AWS CLI works with an R2 API token and your account's endpoint. In Cloudflare, create an R2 API token with **Object Read & Write** access to the bucket, then upload with explicit content types:

```bash
export AWS_ACCESS_KEY_ID=YOUR_R2_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=YOUR_R2_SECRET_ACCESS_KEY
export AWS_DEFAULT_REGION=auto
R2=https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com

aws s3 cp ./hls s3://YOUR_BUCKET/videos/my-video/ --recursive \
  --endpoint-url "$R2" \
  --exclude "*" --include "*.m3u8" \
  --content-type application/vnd.apple.mpegurl

aws s3 cp ./hls s3://YOUR_BUCKET/videos/my-video/ --recursive \
  --endpoint-url "$R2" \
  --exclude "*" --include "*.ts" \
  --content-type video/mp2t
```

The endpoint is for uploads only. Viewers never use it.

### 3. Connect a custom domain

In the bucket's **Settings → Custom Domains**, add a playback hostname such as `media.example.com`. The domain must be a zone in the same Cloudflare account as the bucket; a partial (CNAME) setup works if your DNS is hosted elsewhere.

Do not use the bucket's `r2.dev` URL for viewers. Cloudflare rate-limits it and intends it for development only, and caching requires a custom domain ([public buckets](https://developers.cloudflare.com/r2/buckets/public-buckets/)).

Connecting a domain makes every object in the bucket readable at its URL. Keep original uploads and anything private in a separate bucket.

### 4. Allow playback from your website

hls.js loads playlists and segments with `fetch`, so the bucket needs a CORS policy for your website's origin. In **Settings → CORS policy**, add:

```json
[
  {
    "AllowedOrigins": ["https://app.example.com"],
    "AllowedMethods": ["GET", "HEAD"],
    "AllowedHeaders": ["Range"],
    "ExposeHeaders": ["Accept-Ranges", "Content-Length", "Content-Range", "ETag"],
    "MaxAgeSeconds": 3600
  }
]
```

The custom domain returns the bucket's CORS headers. If you change the policy after files have been cached, purge the cache so old responses do not keep the previous headers.

### 5. Add cache rules for HLS

This is where HLS matters most on Cloudflare. On Free, Pro and Business plans, Cloudflare caches files up to 512 MB; Enterprise plans start at 5 GB ([cacheable size limits](https://developers.cloudflare.com/cache/concepts/default-cache-behavior/#cacheable-size-limits)). A single 1080p MP4 at 5 Mbps passes 512 MB after about 13 minutes, so long videos served as one file are too large to cache. Each 6-second HLS segment from the ladder above is about 4 MB, well within the limit.

Cloudflare also caches by file extension, and its default list includes `.mp4` but not `.ts` or `.m3u8` ([default cache behavior](https://developers.cloudflare.com/cache/concepts/default-cache-behavior/)). Without a rule, every segment request goes to R2.

In your domain's **Caching → Cache Rules**, create a rule for segments with this expression and set **Cache eligibility** to **Eligible for cache**:

```text
(http.host eq "media.example.com" and ends_with(http.request.uri.path, ".ts"))
```

Segments can then stay cached for a long time. Playlists are not on the default list, so they are read from R2 on every request.

To replace a video, upload the new version to a new path, such as `videos/my-video/v2/`, and point your app to its master playlist. If you overwrite the files at the same path, purge the old playlists and segments from Cloudflare's cache as well. Bypassing the cache for playlists alone is not enough: the new playlists reuse segment names such as `stream_0_000.ts`, and Cloudflare keeps serving the cached segments from the old version.

### 6. Play it and check the cache

Use the master playlist URL with the same hls.js player as for any HLS source, shown in [step 4 of the S3 article](/learn/video-hosting/stream-video-from-s3#4-play-it-in-the-browser):

```text
https://media.example.com/videos/my-video/master.m3u8
```

Then request a segment twice and look at the cache status:

```bash
curl -s -D - -o /dev/null \
  -H 'Origin: https://app.example.com' \
  https://media.example.com/videos/my-video/stream_0_000.ts | grep -i 'cf-cache-status\|access-control'
```

The second request should show `cf-cache-status: HIT` and your origin in `access-control-allow-origin`.

## What to check before you rely on it

- **Serve video from R2, not from another origin.** Cloudflare's CDN terms allow video when it comes from one of its paid services, such as R2 on its Developer Platform. Caching video from a server outside Cloudflare on a Free, Pro or Business plan is what they restrict ([service-specific terms](https://www.cloudflare.com/service-specific-terms-application-services/)).
- **Read requests.** Egress is free, but every request that reaches the bucket is a Class B operation. Cache misses and uncached playlists count; cache hits do not reach R2.
- **Encoding is still yours.** R2 stores and serves files. Every upload still needs the FFmpeg step, a queue to run it, and a way to know when the video is ready.

## The easier route: Chunkify with R2

The encoding is the part that turns into a project: a worker and a queue for every upload, a ladder to tune, posters and previews to generate, and a way to know when each video is ready. A video API takes it over. The difference between them is where the files end up:

- **Cloudflare Stream** encodes and hosts the video in Cloudflare's own storage, and bills per minute stored and delivered. Encoding is free.
- **Chunkify** prepares the video and writes the HLS files to your own R2 bucket, so delivery keeps R2's pricing and the files stay yours. See [Chunkify vs Cloudflare Stream](/compare/chunkify-vs-cloudflare-stream).

Chunkify and R2 fit together: Chunkify does the encoding R2 leaves to you, writes short HLS segments that Cloudflare caches, and configures the Cloudflare side from its dashboard.

### Connect R2 in a few clicks

Chunkify's Cloudflare setup does steps 2 to 5 for you. In your Project, open **Project settings → Storage**, choose **Cloudflare R2**, then **Set up with Cloudflare**:

1. Sign in to Cloudflare and authorize Chunkify to access your account.
2. Pick an existing bucket or name a new one, choose a playback subdomain such as `media.example.com`, and list the websites that will play your videos. Chunkify selects the processing location closest to the bucket.
3. Review the changes, including the complete CORS policy, then apply them. Chunkify creates the bucket if needed, connects the custom domain, adds the cache rule for video segments, and merges the CORS rules with the ones you already have. If the subdomain already has a DNS record, Chunkify shows it instead of replacing it.
4. Paste an R2 access key with **Object Read & Write** access to the bucket. Chunkify uses it to write your videos.

The guided setup is available to Team owners. To configure everything yourself instead, follow the [R2 storage guide](/docs/storage/cloudflare-r2).

### Then one API call per video

Once the bucket is connected, you don't upload files or set up FFmpeg. Create the video from its source:

```typescript
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 to the bucket, and sends an `asset.playback_ready` [webhook](/docs/integration/webhooks) when the video can play. Playback can start before every rendition has finished encoding. The asset's `cdn_url` is the address of its master playlist on your custom domain.

For apps where users upload videos, the [Uploader](/docs/integration/uploader) sends files from the browser straight to your bucket, and [Chunkify Player](/docs/integration/player) plays the stream with its poster and timeline previews.

## What it costs

The same workload as in the S3 article: a 10-minute 1080p video, stored for a month and watched 10,000 times in full at the top quality.

| Setup | Storage | Delivery | Encoding |
| --- | --- | --- | --- |
| R2, encoded yourself | $0: 0.72 GB, within the free 10 GB | $0 egress; about 1 million segment requests, within the free 10 million reads | Your own servers |
| R2, encoded with Chunkify | Same as above | Same as above | $0.30 once on Pay as you go, $0.20 on Team |
| Cloudflare Stream | $5 per month for a 1,000-minute capacity block | $100 for 100,000 minutes delivered | Free |

The R2 rows assume the worst case for reads: every segment request misses the cache. Uploading the video's roughly 300 files uses 300 of the free million writes. Above the free tier, storage costs $0.015 per GB-month and reads $0.36 per million.

Cloudflare Stream prepays storage in blocks of $5 per month for 1,000 minutes, based on duration rather than file size, and charges $1 per 1,000 minutes delivered, with ingress and encoding free ([Stream pricing](https://developers.cloudflare.com/stream/pricing/), checked November 30, 2026).

Chunkify's preparation rate is on the [streaming pricing page](/docs/video-streaming/pricing). Transferring the prepared files to R2 is included up to 50 GiB a month on Pay as you go and 100 GiB on Team, then costs $0.09 per GiB ([data transfer costs](/docs/storage#data-transfer-costs)). This video's 0.72 GB fits well within that allowance.

For the same workload on S3 and CloudFront, see [What it costs](/learn/video-hosting/stream-video-from-s3#what-it-costs) in the S3 article. For more workloads and per-minute platforms, see [Cut your video bill with your own S3 or R2 bucket](/blog/guides/cut-video-costs-with-your-own-bucket).

## FAQ

### Can I use the r2.dev URL in production?

No. Cloudflare rate-limits `r2.dev` URLs and intends them for development. They also cannot use Cloudflare's cache, WAF rules or access controls. Use a custom domain.

### Do cached requests count as R2 operations?

Requests that Cloudflare answers from its cache do not reach the bucket. Requests that miss the cache, or that a rule tells Cloudflare to bypass, are read from R2 and billed as Class B operations after the free 10 million a month.

### Can I keep the bucket private?

Not with a public custom domain: every object becomes readable at its URL. Keep only the HLS outputs in the playback bucket and store original uploads in a separate, private bucket. To restrict who can watch, you need signed URLs in front of the bucket, for example with a Cloudflare Worker. Chunkify does not provide signed URLs or DRM.

### Should I use R2 or Cloudflare Stream?

Stream is the simplest option if you want Cloudflare to encode, store and deliver your videos, and per-minute pricing works at your volume. R2 costs less to deliver as your audience grows and keeps the files in a bucket you own, but it leaves encoding to you.

Chunkify fills that gap. It encodes each video and writes the streams to your R2 bucket, so you work with one API, as with Stream, and keep R2's delivery pricing. Because the files stay in your bucket, you can also change encoding providers later without moving your library.

## Related

- [Video hosting and storage](/learn/video-hosting): more answers on where to keep video and what it costs.
- [Cloudflare R2 storage](/docs/storage/cloudflare-r2) and [Cloudflare CDN](/docs/storage/cloudflare): connect your bucket and playback domain to Chunkify.
- [Chunkify vs Cloudflare Stream](/compare/chunkify-vs-cloudflare-stream): when Stream fits better, and when to keep the files in R2.
- [Video hosting cost calculator](/tools/video-cost-calculator): compare Mux, Cloudflare, Bunny, AWS and R2 at your own numbers.
