36

I want to use ffmpeg to copy all meta data not associated with core aspects of a video (frame rate, resolution) from one video to another. Is there a simple way to do this with a single command?

David
  • 535
  • 2
  • 5
  • 6

5 Answers5

37

Use -map_metadata.

In this example the global and stream metadata will be copied from in0.mkv. The video and audio streams will be stream copied from in1.mkv:

ffmpeg -i in0.mkv -i in1.mkv -map 1 -c copy \
# copies all global metadata from in0.mkv to out.mkv  
-map_metadata 0 \
# copies video stream metadata from in0.mkv to out.mkv
-map_metadata:s:v 0:s:v \
# copies audio stream metadata from in0.mkv to out.mkv
-map_metadata:s:a 0:s:a \
out.mkv

This will result in something like:

Output #0, matroska, to 'out.mkv':
  Metadata:
    title           : Global Title
    AUTHOR          : Global Author
    Stream #0:0: Video: h264
    Metadata:
      title           : Stream 0 Title
    Stream #0:1: Audio: vorbis
    Metadata:
      title           : Stream 1 Title

By default global metadata is copied from the first input file, so -map_metadata 0 could probably be omitted.

llogan
  • 57,139
  • 15
  • 118
  • 145
  • 4
    If your source video is a .mov file you need to also add the flag `-movflags use_metadata_tags` or the output video won't contain the metadata. – Mastergalen Aug 28 '19 at 20:55
  • It overwrites the output file.. Output file contains video from the input file now.. I would expect to leave the contents of the output file, just copy the metadata of the input file to output file. – hipokito Feb 11 '21 at 21:17
  • @hipokito `ffmpeg` can't perform in-place editing. A new file must be created, and it will ask you if you want to overwrite any existing files. – llogan Feb 12 '21 at 00:09
  • Is `map_metadata` still required with ffmpeg 4.4 when there is only 1 input file? (For example if you want to optimise mp4 files with `movflag +faststart` while ensuring all metadata is also copied over tot he new file)? – sfxedit Oct 20 '21 at 03:12
  • @sfxedit No, you don't need `-map_metadata` for a single file. ffmepg will attempt to copy all metadata. I should update this old answer to make it more clear. – llogan Oct 20 '21 at 05:53
  • Note that the above only works for outputs with at most 1 video and 1 audio stream. Outputs with multiple streams (including multiple video, audio, subtitle, or data streams) require 1 `-map_metadata:s` per stream to copy all per-stream metadata. For example, for 3 subtitle streams: `-map_metadata:s:s:0 0:s:s:0 -map_metadata:s:s:1 0:s:s:1 -map_metadata:s:s:2 0:s:s:2`. – Arnon Weinberg Feb 18 '22 at 03:52
  • I had to remove the inline comments for it to work correctly. – Gringo Suave Jun 21 '22 at 18:06
1

From the comment in the answer How to make Handbrake preserve capture time / creation time?

A full command line adding the option to copy special tags will be:

ffmpeg -i in.mp4 -i out.mp4 -map 1 -map_metadata 0 -c copy -movflags use_metadata_tags fixed.mp4

0

For decoding Flac to AIFF whilst preserving metadata, this answer worked better for me: Covert FLAC to AIFF while saving tags/metadata

Ben XO
  • 314
  • 2
  • 8
0

When using ffmpeg at a file which has metadata (like a title) in its streams, you need to use -map_metadata again, as it will not hold those metadata in the new output

lhksoft
  • 1
  • 1
-3

If all you need is the basics (creation date, etc), then touch -r FILE1 FILE2 Will work as a charm coping metadata from FILE1 to FILE2