[Python] how to merge multiple subtitle files in python?

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have a batch of files "srt" I want to merge

    sub1.srt

    1
    00:00:21,601 --> 00:00:24,130
    - What happened? - It's a mess, I heard.

    2
    00:00:24,131 --> 00:00:25,900
    - What's that? - Dead bodies?

    3
    00:00:25,901 --> 00:00:28,839
    - What's going on? - I wish I knew.


    sub2.srt

    1
    00:00:28,840 --> 00:00:31,310
    No one knows. They won't say.

    2
    00:00:31,311 --> 00:00:35,276
    - My gosh. - How can so many die?

    3
    00:00:45,191 --> 00:00:46,556
    When you starve,


    after merge

    1
    00:00:21,601 --> 00:00:24,130
    - What happened? - It's a mess, I heard.

    2
    00:00:24,131 --> 00:00:25,900
    - What's that? - Dead bodies?

    3
    00:00:25,901 --> 00:00:28,839
    - What's going on? - I wish I knew.

    4
    00:00:28,840 --> 00:00:31,310
    No one knows. They won't say.

    5
    00:00:31,311 --> 00:00:35,276
    - My gosh. - How can so many die?

    6
    00:00:45,191 --> 00:00:46,556
    When you starve,


    I found this script that it works The problem is in the numbers The subtitle are not in order

    filenames = ['sub1.srt', 'sub2.srt']
    with open('output_file.srt', 'w') as outfile:
    for fname in filenames:
    with open(fname) as infile:
    for line in infile:
    outfile.write(line)


    appear unordered

    1
    2
    3
    1
    2
    3


    how to fix?

    Continue reading...

Compartilhe esta Página