1

I have two srt files, cd1.srt and cd2.srt. Unfortunately, srtool is no longer available anywhere which was the answer to the same question when it was asked a few years ago.

How do I merge two *.srt files

but came up short. I tried https://tracker.debian.org/pkg/pysrt, it does everything except join srt files :(

I even looked up github but found entries only such as these -

https://github.com/malfroid/merge-srt-subtitles

If anybody knows a better way please share. I also looked at a few online solutions as well but sadly all of them did not work out :(

shirish
  • 12,356

1 Answers1

0

This is how I did it:

I first checked the end timestamp of the first srt file. In my case, the first timestamp of the second one started immediately after, so the shifting timespan was easy to deduce.

Having this, and as mentioned by @aviro in the comments of your answer, I used pysrt with the timespan I deduced above.

pip install pysrt
srt -i shift 1h2m58s cd2.srt

Then before merging the two srt files, we need to offset the sub counter at cd2.srt for them to work properly.

For this I used this python script to generated the fully offsetted cd2.srt: (replace $numX with (sum of subtitles in cd1.srt)+1)

# Open the input file for reading
with open('cd2.srt', 'r') as input_file:
  # Open the output file for writing
  with open('cd2.output.srt', 'w') as output_file:
    # Iterate over each line in the input file
    for line in input_file:
      # Check if the line contains a single integer
      if line.strip().isdigit():
        # Convert the line to an integer
        number = int(line)
        # Add $numX to the number
        number += $numX
        # Write the resulting number to the output file
        output_file.write(str(number) + '\n')
      else:
        # Write the original line to the output file
        output_file.write(line)

then perform the final merge via: cat cd1.srt cd2.output.srt > final.srt

Now the subtitles should work fine alongside your newly merged video file!

dimisjim
  • 131