-map_metadata flag combined with -c copy for lossless remuxing or specific codec settings for re-encoding. This ensures that titles, authors, creation dates, and technical headers remain intact throughout your processing workflow without data loss or corruption.
Understanding Default Metadata Behavior
When you run a standard FFmpeg command to convert or edit a video file, the software does not automatically carry over all metadata from the source file to the output. By default, FFmpeg may only copy a subset of metadata fields, such as duration or basic stream information, while discarding descriptive tags like title, artist, comment, or custom user-defined keys. This behavior can be problematic for media libraries, content management systems, or professional workflows where accurate attribution and categorization are critical. For example, if you simply run a conversion command likeffmpeg -i input.mp4 output.mp4, you might notice that the resulting file lacks the original creator's name or the date it was recorded. This is because FFmpeg treats metadata as optional unless explicitly told otherwise. Understanding this default limitation is the first step toward mastering metadata retention in your video processing pipeline.
The -map_metadata Flag Explained
The primary tool for controlling metadata flow in FFmpeg is the-map_metadata option. This flag allows you to specify which metadata from the input files should be written to the output file. It uses an index number to refer to the input file. If you have multiple inputs, you can choose exactly which one’s metadata to keep.
To copy all metadata from the first input file to the output, use:
ffmpeg -i input.mp4 -map_metadata 0 output.mp4
Here, 0 refers to the first input file (input.mp4). If you have multiple inputs and want to merge their metadata or select a specific one, you adjust this number accordingly. For instance, -map_metadata 1 would take metadata from the second input file. This level of control prevents accidental overwriting or loss of important tags during complex multi-source operations.
Using -c Copy for Lossless Retention
While-map_metadata handles the tags, the encoding process itself can sometimes interfere with container-level metadata. To ensure maximum compatibility and minimal risk of metadata corruption, it is often best to use stream copying (-c copy) whenever possible. This technique avoids re-encoding the video and audio streams entirely, merely repackaging them into a new container format.
When you use -c copy, FFmpeg preserves the exact bitstream, including all embedded metadata, chapters, and subtitles, provided the target container supports these features. A typical command looks like this:
ffmpeg -i input.mov -c copy -map_metadata 0 output.mp4
This approach is significantly faster than re-encoding and guarantees that no generational loss occurs. It is ideal for tasks like format conversion (e.g., MOV to MP4) where the visual and audio quality must remain identical to the source. However, note that some metadata types are container-specific and may not transfer perfectly between different formats even with -c copy.
Handling Global vs. Stream Metadata
Video files contain two main types of metadata: global metadata (applying to the whole file, like title or artist) and stream metadata (applying to specific tracks, like language or codec details). Sometimes, you may need to handle these separately. FFmpeg allows you to map global metadata specifically using-map_metadata 0:g for global and -map_metadata 0:s for stream-specific data.
In many cases, using -map_metadata 0 without suffixes copies both global and stream metadata, which is usually what users want. However, if you encounter issues where certain tags are missing, check whether they were stored globally or within a specific stream. You can inspect the metadata of your input file using:
ffprobe -show_streams -show_format input.mp4
This diagnostic step reveals where each piece of information is located, helping you fine-tune your mapping strategy. For most general purposes, sticking to -map_metadata 0 alongside -c copy provides a robust solution for retaining all relevant information.
Related Resources
If you are dealing with advanced bitstream filtering alongside metadata preservation, consider reviewing How to use ffplay with -bsf? for insights into stream manipulation.
For issues related to timestamp synchronization that might affect metadata interpretation, see Fixing the 'ffmpeg -itsoffset is not working' Issue.
Frequently Asked Questions
Does -c copy guarantee all metadata is preserved?
Yes, -c copy preserves the raw bitstream, which includes all container-level metadata. However, if the output format does not support a specific metadata type found in the input, that particular tag will be dropped regardless of the flags used.
How do I remove specific metadata tags instead of keeping them?
You can use the -metadata flag followed by the key-value pair to overwrite or clear specific tags. For example, -metadata title="" will remove the title tag from the output file, overriding any inherited metadata.
Why are my chapter markers missing after conversion?
Chapter markers are often stored as stream metadata or in the container header. Ensure you are using -map_metadata 0 and that the output container format (like MP4 or MKV) supports chapters. Some containers require explicit muxing options to retain chapter data.
Can I add new metadata while keeping existing ones?
FFmpeg does not natively merge metadata sets in a single command easily. You typically need to copy existing metadata first using -map_metadata and then append new tags using additional -metadata flags, though this may override conflicting keys.
What is the difference between -map_metadata 0 and -map_metadata 0:g?
-map_metadata 0 copies both global and stream metadata from the first input. -map_metadata 0:g restricts the operation to only global metadata, ignoring any tags attached to individual video or audio streams.