RobCaamano commited on
Commit
f58528d
1 Parent(s): e193799

Update translated_video.py

Browse files
Files changed (1) hide show
  1. translated_video.py +77 -77
translated_video.py CHANGED
@@ -1,77 +1,77 @@
1
- from moviepy.editor import VideoFileClip, AudioFileClip
2
- from pydub import AudioSegment
3
- import srt
4
- import datetime
5
- import ffmpeg
6
- import os
7
- import re
8
-
9
- def create_translated_video(original_video_path, translated_audio_path, translated_text_path, output_dir='./translated'):
10
- # Load original video
11
- video = VideoFileClip(original_video_path)
12
-
13
- # Load TTS audio
14
- new_audio = AudioFileClip(translated_audio_path)
15
- video = video.set_audio(new_audio)
16
- audio_segment = AudioSegment.from_file(translated_audio_path, format="wav")
17
-
18
- # Check if new audio is shorter to pad with silence
19
- if new_audio.duration < video.duration:
20
- silence_duration = (video.duration - new_audio.duration) * 1000 # convert to milliseconds
21
- silence_segment = AudioSegment.silent(duration=silence_duration)
22
- audio_segment = audio_segment + silence_segment
23
- padded_audio_path = os.path.join(output_dir, 'padded_audio.wav')
24
- audio_segment.export(padded_audio_path, format='wav')
25
- new_audio = AudioFileClip(padded_audio_path)
26
-
27
- # Generate SRT content
28
- def parse_translated_text(file_path):
29
- with open(file_path, 'r') as file:
30
- content = file.readlines()
31
-
32
- subtitles = []
33
- timestamp_pattern = re.compile(r'\[(\d+\.\d+)\-(\d+\.\d+)\]')
34
- for line in content:
35
- match = timestamp_pattern.match(line)
36
- if match:
37
- start_time = datetime.timedelta(seconds=float(match.group(1)))
38
- end_time = datetime.timedelta(seconds=float(match.group(2)))
39
- text = line[match.end():].strip()
40
-
41
- subtitle = srt.Subtitle(index=len(subtitles)+1,
42
- start=start_time,
43
- end=end_time,
44
- content=text)
45
- subtitles.append(subtitle)
46
-
47
- return srt.compose(subtitles)
48
-
49
- # Generate SRT content
50
- srt_content = parse_translated_text(translated_text_path)
51
-
52
- # Write to an SRT file
53
- srt_file = './translated/translated.srt'
54
- with open(srt_file, 'w', encoding='utf-8') as file:
55
- file.write(srt_content)
56
-
57
- # Write the final video file
58
- temp = "./translated/temp.mp4"
59
- video.write_videofile(temp)
60
-
61
- # Add subtitles
62
- final_video_file = os.path.join(output_dir, "final_video.mp4")
63
-
64
- # Correct the subtitle filter string for ffmpeg
65
- subtitle_filter_str = f"subtitles='{srt_file}'"
66
-
67
- try:
68
- ffmpeg.input(temp).output(final_video_file, vf=subtitle_filter_str).run()
69
- except ffmpeg.Error as e:
70
- print(f"Error creating final video: {e}")
71
- return None
72
-
73
- # Remove temp file
74
- os.remove(temp)
75
- return final_video_file
76
-
77
- # The rest of your script, if any
 
1
+ from moviepy.editor import VideoFileClip, AudioFileClip
2
+ from pydub import AudioSegment
3
+ import srt
4
+ import datetime
5
+ import ffmpeg
6
+ import os
7
+ import re
8
+
9
+ def create_translated_video(original_video_path, translated_audio_path, translated_text_path, video_name, output_dir='./translated'):
10
+ # Load original video
11
+ video = VideoFileClip(original_video_path)
12
+
13
+ # Load TTS audio
14
+ new_audio = AudioFileClip(translated_audio_path)
15
+ video = video.set_audio(new_audio)
16
+ audio_segment = AudioSegment.from_file(translated_audio_path, format="wav")
17
+
18
+ # Check if new audio is shorter to pad with silence
19
+ if new_audio.duration < video.duration:
20
+ silence_duration = (video.duration - new_audio.duration) * 1000 # convert to milliseconds
21
+ silence_segment = AudioSegment.silent(duration=silence_duration)
22
+ audio_segment = audio_segment + silence_segment
23
+ padded_audio_path = os.path.join(output_dir, 'padded_audio.wav')
24
+ audio_segment.export(padded_audio_path, format='wav')
25
+ new_audio = AudioFileClip(padded_audio_path)
26
+
27
+ # Generate SRT content
28
+ def parse_translated_text(file_path):
29
+ with open(file_path, 'r') as file:
30
+ content = file.readlines()
31
+
32
+ subtitles = []
33
+ timestamp_pattern = re.compile(r'\[(\d+\.\d+)\-(\d+\.\d+)\]')
34
+ for line in content:
35
+ match = timestamp_pattern.match(line)
36
+ if match:
37
+ start_time = datetime.timedelta(seconds=float(match.group(1)))
38
+ end_time = datetime.timedelta(seconds=float(match.group(2)))
39
+ text = line[match.end():].strip()
40
+
41
+ subtitle = srt.Subtitle(index=len(subtitles)+1,
42
+ start=start_time,
43
+ end=end_time,
44
+ content=text)
45
+ subtitles.append(subtitle)
46
+
47
+ return srt.compose(subtitles)
48
+
49
+ # Generate SRT content
50
+ srt_content = parse_translated_text(translated_text_path)
51
+
52
+ # Write to an SRT file
53
+ srt_file = './translated/translated.srt'
54
+ with open(srt_file, 'w', encoding='utf-8') as file:
55
+ file.write(srt_content)
56
+
57
+ # Write the final video file
58
+ temp = "./translated/temp.mp4"
59
+ video.write_videofile(temp)
60
+
61
+ # Add subtitles
62
+ final_video_file = os.path.join(output_dir, video_name)
63
+
64
+ # Correct the subtitle filter string for ffmpeg
65
+ subtitle_filter_str = f"subtitles='{srt_file}'"
66
+
67
+ try:
68
+ ffmpeg.input(temp).output(final_video_file, vf=subtitle_filter_str).run()
69
+ except ffmpeg.Error as e:
70
+ print(f"Error creating final video: {e}")
71
+ return None
72
+
73
+ # Remove temp file
74
+ os.remove(temp)
75
+ return final_video_file
76
+
77
+ # The rest of your script, if any