wotanii
wotanii

Reputation: 2826

replace a word in subtitle with ffmpeg

I want to use ffmpeg to replace all occurrences of a word in all subtitles of a video file with ffmpeg. All non-subtitles channels should be copied (not reencoded) and all formatting from the original subtitles should stay if possible.

example:

ffmpeg -i input.mkv -SUBTITLEFILER='old_word/new_word' output.mkv

I am using ubuntu 19.04 and bash (in case additional steps or dependencies would be required for this)

Upvotes: 1

Views: 744

Answers (1)

llogan
llogan

Reputation: 133743

ffmpeg has no find/replace functionality for subtitles, but you can do this losslessly in 3 commands:

  1. Extract the subtitles:

    ffmpeg -i input.mkv -map 0:s:0 -c copy sub0.ass -map 0:s:1 -c copy sub1.ass -map 0:s:2 -c copy sub2.ass
    

    I'm assuming your subtitles are SubStation Alpha (ASS/SSA) subtitles. Use the appropriate output name if they differ: such as .srt for SubRip (refer to ffmpeg -muxers).

  2. Replace with sed:

    sed -i 's/cat/dog/g' *.ass
    
  3. Remux:

    ffmpeg -i input.mkv -i sub0.ass -i sub1.ass -i sub2.ass -map 0 -map -0:s -map 1 -map 2 -map 3 -c copy -metadata:s:s:0 language=fas -metadata:s:s:1 language=eng -metadata:s:s:2 language=fin output.mkv
    

If you want to make a certain subtitle the default see the -disposition option as shown in ffmpeg set subtitles track as default.

Upvotes: 2

Related Questions