Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from moviepy.editor import *
- audio_path = '09-01-24_18_43_191po9d.mp3'
- subtitle_path = 'words_transcription.srt'
- output_video_path = 'words_output_video.mp4'
- def srt_to_seconds(time_string):
- hours, minutes, rest = time_string.split(':')
- seconds, milliseconds = rest.replace(',', '.').split('.')
- return (
- int(hours) * 3600 +
- int(minutes) * 60 +
- int(seconds) +
- int(milliseconds) / 1000
- )
- audio = AudioFileClip(audio_path)
- subtitles = []
- with open(subtitle_path, 'r', encoding='utf-8') as file:
- subtitle_lines = file.read().strip().split('\n\n')
- for line in subtitle_lines:
- parts = line.strip().split('\n')
- if len(parts) >= 3:
- start, end = parts[1].split(' --> ')
- start = srt_to_seconds(start)
- end = srt_to_seconds(end)
- text = '\n'.join(parts[2:])
- subtitles.append((start, end, text))
- video_clips = []
- for i, subtitle in enumerate(subtitles):
- start, end, text = subtitle
- duration = end - start
- txt_clip = TextClip(text, fontsize=120, color='white')
- txt_clip = txt_clip.set_start(start).set_duration(duration)
- video_clips.append(txt_clip)
- final_video = concatenate_videoclips(video_clips, method="compose", bg_color=None, padding=-0.01)
- final_video = final_video.set_audio(audio)
- final_video.write_videofile(output_video_path, codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a',
- remove_temp=True, fps=15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement