Advertisement
Guest User

auto_subs

a guest
Jan 9th, 2024
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. from moviepy.editor import *
  2.  
  3. audio_path = '09-01-24_18_43_191po9d.mp3'
  4. subtitle_path = 'words_transcription.srt'
  5. output_video_path = 'words_output_video.mp4'
  6.  
  7. def srt_to_seconds(time_string):
  8.     hours, minutes, rest = time_string.split(':')
  9.     seconds, milliseconds = rest.replace(',', '.').split('.')
  10.     return (
  11.         int(hours) * 3600 +
  12.         int(minutes) * 60 +
  13.         int(seconds) +
  14.         int(milliseconds) / 1000
  15.     )
  16.  
  17. audio = AudioFileClip(audio_path)
  18.  
  19. subtitles = []
  20. with open(subtitle_path, 'r', encoding='utf-8') as file:
  21.     subtitle_lines = file.read().strip().split('\n\n')
  22.     for line in subtitle_lines:
  23.         parts = line.strip().split('\n')
  24.         if len(parts) >= 3:
  25.             start, end = parts[1].split(' --> ')
  26.             start = srt_to_seconds(start)
  27.             end = srt_to_seconds(end)
  28.             text = '\n'.join(parts[2:])
  29.             subtitles.append((start, end, text))
  30.  
  31. video_clips = []
  32.  
  33. for i, subtitle in enumerate(subtitles):
  34.     start, end, text = subtitle
  35.     duration = end - start
  36.  
  37.     txt_clip = TextClip(text, fontsize=120, color='white')
  38.     txt_clip = txt_clip.set_start(start).set_duration(duration)
  39.  
  40.     video_clips.append(txt_clip)
  41.  
  42. final_video = concatenate_videoclips(video_clips, method="compose", bg_color=None, padding=-0.01)
  43. final_video = final_video.set_audio(audio)
  44. final_video.write_videofile(output_video_path, codec='libx264', audio_codec='aac', temp_audiofile='temp-audio.m4a',
  45.                             remove_temp=True, fps=15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement