VideoHelp Forum
+ Reply to Thread
Results 1 to 3 of 3
Thread
  1. 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

    Code:
    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)
    numbers appear unordered

    1
    2
    3
    1
    2
    3


    Note, I know a program "Subtitle Edit" Specialized in merging a group of Subtitles. I want a script to add to My project I'm working on. I hope for help, thank you
    Quote Quote  
  2. try to rename those numbers at the same time as making copy
    Code:
    filenames = ['sub1.srt', 'sub2.srt']
    numbers = 0
    with open('output_file.srt', 'w') as outfile:
        for fname in filenames:
            with open(fname) as infile:
                for line in infile:
                    try:
                        if 1 <len(line)<6 and isinstance(int(line),int):
                             numbers += 1
                             line = f'{numbers}\n'
                    except ValueError:
                        pass
                    finally:
                        outfile.write(line)
                outfile.write('\n\n')
    this can still fail if there is a lone number less than 5 digits in a title, you might check for correct position , above time,etc., but it seams there must be easier way to do it
    Last edited by _Al_; 7th Nov 2021 at 01:12.
    Quote Quote  
  3. Thanks it works
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!