Copy Metadata From Input to Output in FFmpeg
June 24, 2018
Posted by on
The documentation for the -map_metadata option for FFmpeg reads:
-map_metadata[:metadata_spec_out] infile[:metadata_spec_in] (output,per-metadata)
Set metadata information of the next output file from infile. Note that those are file indices (zero-based), not filenames. Optional metadata_spec_in/out parameters specify, which metadata to copy. A metadata specifier can have the following forms:
- g – global metadata, i.e. metadata that applies to the whole file
- s[:stream_spec] – per-stream metadata. stream_spec is a stream specifier as described in the Stream specifiers chapter. In an input metadata specifier, the first matching stream is copied from. In an output metadata specifier, all matching streams are copied to.
- c:chapter_index – per-chapter metadata. chapter_index is the zero-based chapter index.
- p:program_index – per-program metadata. program_index is the zero-based program index.
If metadata specifier is omitted, it defaults to global.
By default, global metadata is copied from the first input file, per-stream and per-chapter metadata is copied along with streams/chapters. These default mappings are disabled by creating any mapping of the relevant type. A negative file index can be used to create a dummy mapping that just disables automatic copying.
For example to copy metadata from the first stream of the input file to global metadata of the output file:
ffmpeg -i in.ogg -map_metadata 0:s:0 out.mp3
To do the reverse, i.e. copy global metadata to all audio streams:
ffmpeg -i in.mkv -map_metadata:s:a 0:g out.mkv
Note that simple 0 would work as well in this example, since global metadata is assumed by default.
So let’s say we had three input streams, 0:0 (video), 0:1 (audio), and 0:2 (subtitle) and we wanted to copy the metadata for all three streams and the global metadata. We could use:
ffmpeg -i input.mov -map 0:0 -map 0:1 -map 0:2
-map_metadata:g 0:g # take file 0's global metadata and copy to this' global
-map_metadata:s:0 0:s:0 # take file 0's stream 0 metadata and copy to this' stream 0
-map_metadata:s:1 0:s:1 # take file 0's stream 1 metadata and copy to this' stream 1
-map_metadata:s:2 0:s:2 # take file 0's stream 2 metadata and copy to this' stream 2