File size: 2,842 Bytes
f0ceee4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from moviepy.editor import VideoFileClip, AudioFileClip
from pydub import AudioSegment
import srt
import datetime
import ffmpeg
import os
import re

def create_translated_video(original_video_path, translated_audio_path, translated_text_path, output_dir='./translated'):
    # Load original video
    video = VideoFileClip(original_video_path)

    # Load TTS audio
    new_audio = AudioFileClip(translated_audio_path)
    video = video.set_audio(new_audio)
    audio_segment = AudioSegment.from_file(translated_audio_path, format="wav")

    # Check if new audio is shorter to pad with silence
    if new_audio.duration < video.duration:
        silence_duration = (video.duration - new_audio.duration) * 1000  # convert to milliseconds
        silence_segment = AudioSegment.silent(duration=silence_duration)
        audio_segment = audio_segment + silence_segment
        padded_audio_path = os.path.join(output_dir, 'padded_audio.wav')
        audio_segment.export(padded_audio_path, format='wav')
        new_audio = AudioFileClip(padded_audio_path)

    # Generate SRT content
    def parse_translated_text(file_path):
        with open(file_path, 'r') as file:
            content = file.readlines()

        subtitles = []
        timestamp_pattern = re.compile(r'\[(\d+\.\d+)\-(\d+\.\d+)\]')
        for line in content:
            match = timestamp_pattern.match(line)
            if match:
                start_time = datetime.timedelta(seconds=float(match.group(1)))
                end_time = datetime.timedelta(seconds=float(match.group(2)))
                text = line[match.end():].strip()

                subtitle = srt.Subtitle(index=len(subtitles)+1,
                                        start=start_time,
                                        end=end_time,
                                        content=text)
                subtitles.append(subtitle)

        return srt.compose(subtitles)

    # Generate SRT content
    srt_content = parse_translated_text(translated_text_path)

    # Write to an SRT file
    srt_file = './translated/translated.srt'
    with open(srt_file, 'w', encoding='utf-8') as file:
        file.write(srt_content)

    # Write the final video file
    temp = "./translated/temp.mp4"
    video.write_videofile(temp)

    # Add subtitles
    final_video_file = os.path.join(output_dir, "final_video.mp4")

    # Correct the subtitle filter string for ffmpeg
    subtitle_filter_str = f"subtitles='{srt_file}'"

    try:
        ffmpeg.input(temp).output(final_video_file, vf=subtitle_filter_str).run()
    except ffmpeg.Error as e:
        print(f"Error creating final video: {e}")
        return None

    # Remove temp file
    os.remove(temp)
    return final_video_file

# The rest of your script, if any