I'm using ffmpeg to splt and merge streams. I create videos with multiple audio streams…
- User
I'm using ffmpeg to splt and merge streams. I create videos with multiple audio streams and no player allows me to mix those streams in the output, so I have to extract all but one, to use it in my web-based mixer, since JS currently cannot handle multiple audio tracks in a video file. The basic command is ffmpeg -i something.mkv -map 0:2 -c copy something_2.aac -map 0:3 -c copy something_3.aac to extract audio #2 and #3 into separate files On the other hand I sometimes assemble a single media file from multiple single stream files where I just copy together the streams. This command I don't quite remember but does not contain -map, but does contain -c:a Both commands were tried and worked. Since I'm using them quite often it is time for me to understand these command line options and what can one do with them, what they mean. Your task is to explain . Do not extend the task I'm just interested in no-reencoding disassemlbe and assemle operations.
- Assistant
Here’s a concise breakdown of the two “no‐reencode” workflows you described—“disassemble” (extract streams) and “assemble” (mux streams)—and what the key ffmpeg options actually do.
1. Disassemble (extract individual streams)
You already use:
ffmpeg -i something.mkv \ -map 0:2 -c copy something_2.aac \ -map 0:3 -c copy something_3.aacWhat’s going on here?
•
-i something.mkv
your input file (index 0).•
-map 0:2
“take stream 2 from input 0.”
Streams are zero-based; you can also list them viaffprobe something.mkv.•
-c copy
“codec = copy” — no re-encode, just dump the stream as‐is.
(Equivalent to-codec copyor more specifically for audio:-c:a copy.)•
something_2.aac
the output filename/format. ffmpeg infers container/codec from the extension.Repeat for each stream you want to pull out.
2. Assemble (mux multiple single-stream files into one container)
Assume you have:
•
video.h264
•audio_eng.aac
•audio_jpn.aacYou can merge them like this:
ffmpeg \ -i video.h264 \ -i audio_eng.aac \ -i audio_jpn.aac \ -map 0:0 \ # video from input 0 -map 1:0 \ # first audio from input 1 -map 2:0 \ # second audio from input 2 -c copy merged.mkvWhat’s new here?
• Multiple
-i: you list each source file (each becomes input 0, 1, 2 …).•
-map X:0:
“take the first (0-th) stream from input X.”
(If your files contain only one stream each, that’s always stream 0.)• A single
-c copyafter all your-maplines tells ffmpeg to copy every mapped stream (video and audio) without re-encoding.
A few extra tips
- “Copy all streams from input N”
You can shortcut-map N:0 -map N:1 …by doing-map N # means “map every stream from input N” - Selective codecs
If you ever want to mix copying and re-encoding (not in your current use case), you can do:-c:v copy # copy video -c:a aac # re-encode audio to AAC - Default mapping
If you omit all-mapoptions, ffmpeg grabs “one of each type” from the first input—usually not what you want for precise stream control.
Quick‐reference summary
Option Meaning -i fileinput file (assigned an input-index: 0,1,2…) -map i:jpick stream #j from input #i -map ipick all streams from input #i -c copycopy every mapped stream (no re-encode) -c:v copycopy only video streams -c:a copycopy only audio streams With just these few flags you can disassemble any container into its raw streams and re-assemble them into a new container, all without touching the bit-streams themselves. 🎬🎧
Let me know if you’ve got follow-up questions!
- “Copy all streams from input N”
- User
Now I try to remember the assembly command line ffmpeg -i just_video.mp4 -i just_audio.aac -c:a copy -c:v copy assembled.mkv Can i be correct? Also there was some deliberation of which container format to use for the output if I insist on copying without reencoding. Expand on that.
- Assistant
Your recall is basically spot-on. If you have a video-only MP4 and a raw AAC file and just want to “glue” them together without re-encoding, something like this will work:
ffmpeg \ -i just_video.mp4 \ -i just_audio.aac \ -map 0:v \ # take the video stream from input #0 -map 1:a \ # take the audio stream from input #1 -c:v copy \ # copy video as-is -c:a copy \ # copy audio as-is assembled.mkvNotes:
- Explicit
-mapis good practice—even if there’s only one stream in each file—so you know exactly what’s going into your output. - We chose
.mkvhere because it’s the most forgiving container for “raw” streams.
Container-vs-codec compatibility
Not every container will accept every codec in copy-mode. Below is a quick compatibility guide:
Container Video Codecs Audio Codecs Remarks MKV H.264/AVC, HEVC/H.265, VP9, MPEG-2… AAC, MP3, AC-3, DTS, Opus… Very flexible MP4/MOV H.264/AVC, HEVC/H.265, MPEG-4 Part 2… AAC, MP3, AC-3 (limited)… Ubiquitous for web & devices WebM VP8, VP9, AV1 Opus, Vorbis Google-centric, no AAC/AC3 AVI MPEG-4 Part 2, MJPEG… MP3, AC-3 (experimental) Ancient—limited modern codec support MPEG-TS H.264/AVC, HEVC/H.265 AAC, MP3, AC-3, DTS Streaming; subtitle support clunky Why MKV is the safest “copy” container
- It’ll happily mux raw H.264, raw AAC (.aac in ADTS), AC-3, DTS, Opus, you name it.
- No need for extra bitstream filters.
When using MP4/MOV
-
If your
.aacis in ADTS format (most raw .aac files are), MP4 wants “MP4-style” AAC frames. -
You must run the AAC ADTS→MP4 bitstream filter:
ffmpeg -i just_video.mp4 -i just_audio.aac \ -map 0:v -map 1:a \ -c:v copy -c:a copy \ -bsf:a aac_adtstoasc \ assembled.mp4 -
Without
-bsf:a aac_adtstoasc, you’ll get errors or a silent track.
Quick take-aways
• Your “copy” command is correct—just pick a container that supports both your streams.
• Use MKV if you don’t want to worry about bitstream filters.
• If you do insist on MP4 and your audio is raw ADTS AAC, add-bsf:a aac_adtstoasc.Let me know if you need more on any of these containers or bitstream filters! 😃
- Explicit
- User
I suppose the container format is then inferred from the extension of the output. Correct? For every actually existing file is there any inference, or I can use confusing extensions without adverse effects?
- Assistant
Yes—by default ffmpeg will pick the “muxer” (container format) based on your output‐filename extension. If it sees “.mkv” it loads matroska, “.mp4”/“.mov” it picks the MOV/MP4 muxer, “.ts” the MPEG-TS muxer, etc.
You can always override that with
-f format_name. Conversely, if you give an extension ffmpeg doesn’t recognize, it will complain:Could not deduce output format from file extension ".xyz"and you’ll need to add something like
-f matroska(or whatever) to force it.—
How ffmpeg chooses (and how to override)
- You name your file:
ffmpeg … assembled.mkv - ffmpeg looks up “.mkv” → muxer “matroska” → writes Matroska streams.
- If the extension is unknown, it errors out:
ffmpeg -i in.mp4 out.weirdext # → “Could not deduce output format from file extension ‘.weirdext’ (use -f?).” - To force a format, add
-fbefore the output name:ffmpeg -i in.mp4 -f matroska out.weirdext # now it will happily write Matroska data into out.weirdext
You can also explicitly choose a different container than the extension implies. For example:
ffmpeg -i just_video.mp4 -i just_audio.aac \ -map 0:v -map 1:a \ -c copy \ -f mp4 assembled.mkvThat will write an MP4‐style file (with
.mkvextension) unless your players get confused.—
Common extension → muxer mapping
Extension(s) Muxer name Typical codecs .mkv matroska H.264, H.265, VP9, AAC, AC-3, DTS, Opus, … .mp4, .mov mov/mp4 H.264, H.265, MPEG-4, AAC, MP3 (limited), … .webm webm VP8/VP9/AV1 + Opus/Vorbis .ts, .m2ts mpegts H.264/H.265 + AAC/MP3/AC-3/DTS .avi avi MPEG-4 Part 2, MJPEG + MP3 (partial) .flv flv FLV1, H.264 + AAC/Vorbis .mpg, .mpeg mpeg MPEG-1/2 video + MP2 audio .ogg, .ogv ogg/ogg Theora/VP8 + Vorbis .wav wav PCM, ADPCM .mp3 mp3 MP3 If you stick to an obvious pairing (e.g.
.mkvwith Matroska codecs,.mp4with H.264 + AAC), ffmpeg “just works.” If you choose a “weird” extension or a mismatch, either:• You’ll get an error (unknown extension) → add
-f.
• Or ffmpeg will happily write the chosen format into a file whose name doesn’t match its contents (which may confuse players or other tools).—
Bottom line:
• Yes, extension → container is inferred automatically.
• You can use any extension, but if ffmpeg doesn’t know it you must supply-f.
• Mismatched extension vs actual format won’t break ffmpeg, but might break other players. - You name your file:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.