VideoHelp Forum
+ Reply to Thread
Results 1 to 3 of 3
Thread
  1. Hi friends, thanks for the help.


    I need to export the "timing" ("duration") of the dialogues of each subtitle.


    ---------- For example, export from: ----------

    1
    00:01:10,000 --> 00:01:50,000
    Dialogue 1

    2
    00:07:20,840 --> 00:09:20,845
    Dialogue 2


    ---------- Convert o export to file to result in: ----------

    1
    00:01:10,000 --> 00:01:50,000 = 00:00:40,000
    Dialogue 1

    2
    00:07:20,840 --> 00:09:20,840 = 00:02:00,005
    Dialogue 2


    ---------- Or also in: ----------
    1
    00:00:40,000
    Dialogue 1

    2
    00:02:00,005
    Dialogue 2
    Quote Quote  
  2. using python, save this file as totaltimes.py:
    Code:
    def sec2time(sec):
        ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
        m, s = divmod(sec, 60)
        h, m = divmod(m, 60)
        d, h = divmod(h, 24)
        pattern = '%%02d:%%02d:%%0%d.%df' % (6, 3)
        if d == 0:
            return pattern % (h, m, s)
        return (f'{h:d}:{m:02d}:{s:02d}')
    
    def time2sec(time):
        ''' converts HH:MM:SS.FFF or HH:MM:SS,FFF string to seconds '''
        time = time.strip()[:12].replace(',','.')
        h, m, s = map(float, time.split(':'))
        return h * 3600 + m * 60 + s
                          
    def main(old_srt, new_srt):             
        with open(old_srt, 'r+') as f, open(new_srt, 'w') as w:
            for line in f:
                if '-->' in line:
                    start_sec, end_sec, *_  = map(time2sec, line.split('-->'))
                    difference_time = sec2time(end_sec - start_sec).replace('.',',')
                    w.write(f'{line[:-1]} = {difference_time}\n')
                    #w.write(f'{difference_time}\n') 
                else:
                    w.write(line)
    
    
    
    if __name__ == '__main__':
        '''
        Using command line:
        python "totaltimes.py" "old.srt" "new.srt"
        '''
        import os
        import sys
        source_paths = []
        if len(sys.argv) > 1:
            for i, path in enumerate(sys.argv[1:]):
                if i < 1 and not os.path.isfile(path):
                    raise ValueError(f'Not a valid path or file does not exist: {path}')
                source_paths.append(path)        
            if i < 1:
                raise ValueError(f'Two path arguments needed')
            source_paths = source_paths[0:2]
            source_paths = [os.path.abspath(path)  for path in source_paths]
            main(source_paths[0], source_paths[1])
    if using within python:
    Code:
    import totaltimes
    from pathlib import Path
    original = r"D:/path/my_subtitles.srt"
    original = Path(original)
    new = original.parent.joinpath(f'new_{original.name}') #just adds "new_" for a new file
    totaltimes.main(original, new)
    Last edited by _Al_; 25th Jun 2022 at 17:29.
    Quote Quote  
  3. Originally Posted by _Al_ View Post
    using python, save this file as totaltimes.py:
    Code:
    def sec2time(sec):
        ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
        m, s = divmod(sec, 60)
        h, m = divmod(m, 60)
        d, h = divmod(h, 24)
        pattern = '%%02d:%%02d:%%0%d.%df' % (6, 3)
        if d == 0:
            return pattern % (h, m, s)
        return (f'{h:d}:{m:02d}:{s:02d}')
    Thanks _AI_, thanks for coding.
    Quote Quote  



Similar Threads

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