Install ffmpeg
Get ffmpeg from the official ffmpeg download page. The project publishes source code, and the page links to ready-made builds for Windows, macOS and Linux.
- Windows: download a build linked from that page and unzip it. Add its
binfolder to your PATH, or run the commands from inside that folder. - macOS: use a build linked from that page, or install it with Homebrew:
brew install ffmpeg. - Linux: install it from your distribution's packages, for example
sudo apt install ffmpegon Debian and Ubuntu.
Open a terminal (Terminal on a Mac, PowerShell or Command Prompt on Windows) and run ffmpeg -version. If it prints a version number, you are ready.
The basic command
ffmpeg -i "https://media.example.gov/council/master.m3u8" -c copy meeting.mp4
-isets the input: the playlist's address. Keep it in double quotes. Links often contain?and&, which your terminal would otherwise treat as part of the command.-c copycopies the audio and video as they are instead of re-encoding them (decoding and compressing them again). That makes the job fast, often limited only by your download speed, and keeps the original quality exactly.meeting.mp4is the output file. Its extension tells ffmpeg which container (file type) to write.
When you give ffmpeg a master playlist and don't say which quality you want, its automatic stream selection picks the video with the highest resolution and the audio with the most channels.
Save part of a video, or only the audio
Trim
ffmpeg -ss 00:12:30 -i "https://media.example.gov/council/master.m3u8" -t 00:45:00 -c copy part.mp4
-ss 00:12:30placed before-imakes ffmpeg seek in the input. For an on-demand playlist it jumps to the piece that contains 12 minutes 30 seconds instead of downloading everything before it.-t 00:45:00placed after-istops writing once the output is 45 minutes long.
With -c copy, ffmpeg can't cut between keyframes (full pictures that the frames after them depend on). The ffmpeg documentation explains that it seeks to the closest point before your start time and keeps that extra bit, so the clip may begin a few seconds early.
Audio only
ffmpeg -i "https://media.example.gov/council/master.m3u8" -vn -c:a copy meeting-audio.m4a
-vn leaves out the video, and -c:a copy copies the audio without re-encoding. Most HLS audio is AAC, which fits in an .m4a file. If the stream carries its audio inside the video pieces, as many .ts streams do, ffmpeg still has to download those pieces, but it saves only the sound.
Choose a quality from a master playlist
ffmpeg's HLS reader presents each quality in a master playlist as a numbered program, starting at 0, in the order they appear in the playlist. To see them, run ffmpeg with an input and no output:
ffmpeg -hide_banner -i "https://media.example.gov/council/master.m3u8"
It prints the programs, then complains that no output file was given, which is expected. The list looks like this (shortened):
Program 0
Metadata:
variant_bitrate : 1400000
Stream #0:0: Video: h264 ..., 854x480
Stream #0:1: Audio: aac ...
Program 1
Metadata:
variant_bitrate : 2800000
Stream #0:2: Video: h264 ..., 1280x720
Stream #0:3: Audio: aac ...
Then pick a program with -map:
ffmpeg -i "https://media.example.gov/council/master.m3u8" -map 0:p:1 -c copy meeting-720p.mp4
-map 0:p:1 means "from the first input (0), take every stream in program 1". If ffmpeg complains about a subtitle stream, add -sn to leave subtitles out.
The other way is to skip the master playlist. Each #EXT-X-STREAM-INF line in it is followed by the address of that quality's own playlist. Give that address to ffmpeg. If it is relative, such as 720p/index.m3u8, put it after the last / of the master playlist's address.
Record a live stream
ffmpeg -i "https://live.example.gov/chamber/live.m3u8" -c copy floor-session.ts
- ffmpeg keeps reloading the live playlist and saving new pieces until the stream ends or you stop it.
- To stop, click the terminal window and press q. ffmpeg finishes writing the file and exits. It reminds you of this with "Press [q] to stop" when it starts.
- By default ffmpeg starts near the end of a live playlist. To start from the oldest piece the playlist still lists, add
-live_start_index 0before-i.
Saving to .ts is safer than .mp4 for long recordings. An MP4 file's index is written at the very end, so if the recording is cut off, for example because the computer sleeps or the window is closed, the MP4 may not play. A .ts file keeps everything recorded up to that point. Convert it afterwards with TS to MP4, or with ffmpeg:
ffmpeg -i floor-session.ts -c copy floor-session.mp4
Adding headers your own session uses
Some servers expect a header that your browser sends when you watch the video on its own page, such as Referer (the address of the page that asked for the video). ffmpeg's HTTP options include -headers, which you place before -i:
ffmpeg -headers "Referer: https://www.example.gov/meetings/" -i "https://media.example.gov/council/master.m3u8" -c copy meeting.mp4
ffmpeg expects each header to end with a line break. If the last one doesn't, it adds one and prints a warning, which you can ignore. For a single Referer or User-Agent, the -referer and -user_agent options do the same job with less typing.
Common errors and fixes
Invalid data found when processing input
ffmpeg couldn't read what the address sent back as a playlist or video. Usually the address is a web page rather than the .m3u8 itself, the link has expired and the server sent an error page, or part of the address was lost when copying. Copy a fresh link (see how to find the m3u8 link) and check that the whole address is inside the quotes.
Server returned 403 Forbidden (access denied)
The server refused the request. Most often the link's token has expired: copy a fresh link from the video page and run the command soon after. If the video is limited to certain regions or needs access you don't have, ffmpeg can't change that. Why an m3u8 download fails covers 403, 404 and other errors in detail.
Protocol 'https' not on whitelist 'file,crypto,data'
This appears when the input is a playlist saved on your computer that lists pieces online. For safety, ffmpeg lets a local playlist open only local files unless you allow more. The better fix is to use the playlist's original web address. If you need the local copy, add -protocol_whitelist before -i:
ffmpeg -protocol_whitelist file,http,https,tcp,tls -i playlist.m3u8 -c copy output.mp4
Pieces listed with relative addresses, like segment0.ts, are looked for next to the saved file, so they won't be found unless they are there too.
An error that mentions aac_adtstoasc
Older ffmpeg versions can stop with an error that names the aac_adtstoasc filter when copying AAC audio from .ts pieces into MP4. Current versions apply this bitstream filter automatically. Updating ffmpeg is the best fix. Otherwise, add it by hand:
ffmpeg -i "https://media.example.gov/council/master.m3u8" -c copy -bsf:a aac_adtstoasc meeting.mp4
Questions
Does ffmpeg lower the quality with -c copy?
No. With -c copy the audio and video are copied exactly as the server sent them. Only the container changes, from .ts or fMP4 pieces to one MP4 file.
Where does ffmpeg save the file?
In the folder your terminal is currently in, unless you give a full path such as C:\Videos\meeting.mp4 or ~/Movies/meeting.mp4 as the output name.
Why is my trimmed clip a few seconds longer than I asked?
With -c copy, ffmpeg can only start a clip on a keyframe, a full picture that the frames after it depend on. It starts at the keyframe before your chosen time, so the clip can begin a little early.
Can ffmpeg resume an interrupted download?
No. Run the command again. For a long on-demand video you could save the missing part with -ss, but joining the parts cleanly takes extra steps, so starting over is usually simpler.
Where do I get the right command for my stream?
Paste the link on the home page. m3u8.cloud shows the equivalent ffmpeg command for every stream, ready to copy.
Related
Have the link? Save the stream as one video file.
Paste a link