forked from Huanshere/VideoLingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep6_generate_final_timeline.py
175 lines (138 loc) · 7.13 KB
/
step6_generate_final_timeline.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import pandas as pd
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import re
from rich.panel import Panel
from rich.console import Console
import autocorrect_py as autocorrect
console = Console()
CLEANED_CHUNKS_FILE = 'output/log/cleaned_chunks.xlsx'
TRANSLATION_RESULTS_FOR_SUBTITLES_FILE = 'output/log/translation_results_for_subtitles.xlsx'
TRANSLATION_RESULTS_REMERGED_FILE = 'output/log/translation_results_remerged.xlsx'
OUTPUT_DIR = 'output'
AUDIO_OUTPUT_DIR = 'output/audio'
SUBTITLE_OUTPUT_CONFIGS = [
('src.srt', ['Source']),
('trans.srt', ['Translation']),
('src_trans.srt', ['Source', 'Translation']),
('trans_src.srt', ['Translation', 'Source'])
]
AUDIO_SUBTITLE_OUTPUT_CONFIGS = [
('src_subs_for_audio.srt', ['Source']),
('trans_subs_for_audio.srt', ['Translation'])
]
def convert_to_srt_format(start_time, end_time):
"""Convert time (in seconds) to the format: hours:minutes:seconds,milliseconds"""
def seconds_to_hmsm(seconds):
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
seconds = seconds % 60
milliseconds = int(seconds * 1000) % 1000
return f"{hours:02d}:{minutes:02d}:{int(seconds):02d},{milliseconds:03d}"
start_srt = seconds_to_hmsm(start_time)
end_srt = seconds_to_hmsm(end_time)
return f"{start_srt} --> {end_srt}"
def remove_punctuation(text):
text = re.sub(r'\s+', ' ', text)
text = re.sub(r'[^\w\s]', '', text)
return text.strip()
def show_difference(str1, str2):
"""Show the difference positions between two strings"""
min_len = min(len(str1), len(str2))
diff_positions = []
for i in range(min_len):
if str1[i] != str2[i]:
diff_positions.append(i)
if len(str1) != len(str2):
diff_positions.extend(range(min_len, max(len(str1), len(str2))))
print("Difference positions:")
print(f"Expected sentence: {str1}")
print(f"Actual match: {str2}")
print("Position markers: " + "".join("^" if i in diff_positions else " " for i in range(max(len(str1), len(str2)))))
print(f"Difference indices: {diff_positions}")
def get_sentence_timestamps(df_words, df_sentences):
time_stamp_list = []
# Build complete string and position mapping
full_words_str = ''
position_to_word_idx = {}
for idx, word in enumerate(df_words['text']):
clean_word = remove_punctuation(word.lower())
start_pos = len(full_words_str)
full_words_str += clean_word
for pos in range(start_pos, len(full_words_str)):
position_to_word_idx[pos] = idx
current_pos = 0
for idx, sentence in df_sentences['Source'].items():
clean_sentence = remove_punctuation(sentence.lower()).replace(" ", "")
sentence_len = len(clean_sentence)
match_found = False
while current_pos <= len(full_words_str) - sentence_len:
if full_words_str[current_pos:current_pos+sentence_len] == clean_sentence:
start_word_idx = position_to_word_idx[current_pos]
end_word_idx = position_to_word_idx[current_pos + sentence_len - 1]
time_stamp_list.append((
float(df_words['start'][start_word_idx]),
float(df_words['end'][end_word_idx])
))
current_pos += sentence_len
match_found = True
break
current_pos += 1
if not match_found:
print(f"\n⚠️ Warning: No exact match found for sentence: {sentence}")
show_difference(clean_sentence,
full_words_str[current_pos:current_pos+len(clean_sentence)])
print("\nOriginal sentence:", df_sentences['Source'][idx])
raise ValueError("❎ No match found for sentence.")
return time_stamp_list
def align_timestamp(df_text, df_translate, subtitle_output_configs: list, output_dir: str, for_display: bool = True):
"""Align timestamps and add a new timestamp column to df_translate"""
df_trans_time = df_translate.copy()
# Assign an ID to each word in df_text['text'] and create a new DataFrame
words = df_text['text'].str.split(expand=True).stack().reset_index(level=1, drop=True).reset_index()
words.columns = ['id', 'word']
words['id'] = words['id'].astype(int)
# Process timestamps ⏰
time_stamp_list = get_sentence_timestamps(df_text, df_translate)
df_trans_time['timestamp'] = time_stamp_list
df_trans_time['duration'] = df_trans_time['timestamp'].apply(lambda x: x[1] - x[0])
# Remove gaps 🕳️
for i in range(len(df_trans_time)-1):
delta_time = df_trans_time.loc[i+1, 'timestamp'][0] - df_trans_time.loc[i, 'timestamp'][1]
if 0 < delta_time < 1:
df_trans_time.at[i, 'timestamp'] = (df_trans_time.loc[i, 'timestamp'][0], df_trans_time.loc[i+1, 'timestamp'][0])
# Convert start and end timestamps to SRT format
df_trans_time['timestamp'] = df_trans_time['timestamp'].apply(lambda x: convert_to_srt_format(x[0], x[1]))
# Polish subtitles: replace punctuation in Translation if for_display
if for_display:
df_trans_time['Translation'] = df_trans_time['Translation'].apply(lambda x: re.sub(r'[,。]', ' ', x).strip())
# Output subtitles 📜
def generate_subtitle_string(df, columns):
return ''.join([f"{i+1}\n{row['timestamp']}\n{row[columns[0]].strip()}\n{row[columns[1]].strip() if len(columns) > 1 else ''}\n\n" for i, row in df.iterrows()]).strip()
if output_dir:
os.makedirs(output_dir, exist_ok=True)
for filename, columns in subtitle_output_configs:
subtitle_str = generate_subtitle_string(df_trans_time, columns)
with open(os.path.join(output_dir, filename), 'w', encoding='utf-8') as f:
f.write(subtitle_str)
return df_trans_time
# ✨ Beautify the translation
def clean_translation(x):
if pd.isna(x):
return ''
cleaned = str(x).strip('。').strip(',')
return autocorrect.format(cleaned)
def align_timestamp_main():
df_text = pd.read_excel(CLEANED_CHUNKS_FILE)
df_text['text'] = df_text['text'].str.strip('"').str.strip()
df_translate = pd.read_excel(TRANSLATION_RESULTS_FOR_SUBTITLES_FILE)
df_translate['Translation'] = df_translate['Translation'].apply(clean_translation)
align_timestamp(df_text, df_translate, SUBTITLE_OUTPUT_CONFIGS, OUTPUT_DIR)
console.print(Panel("[bold green]🎉📝 Subtitles generation completed! Please check in the `output` folder 👀[/bold green]"))
# for audio
df_translate_for_audio = pd.read_excel(TRANSLATION_RESULTS_REMERGED_FILE) # use remerged file to avoid unmatched lines when dubbing
df_translate_for_audio['Translation'] = df_translate_for_audio['Translation'].apply(clean_translation)
align_timestamp(df_text, df_translate_for_audio, AUDIO_SUBTITLE_OUTPUT_CONFIGS, AUDIO_OUTPUT_DIR)
console.print(Panel("[bold green]🎉📝 Audio subtitles generation completed! Please check in the `output/audio` folder 👀[/bold green]"))
if __name__ == '__main__':
align_timestamp_main()