Randomly split a single line sentence into multiple lines in a srt file

263 Views Asked by At

I had an srt file, and to be able to translate it using DeepL, I gathered the fragmented sentences in one line. Now I want to do the opposite of this process. In other words, I want to randomly divide the sentence in a single line into multiple lines as I have exemplified below.

This is the file that I have:

1
00:00:00,000 --> 00:00:13,680
Otele canlı yayın koyacağım, böylece mesajlara ve diğer şeylere bakabileceğiz.

2
00:00:13,680 --> 00:00:14,680
Evet.

3
00:00:14,680 --> 00:00:20,080
Romain'in direkt olduğunu bildireceğim.

4
00:00:20,080 --> 00:00:22,720
Devam edin ve canlı yayını açın.

5
00:00:22,720 --> 00:00:25,640
Evet, seni duyabiliyoruz.

6
00:00:25,640 --> 00:00:26,640
Güzel.

7
00:00:26,640 --> 00:00:28,480
Hızlı bir sunum yapacağım, herkesin gelmesini bekleyeceğim.

8
00:00:28,480 --> 00:00:40,640
Merhaba Raphaël, merhaba Ismaël, merhaba Jean-Marc, herkese merhaba, merhaba Clément, merhaba Thierry.

9
00:00:40,640 --> 00:00:41,640

10
00:00:41,640 --> 00:00:42,640
Herkese merhaba.

11
00:00:42,640 --> 00:00:44,640
Normalde bizi duyup duymadıklarını bilmiyorum.

12
00:00:44,640 --> 00:00:45,640
Evet, artık seni duyabiliyorlar.

13
00:00:45,640 --> 00:00:46,640
Bekleyeceğiz.

14
00:00:46,640 --> 00:00:51,480
Hızlı bir şekilde çalışmasını beklerken bizi bile filme alabilirsiniz, sadece bekleyin.

15
00:00:51,480 --> 00:00:53,360
Seni filme alacağım.

16
00:00:53,360 --> 00:00:57,520
Pekala çocuklar, sadece küçük bir açıklama yapmak için bugün iki konuşmacımız olacak, kafamı keseceğim, CP Dropshipping olacak, sanırım onu tanıyorsunuz, bir YouTube kanalı var, Çok güzel bir Balenciaga tişörtü olan bir Facebook hesabı var ve ayrıca Jérémy, yani oldukça ağır olan iki Dropshipper'ımız var ve Jérémy her şeyi doğru yapmaya başlayan, oldukça fazla para kazanmaya başlayan ve sanırım yakında Malta'ya gelip yaşayacak olan bir Discord yöneticisi.

17
00:00:57,520 --> 00:01:02,560

18
00:01:02,560 --> 00:01:06,320

19
00:01:06,320 --> 00:01:11,520

20
00:01:11,520 --> 00:01:16,960

21
00:01:16,960 --> 00:01:21,520

22
00:01:21,520 --> 00:01:22,520

23
00:01:22,520 --> 00:01:25,520
Daha önce orada uçak videolarını izlemiştik.

24
00:01:25,520 --> 00:01:28,520
Bir hafta sonra geri getirmem gerekecek.

25
00:01:28,520 --> 00:01:30,320
Kahretsin, yanılmışım.

And I want this instead:

1
00:00:00,000 --> 00:00:13,680
Otele canlı yayın koyacağım, böylece mesajlara ve diğer şeylere bakabileceğiz.

2
00:00:13,680 --> 00:00:14,680
Evet.

3
00:00:14,680 --> 00:00:20,080
Romain'in direkt olduğunu bildireceğim.

4
00:00:20,080 --> 00:00:22,720
Devam edin ve canlı yayını açın.

5
00:00:22,720 --> 00:00:25,640
Evet, seni duyabiliyoruz.

6
00:00:25,640 --> 00:00:26,640
Güzel.

7
00:00:26,640 --> 00:00:28,480
Hızlı bir sunum yapacağım, herkesin gelmesini bekleyeceğim.

8
00:00:28,480 --> 00:00:40,640
Merhaba Raphaël, merhaba Ismaël, merhaba Jean-Marc, herkese merhaba, merhaba Clément,

9
00:00:40,640 --> 00:00:41,640
merhaba Thierry.

10
00:00:41,640 --> 00:00:42,640
Herkese merhaba.

11
00:00:42,640 --> 00:00:44,640
Normalde bizi duyup duymadıklarını bilmiyorum.

12
00:00:44,640 --> 00:00:45,640
Evet, artık seni duyabiliyorlar.

13
00:00:45,640 --> 00:00:46,640
Bekleyeceğiz.

14
00:00:46,640 --> 00:00:51,480
Hızlı bir şekilde çalışmasını beklerken bizi bile filme alabilirsiniz, sadece bekleyin.

15
00:00:51,480 --> 00:00:53,360
Seni filme alacağım.

16
00:00:53,360 --> 00:00:57,520
Pekala çocuklar, sadece küçük bir açıklama yapmak için bugün iki konuşmacımız olacak, 

17
00:00:57,520 --> 00:01:02,560
kafamı keseceğim, CP Dropshipping olacak, sanırım onu tanıyorsunuz, 

18
00:01:02,560 --> 00:01:06,320
bir YouTube kanalı var, Çok güzel bir Balenciaga tişörtü olan bir Facebook hesabı var 

19
00:01:06,320 --> 00:01:11,520
ve ayrıca Jérémy, yani oldukça ağır olan iki Dropshipper'ımız var ve Jérémy her şeyi doğru yapmaya başlayan, 

20
00:01:11,520 --> 00:01:16,960
oldukça fazla para kazanmaya başlayan 

21
00:01:16,960 --> 00:01:21,520
ve sanırım yakında Malta'ya gelip yaşayacak olan 

22
00:01:21,520 --> 00:01:22,520
bir Discord yöneticisi.

23
00:01:22,520 --> 00:01:25,520
Daha önce orada uçak videolarını izlemiştik.

24
00:01:25,520 --> 00:01:28,520
Bir hafta sonra geri getirmem gerekecek.

25
00:01:28,520 --> 00:01:30,320
Kahretsin, yanılmışım.
1

There are 1 best solutions below

1
On

I think you can do this by the following steps:

Step 1: split the text into (index,time_span,text) or ( (index,time_span) by '\n\n':

with open('original.srt') as org_file:
    text = org_file.read()
segment_list = text.split('\n\n')

Step 2: find consecutive non-text segments, and split the previous text proportionally to these non-text segments:

processed_segment_list = []
tmp_segment_list = []
dirty = False
for segment in segment_list:
    element_list = segment.split('\n')
    if len(element_list) == 3: # for example element_list = ['1', '00:00:00,000 --> 00:00:13,680', 'Otele canlı yayın koyacağım, böylece mesajlara ve diğer şeylere bakabileceğiz.']
        if dirty:
            processed_segment_list.extend(allocate_long_text(tmp_segment_list))
            dirty = False
            tmp_segment_list = []
        if tmp_segment_list:
            processed_segment_list.append(tmp_segment_list.pop(0))
        tmp_segment_list.append(segment)
    elif len(element_list) == 2: # for example element_list = ['9', '00:00:40,640 --> 00:00:41,640']
        tmp_segment_list.append(segment)
        dirty = True
    else:
        print(segment)
        raise ValueError('Something went wrong...')

The key is the function allocate_long_text. For its parameter, tmp_segment_list include one segment with long text and several segments with no text. You said you want to split them randomly, but you can do better. You have the duration of each segment, so if the duration of one segment is long, you can allocate more text to this segment. In this way, you need to write some function to parse time and cut the long text to len(tmp_segment_list) parts based on each segment's duration. Otherwise, you can just separate the sentence with words number equally.

Step 3: write the result to the file:

with open('processed.srt', 'w') as proc_file:
    proc_file.write('\n\n'.join(processed_segment_list))