Add a stream using ffmpeg without creating a new file
You cannot edit a file in-place with ffmpeg
– it always needs to create a new output file. I think that's the default for almost any program, and in many cases the output file needs to be seekable and editable by the program during writing.*
If the real issue is that you lose metadata, try:
ffmpeg -i input.mp4 -i subtitles.srt \
-c copy -c:s mov_text \
-map 0 -map 1 -map_metadata 0 output.mp4
You need to have ffmpeg
convert SRT (SubRip) to MP4-compliant subtitles with -c:s mov_text
, otherwise ffmpeg
refuses to copy the stream over.**
The -map_metadata
option should copy over all global metadata from the input MP4 file to the output, i.e. anything you set at the container level. The bitstream-level metadata should be copied automatically without any further options.
* Of course, programs like sponge
from moreutils can "soak" up input and overwrite a file in-place, but this only works with muxers that support non-seekable output, and therefore not with MP4.
** There are problems with mov_text
encoded subtitles and the QuickTime player, see FFmpeg ticket #1845 and also #2488.
Related videos on Youtube
Sam
Updated on September 18, 2022Comments
-
Sam almost 2 years
I have some videos (.mp4) that have been tagged with metadata (descriptions, artwork, actors etc) and I now have subtitle files for them that I want to add as a stream.
I can use
ffmpeg
and set both the video and the.srt
file as inputs and get a new output file but the output always seem to lose all of the container level metadata and it sometimes gets annoying always going back and deleting the original files.Is there a way that I can add the subtitle file as a new stream to the video file without creating a new file? Just by modifying the original video file?
Programs like Subler seem to do this but, out of interest, can it be done from the command line?
-
Sam almost 11 yearsAh good. I didn't know you could map metadata like that. That will be very handy, although it is annoying that you can't just 'inject' a stream. You say that
only works with muxers that support non-seekable output, and therefore not with MP4
so how can Subler do this with mp4s? When I add a subtitle stream to a mp4 using that it takes less than two seconds, so it cant be creating a whole new file and deleting the old...? -
slhck almost 11 yearsI think this is a design choice, and
ffmpeg
developers never intended to do that. Of course, I don't know what subler does – I haven't looked at its source. It could also be using temporary files, deleting the original, and replacing it with the new one. Since bitstream copies are very fast you wouldn't see the difference. I think MP4Box can also do in-place edits, but I always forget its option syntax… haven't really used it a lot. -
rien333 over 5 yearsUsing process substitution and a simple
cat
, I was able to modify a file in place. Seems fast and to work great. Is this problematic in anyway? -
slhck over 5 years@rien333 Depends on when the output file is being written in relation to the input being read. I can imagine that this depends on the output container as well (for some formats it must be seekable). I would not generally advise to do so, but instead use a temporary file. See also my first footnote in the post above.