Datasets:
Download align.py from CanCLID/zoengjyutgaai: direct link, hf CLI and curl.
- Browser
- Download file 3.61 kB
-
https://huggingface.co/datasets/CanCLID/zoengjyutgaai/resolve/main/align.py
- Command line
-
hf download hf://datasets/CanCLID/zoengjyutgaai/align.py
-
curl -L -o align.py https://huggingface.co/datasets/CanCLID/zoengjyutgaai/resolve/main/align.py
3.61 kB
| 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(81, 93): | |
| 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}") | |