FFmpeg通过文件传递元数据的-map_metadata参数用法问询
-map_metadata and File-Based Metadata in FFmpeg Great question—using files to handle metadata is a smart way to keep your FFmpeg commands clean, especially when dealing with lots of parameters. Let’s break down both how to use -map_metadata properly and an alternative method for custom metadata files.
Understanding -map_metadata Syntax
The official syntax outfile[,metadata]:infile[,metadata] looks a bit intimidating, but it’s simpler when you break it down:
infileis the index of your input file (starting at 0 for the first input)outfileis the index of your output file (also starting at 0, useful if you’re exporting multiple outputs)- The optional
[,metadata]part lets you target specific streams (video, audio, subtitles) instead of the entire file.
Common -map_metadata Examples
Let’s start with the most useful cases:
Copy all metadata from the first input to the output
This is the simplest use case—pulls every metadata field (title, artist, etc.) from your input media to the output:ffmpeg -i input.mp4 -map_metadata 0 -c copy output.mp4The
0here refers to the first input file;-c copycopies streams without re-encoding (faster).Pull metadata from a separate "metadata source" file
If you have a dedicated media file (like a dummy audio/video file with all your desired metadata) you want to reuse, point-map_metadatato that input index:ffmpeg -i raw_video.mp4 -i metadata_template.mp4 -map_metadata 1 -c copy final_video.mp4Here,
1targets the second input (your metadata template), so the output gets its metadata from that file while using the video from the first input.Target specific stream metadata
If you only want to copy metadata from, say, the audio stream of your input, use stream specifiers (:s:afor audio streams,:s:vfor video):ffmpeg -i input.mp4 -map_metadata:s:a 0:s:a -c copy output.mp4This copies only the audio stream’s metadata (like artist or track number) to the output’s audio stream.
Alternative: Load Custom Metadata from a Text File
If you’re creating entirely custom metadata (not copying from another media file), the @ flag lets you load a text file full of -metadata parameters. This is perfect for long lists of metadata fields.
- Create a
metadata.txtfile with your parameters:-metadata title="My Travel Vlog 2024" -metadata artist="Jane Smith" -metadata year="2024" -metadata description="A week exploring the Italian countryside" -metadata genre="Travel" -metadata copyright="© 2024 Jane Smith" - Reference this file in your FFmpeg command:
FFmpeg will read all theffmpeg -i input.mp4 @metadata.txt -c copy output.mp4-metadatalines from the file just like you typed them directly in the CLI.
Key Notes
- When using
-map_metadata, if metadata fields overlap between inputs, the later input (or the one specified in-map_metadata) will overwrite earlier ones. - The
@filemethod works for any FFmpeg parameters, not just metadata—you can put your entire command (except theffmpegstart) in a text file if you want!
内容的提问来源于stack exchange,提问作者Alejandro Galera

