ScottBot10
ScottBot10

Reputation: 153

Add multiple subtitles to video with ffmpeg-python

I am trying to add multiple subtitles (not burned) to a video file with ffmpeg-python. I have this ffmpeg command:

ffmpeg -i input.mp4 -f srt \
-i "subs/en/en_srt_sub.srt" \
-i "subs/fr/fr_srt_sub" \
-map 0:0 -map 0:1 -map 1:0 -map 2:0 \
-c:v copy -c:a copy \
-c:s srt -c:s srt \
-metadata:s:s:0 language=en -metadata:s:s:0 title=English \
-metadata:s:s:1 language=fr -metadata:s:s:1 title=French \
output.mkv

Is there a way to do this with the ffmpeg-python module or do I just have to use string formatting and subprocess.

Upvotes: 1

Views: 3533

Answers (1)

Timmy
Timmy

Reputation: 745

The same command can be translated to something like this:

#Import library
import ffmpeg

#Define vars
video="input.mp4"
sub1="subs/en/en_srt_sub.srt"
l1="en"
t1="English"
sub2="subs/fr/fr_srt_sub.srt"
l2="fr"
t2="French"
output_file="output.mkv"

#Define input values
input_ffmpeg = ffmpeg.input(video)
input_ffmpeg_sub1 = ffmpeg.input(sub1)
input_ffmpeg_sub2 = ffmpeg.input(sub2)

#Define output file
input_video = input_ffmpeg['v']
input_audio = input_ffmpeg['a']
input_subtitles1 = input_ffmpeg_sub1['s']
input_subtitles2 = input_ffmpeg_sub['s']
output_ffmpeg = ffmpeg.output(
    input_video, input_audio, input_subtitles1, input_subtitles2, output_file,
    vcodec='copy', acodec='copy', 
    **{'metadata:s:s:0': "language="+l1, 'metadata:s:s:0': "title="+t1, 'metadata:s:s:1': "language="+l2, 'metadata:s:s:1': "title="+t2 }
)

# If the destination file already exists, overwrite it.
output_ffmpeg = ffmpeg.overwrite_output(output_ffmpeg)

# Print the equivalent ffmpeg command we could run to perform the same action as above.
print(ffmpeg.compile(output_ffmpeg))

# Do it! transcode!
ffmpeg.run(output_ffmpeg)

In order to add other stream metadata you can check Custom Filter in the official documentation.

Upvotes: 5

Related Questions