File size: 3,611 Bytes
32adf42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import re
import audioread


def fix_srt_delay(input_file, output_file, delay_start_ms, delay_end_ms, audio_file):
    """
    Fixes a progressive delay in an SRT file by linearly adjusting timestamps.
    Gets video duration from an associated audio file.

    Args:
        input_file: Path to the input SRT file.
        output_file: Path to the output SRT file with corrected timestamps.
        delay_start_ms: Delay at the beginning of the video (in milliseconds).
        delay_end_ms: Delay at the end of the video (in milliseconds).
        audio_file: Path to the audio file to get duration from.
    """

    try:
        with audioread.audio_open(audio_file) as f:
            total_duration_ms = int(f.duration * 1000)  # Get duration in milliseconds
    except audioread.NoBackendError:
        print("Error: No suitable audio backend found. Please install ffmpeg or another supported library.")
        return
    except Exception as e:
        print(f"Error reading audio file: {e}")
        return

    with open(input_file, 'r', encoding='utf-8') as infile, open(output_file, 'w', encoding='utf-8') as outfile:
        for line in infile:
            if '-->' in line:
                # Extract start and end timestamps
                start_time_str, end_time_str = line.strip().split(' --> ')
                start_time_ms = srt_time_to_milliseconds(start_time_str)
                end_time_ms = srt_time_to_milliseconds(end_time_str)

                # Calculate the adjusted delay for this subtitle
                progress = start_time_ms / total_duration_ms
                current_delay_ms = delay_start_ms + progress * (delay_end_ms - delay_start_ms)

                # Adjust the timestamps
                adjusted_start_time_ms = start_time_ms - current_delay_ms
                adjusted_end_time_ms = end_time_ms - current_delay_ms

                # Convert back to SRT time format
                adjusted_start_time_str = milliseconds_to_srt_time(adjusted_start_time_ms)
                adjusted_end_time_str = milliseconds_to_srt_time(adjusted_end_time_ms)

                # Write the corrected line to the output file
                outfile.write(f"{adjusted_start_time_str} --> {adjusted_end_time_str}\n")
            else:
                # Write non-timestamp lines as they are
                outfile.write(line)


def srt_time_to_milliseconds(time_str):
    """Converts an SRT timestamp string to milliseconds."""
    hours, minutes, seconds_milliseconds = time_str.split(':')
    seconds, milliseconds = seconds_milliseconds.split(',')
    total_milliseconds = (int(hours) * 3600 + int(minutes) * 60 + int(seconds)) * 1000 + int(milliseconds)
    return total_milliseconds


def milliseconds_to_srt_time(milliseconds):
    """Converts milliseconds to an SRT timestamp string."""
    milliseconds = int(milliseconds)
    seconds, milliseconds = divmod(milliseconds, 1000)
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    return f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"


delay_at_start_ms = 10  # Delay at the beginning (you think it starts from 0)
delay_at_end_ms = 2200  # Delay at the end in milliseconds

for i in range(46, 51):
    input_srt_file = f'srt/seoiwuzyun/{i:03d}.srt'  # Your input SRT file
    output_srt_file = f'{i:03d}.srt'  # Output file name
    audio_file_path = f'source/seoiwuzyun/{i:03d}.opus'  # Your Opus audio file

    fix_srt_delay(input_srt_file, output_srt_file, delay_at_start_ms, delay_at_end_ms, audio_file_path)
    print(f"SRT file fixed and saved to {output_srt_file}")