How to change metadata with ffmpeg/avconv without creating a new file?

I asked on the mailing list of avconv and got the following answer:

„No, it's not possible [to change the metadata without creating a new file], neither libavformat API nor avconv design allows for in-place editing of files.“


You can do this with FFmpeg like so:

ffmpeg -i input.avi -metadata key=value -codec copy output.avi

Example:

$ du -h test.mov 
 27M    test.mov
$ ffprobe -loglevel quiet -show_format out.mov | grep title    # nothing found
$ ffmpeg -loglevel quiet -i test.mov -codec copy -metadata title="My title" out.mov
$ du -h out.mov
 27M    out.mov
$ ffprobe -loglevel quiet -show_format out.mov | grep title
TAG:title=My title

See the documentation for -metadata and on stream copying for more information.

Note also that not all formats allow setting arbitrary metadata, for, e.g., Quicktime doing -metadata title="my title" does what you'd expect, but -metadata foo=bux does nothing.


Since changing the metadata will change the length of the file, and I expect the metadata is near the beginning of the file, the audio and video would start at a different offset from the beginning of the file. So you cannot alter the metadata without first creating a temporary file, then renaming files after.

If the new metadata were exactly the same size, and you know where the metadata was located in the container (file) you might be able to use a hex editor of some kind to simply replace characters. Good luck with that.

Also. you might be able to put shorter data in place directly if you paded with nulls, but this might be problematic for some players.


You can also make something like this that deletes the file on success and rename the output

ffmpeg -i default.mp4 -metadata title="my title" -codec copy output.mp4 && mv output.mp4 default.mp4