Convert HLS m3u8 to MP4 with FFmpeg

Run ffmpeg -i in.m3u8 -c copy out.mp4. FFmpeg reads the playlist, fetches every segment in order and writes their streams into one MP4 without re-encoding. On FFmpeg 8.0 you no longer need -bsf:a aac_adtstoasc: the output is the same file, byte for byte, with or without it.

Copy Stream copy

The playlist lists the segments directly. Every segment is copied, nothing is re-encoded.

ffmpeg -i in.m3u8 -c copy out.mp4

All 6 segments in one MP4: 864 video frames, 36.0 s. 3.3% smaller than the 10.7 MB of .ts files, which carry MPEG-TS packet overhead.

Frames
864 of 864
Segments joined
6
Run time
98 ms
Output
10.3 MB
One rendition Stream copy

The playlist is a master with several qualities and you want a specific one.

ffmpeg -i master.m3u8 -map 0:p:1 -c copy out.mp4

The 640x272 rendition with its own audio, 864 frames. 0:p:1 is the second program, the second variant in the master.

Picked
640x272, variant 2 of 2
Default pick
1280x546, the highest
Run time
91 ms
Output
3.5 MB

Proof

What the MP4 holds

The first frame of each of the six segments, read from the joined MP4, and the same moment from the two renditions of the master playlist.
-c copy, frames 0, 144, 288, 432, 576, 720 Every segment, in order
Six frames from the joined MP4, one at the start of each 6 second segment, from Sintel's rooftop and dragon scenes
6 of 6 One frame every 6 s, the segment length. The MP4 has 864 frames, 36 s at 24 fps, so no segment was skipped.
master.m3u8, default vs -map 0:p:1 Same moment, different quality
The same crop at 0:20 from the 1280x546 rendition on the left and the 640x272 rendition scaled up on the right
10.3 MB vs 3.5 MB Left: the default pick, 1280x546. Right: the 640x272 variant, scaled up to the same size for comparison.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Cut to 36 s and packaged as HLS by us.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.m3u8": "https://cdn.rendobar.com/examples/hls-to-mp4/in.m3u8",
"in0.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in0.ts",
"in1.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in1.ts",
"in2.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in2.ts",
"in3.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in3.ts",
"in4.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in4.ts",
"in5.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in5.ts",
},
params: {
command: "ffmpeg -i in.m3u8 -c copy out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.m3u8": "https://cdn.rendobar.com/examples/hls-to-mp4/in.m3u8",
"in0.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in0.ts",
"in1.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in1.ts",
"in2.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in2.ts",
"in3.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in3.ts",
"in4.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in4.ts",
"in5.ts": "https://cdn.rendobar.com/examples/hls-to-mp4/in5.ts"
},
"params": {
"command": "ffmpeg -i in.m3u8 -c copy out.mp4"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Make the MP4 start playing before it fully downloads

    Stream copy
    ffmpeg -i in.m3u8 -c copy -movflags +faststart out.mp4

    Same size, same streams. The index (moov) moves from byte 10,294,728 to byte 40, ahead of the media data.

    90 ms run 10.3 MB 36.03 s long

  2. Convert a master playlist with the default pick

    Stream copy
    ffmpeg -i master.m3u8 -c copy out.mp4

    The 1280x546 variant. The file is byte for byte the one in.m3u8 gave, because the master points at it.

    90 ms run 10.3 MB 36.03 s long

  3. Convert HLS with fMP4 (.m4s) segments

    Stream copy
    ffmpeg -i fmp4.m3u8 -c copy out.mp4

    Same command, same 864 frames. FFmpeg reads init.mp4 first, then the six .m4s segments.

    85 ms run 10.3 MB 36.00 s long

  4. Convert an AES-128 encrypted HLS playlist

    Stream copy
    ffmpeg -allowed_extensions m3u8,ts,key -i enc.m3u8 -c copy out.mp4

    Decrypted on the fly. The MP4 has the same MD5 as the one from the unencrypted playlist.

    89 ms run 10.3 MB 36.03 s long

  5. Convert with the aac_adtstoasc filter

    Stream copy
    ffmpeg -i in.m3u8 -c copy -bsf:a aac_adtstoasc out.mp4

    Same MD5 as the command without it. FFmpeg 8.0 applies the filter by itself when it writes AAC from TS into MP4.

    86 ms run 10.3 MB 36.03 s long

  6. Re-encode while converting

    Re-encode
    ffmpeg -i in.m3u8 -c:v libx264 -crf 23 -c:a aac out.mp4

    8.6 MB of fresh H.264 and AAC, 864 frames. Only worth it when you need a different codec, size or bitrate.

    3.1 s run 8.6 MB 36.04 s long

Gotchas

Where it goes wrong

Do I still need -bsf:a aac_adtstoasc?

Not on current FFmpeg. AAC in MPEG-TS segments carries ADTS headers, and MP4 wants them gone. Older answers carry the flag because older builds stopped with "Malformed AAC bitstream detected" and asked for it. FFmpeg 8.0 adds it by itself: its verbose log says "Automatically inserted bitstream filter 'aac_adtstoasc'".

Run with and without it on the same playlist and the two MP4s have the same MD5, 475d5967.... Adding it does no harm, so scripts that carry it keep working.

Why does an encrypted playlist fail with "Error when loading first segment"?

The HLS reader in FFmpeg 8.0 only opens files whose extension is on an allow list, and .key is not on it. The log says why one line higher:

ffmpeg -i enc.m3u8 -c copy out.mp4

"Filename extension of 'enc.key' is not a common multimedia extension, blocked for security reasons." No MP4 is written.

Name the extensions the playlist needs with -allowed_extensions, before -i. With m3u8,ts,key FFmpeg reads the key, decrypts each segment and writes the same MP4 the unencrypted playlist gives.

Protocol 'http' not on whitelist 'file,crypto,data'

A playlist read from disk may only open other local files. When that local m3u8 lists http:// or https:// segment URLs, FFmpeg refuses:

ffmpeg -i local.m3u8 -c copy out.mp4

"Protocol 'http' not on whitelist 'file,crypto,data'!" then "Error when loading first segment".

Allow the network protocols for that input with -protocol_whitelist file,http,https,tcp,tls,crypto before -i. With the flag, FFmpeg passed the check and went on to connect to the segment's server.

Which quality does FFmpeg pick from a master playlist?

The video with the highest resolution, wherever it sits in the list. We swapped the order of the two variants and FFmpeg still took the 1280x546 video. The audio is picked separately, so with variants that have different audio you can get the video of one and the sound of another.

-map 0:p:N takes video and audio from variant N together, counting from 0 in the order the master lists them. Run ffprobe master.m3u8 to see the programs and their sizes first.

Related: Convert to MP4, Web-playable MP4, Trim a video, Merge videos, Convert MP4 to HLS.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.