Learning objectives
After reading this article you will be able to:
- Compare platform embeds, your own server, object storage with a CDN and adaptive streaming
- Estimate a video's bandwidth cost from its bitrate and view count
- Pick the right way to host video for each scenario
On this page
- Which criteria matter?
- How much does a video's bandwidth cost?
- When should you embed the video from a platform like YouTube?
- Can you serve the video from your application server?
- When should you use object storage with a CDN?
- When is adaptive streaming (HLS) worth it?
- Which option fits each scenario?
- How to do this on Square Cloud
- FAQs
Video is the heaviest file a website usually serves. A 200 KB image downloaded a thousand times adds up to 200 MB; a 300 MB video watched a thousand times adds up to 300 GB. So choosing where to host video is, above all, deciding who pays for the bandwidth.
There are four common paths: embedding the video from a platform, serving the file from your application server, serving it from object storage behind a CDN, and adaptive streaming.
Which criteria matter?#
- Length and size. A 15-second background clip and an hour-long lesson call for different solutions. The bigger the file, the more each view weighs.
- Audience. How many people watch, from where and on what network. On unstable mobile networks, being able to drop the quality mid-video makes a difference.
- Control. A player with your branding, no third-party ads or recommendations, access limited to paying users, and download rules.
- Bandwidth cost. Who pays for each GB delivered: you or the platform. With providers that charge for data transfer out, this is the line that dominates the bill, as explained in egress fees.
- Encoding work. Converting the video to a format every browser plays, generating several resolutions and repeating that for every upload.
How much does a video's bandwidth cost?#
The math starts from the bitrate, the amount of data per second of video. Hypothetical example: a 1080p video at 5 Mbps uses 5 ÷ 8 = 0.625 MB per second, or 37.5 MB per minute. If 10,000 people watch 10 minutes each, that is 10,000 × 375 MB = 3.75 TB of egress in a month.
With a provider charging $0.10 per GB of egress (a hypothetical figure, picked to keep the math simple), that comes to about $375 for delivery alone, while storing the file would cost cents. Real prices vary by provider and volume; check the official page, such as Amazon S3's. Three levers bring the bill down: a lower bitrate (the same video at 2.5 Mbps uses half), preload="metadata" so videos nobody plays aren't downloaded, and a CDN with long caching in front of the file.
When should you embed the video from a platform like YouTube?#
Hosting the video on a platform and embedding its player with an <iframe> hands encoding, multi-quality delivery and bandwidth over to the platform. For public marketing videos, tutorials and content that should also be found through the platform's search, it is the cheapest and least work:
<iframe
width="560"
height="315"
src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
title="Product tour"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen
></iframe>The youtube-nocookie.com domain turns on privacy-enhanced mode: according to YouTube Help, views of the embed aren't used to personalize the YouTube experience, and the ads shown are non-personalized. The price is control. The platform's terms apply, there may be ads, the player carries its branding, age-restricted videos won't play on most sites, and the embed has to send an HTTP Referer (without it, playback is blocked with error 153). Paid platforms aimed at businesses offer more control over the player and privacy, with a different pricing model.
Can you serve the video from your application server?#
You can, and it is the quickest path for a prototype: the file sits in a public folder and the server delivers it. Express's express.static, for example, already answers range requests, which let viewers seek without downloading everything. If you write the server from scratch, you have to implement them yourself, as shown in serving video straight from storage.
The problems show up with an audience. Every view takes connections and bandwidth from the application, competing with your API requests. The file comes from a single place, far from part of your viewers. And the video is tied to that machine's disk: with two instances behind a load balancer, each one needs its own copy. Use it for internal videos, prototypes and low traffic; for everything else, see object storage or the app's disk.
When should you use object storage with a CDN?#
For most websites and apps, this is the sweet spot: you upload an MP4 to a bucket, put a CDN in front of it and use the HTML <video> tag. The browser asks for byte ranges with the Range header, the storage answers 206 Partial Content and the viewer seeks without downloading the whole file. The CDN keeps copies close to viewers, and a long Cache-Control keeps each view from going back to the storage.
Prepare the file before uploading. According to MDN, an MP4 with H.264 video and AAC audio is a combination supported by every major browser, and -movflags +faststart moves the video's index (the moov atom) to the start, so playback begins before the download finishes:
ffmpeg -i raw.mov -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k -movflags +faststart video.mp4<video controls playsinline preload="metadata" width="1280" height="720"
poster="https://cdn.example.com/videos/cover.jpg">
<source src="https://cdn.example.com/videos/video.mp4" type="video/mp4">
</video>preload="metadata" asks the browser for the metadata only, such as the duration, until someone presses play (it is a hint the browser may ignore). poster shows an image while the video loads, and explicit width and height avoid the layout shift that counts against CLS. For private videos, such as the lessons of a paid course, keep the file in a private bucket and generate a presigned URL that outlasts the lesson. Large files upload better in parts, with multipart upload.
The limit of this path is a single quality. A viewer on a slow network gets the same 5 Mbps file as someone on fiber, and the video stalls instead of dropping the resolution.
When is adaptive streaming (HLS) worth it?#
In adaptive streaming, the video is encoded at several resolutions and cut into segments a few seconds long. A master playlist lists the versions, and the player switches quality segment by segment based on the network. HLS is the most common format; DASH is the alternative standardized by MPEG. It pays off for long videos, large catalogs, viewers on mobile networks and live broadcasts.
You can generate HLS with ffmpeg. This command creates two quality levels (720p and 360p), with a keyframe every 2 seconds, 6-second segments and a master.m3u8 master playlist (the input video must have audio):
ffmpeg -i video.mp4 \
-filter_complex "[0:v]split=2[a][b];[a]scale=-2:720[v720];[b]scale=-2:360[v360]" \
-map "[v720]" -map "[v360]" -map 0:a -map 0:a \
-c:v libx264 -b:v:0 2800k -b:v:1 800k \
-force_key_frames "expr:gte(t,n_forced*2)" \
-c:a aac -b:a 128k \
-f hls -hls_time 6 -hls_playlist_type vod \
-hls_segment_filename "v%v_%03d.ts" \
-master_pl_name master.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:1" \
"v%v.m3u8"Some browsers play HLS natively in the <video> tag; to cover the rest, the hls.js library plays HLS on top of Media Source Extensions. Its README recommends checking Hls.isSupported() first and falling back to native support (npm install hls.js):
import Hls from "hls.js";
const video = document.querySelector("video");
const src = "https://cdn.example.com/videos/lesson/master.m3u8";
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(src);
hls.attachMedia(video);
} else if (video.canPlayType("application/vnd.apple.mpegurl")) {
video.src = src; // the browser's native support
}Two caveats. According to the hls.js documentation, every HLS file must be delivered with CORS headers that allow GET requests, because the player downloads them through JavaScript. And each video becomes dozens or hundreds of files to upload, version and delete. That is why many projects prefer a video-on-demand service, which takes the original file, transcodes it, builds the playlists and delivers through a CDN. These services usually charge for encoding per minute of output video (the model AWS Elemental MediaConvert uses, for example), plus storage and delivery. Live streaming almost always calls for a dedicated service.
Which option fits each scenario?#
| Scenario | Recommendation |
|---|---|
| Public marketing or tutorial video, focused on reach | YouTube embed in privacy-enhanced mode |
| Short, silent background video on the home page | Light MP4 in object storage behind a CDN (autoplay muted loop playsinline) |
| Product demo or short video in your own player | MP4 with faststart in object storage behind a CDN |
| Lessons of a paid course | Private bucket with presigned URLs; HLS or a video service if the catalog is large |
| Videos uploaded by your app's users | Direct upload to object storage; transcoding in a queue or a service if you need a standard format |
| Long catalog, viewers on mobile networks | HLS with several quality levels, generated by you or by a service |
| Live broadcast | A live streaming service |
| Prototype or internal video with little traffic | File on the application server |
When in doubt, start with an optimized MP4 in object storage behind a CDN and move to HLS when videos get long or viewers complain about stalls. Before publishing, check that the storage answers 206 to a request with Range, as shown in serving video straight from storage.
How to do this on Square Cloud#
Square Cloud's Blob Storage covers the MP4-in-object-storage path. Public files are served by a global edge CDN, and downloads answer range requests with 206, both through the CDN and through the S3 API, so the <video> tag seeks without downloading the whole file. There is no egress or ingress fee, which removes the line that weighs most on video.
Each file can be up to 10 GB, with multipart required above 100 MB. Per object, you set the Cache-Control and whether it opens inline or as a download; per folder, rules such as maximum size and allowed extensions. For private lessons, use the private bucket with a SigV4 presigned URL of up to 7 days, or a temporary link of up to 24 hours. Serving the video from an application's disk works too, but it comes out of that application's dedicated bandwidth, which grows with its RAM (50 Mbps per 256 MB).
Blob doesn't transcode or do adaptive streaming: it delivers the file you upload. Encode the MP4 ahead of time, with faststart, and if you need several quality levels, use a streaming service for that part. Every plan includes a storage quota; each plan's size is on the pricing page.
Blob Storage
S3-compatible object storage with no egress fees
FAQs
Do I need HLS to put a video on my website?
In most cases, no. An MP4 with faststart, served by storage that answers range requests, plays and seeks in the video tag. HLS pays off when videos are long, viewers are on varied networks or you need several quality levels.
Is hosting on YouTube and embedding on my site a good idea?
For public marketing or tutorial videos, yes: the platform encodes and delivers the video at no bandwidth cost to you. In return, its rules apply, there may be ads and you have little control over the player and over who watches.
How much bandwidth does a video use?
Roughly the bitrate times the time watched. A 5 Mbps video uses about 37.5 MB per minute, so 10,000 people watching 10 minutes each add up to about 3.75 TB.
Why can't viewers seek in the video on my site?
Usually the server ignores the Range header and returns 200 with the whole file, or the MP4 has its index (the moov atom) at the end. Test with curl by requesting a byte range and check that the response is 206.
More in Storage
See all 24 articles- What is a presigned (signed) URL?A presigned URL is a temporary link to a private file that works without login or credentials. See how it works, its limits and when to use it.
- What is object storage?Object storage keeps files as objects with a key and metadata, accessed over an HTTP API. See how it works, what it is for and where its limits are.
- What does 'S3-compatible' storage mean?S3 is Amazon's object storage and its API became the industry standard. Learn what S3-compatible means, what differs between providers, and how to connect.
- Uploading large files: multipart and resumable uploadsMultipart upload splits a large file into parts that are sent in parallel and retried on their own. See the Python walkthrough, how to resume, and common mistakes.