-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_with_pyTube.py
100 lines (73 loc) · 3.09 KB
/
read_with_pyTube.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
import os
import moviepy.editor as mp
import pyttsx3
from moviepy.audio.AudioClip import concatenate_audioclips
from pytube import YouTube
import numpy as np
import re
os.environ["HOST_PATH"] = "C://"
host_path = os.environ["HOST_PATH"]
def clean_filename(filename):
cleaned = re.sub(r'[^\w\s]', '', filename)
cleaned = cleaned.strip().replace(" ", "_")
if not cleaned or cleaned.isspace():
cleaned = "default_filename"
return cleaned
def download_video_from_url(url, output_filename):
yt = YouTube(url)
stream = yt.streams.filter(file_extension='mp4', progressive=True).order_by('resolution').desc().first()
stream.download(filename=output_filename)
def parse_srt_time(time_str):
hours, minutes, seconds, milliseconds = re.match(r'(\d{2}):(\d{2}):(\d{2})[:,](\d{3})', time_str).groups()
return int(hours) * 3600 + int(minutes) * 60 + int(seconds) + int(milliseconds) / 1000
def load_subtitles(subtitles_file):
with open(subtitles_file, 'r', encoding='utf-8') as f:
content = f.read()
blocks = content.strip().split('\n\n')
subtitles = []
for block in blocks:
lines = block.strip().split('\n')
start, duration = map(parse_srt_time, lines[1].split(' --> '))
text = ' '.join(lines[2:])
subtitles.append((start, duration, text))
return subtitles
def generate_speech(text):
engine = pyttsx3.init()
os.makedirs(f"{host_path}sound", exist_ok=True)
cleaned_text = clean_filename(text)
engine.save_to_file(text, f"{host_path}sound/{cleaned_text}.mp3")
engine.runAndWait()
print(f"Generated audio file: {cleaned_text}")
def silence_get_frame(t):
global silence_array
return silence_array
def concatenate_audio_with_silence(audio_clip, silence_duration):
global silence_array
silence_array = np.zeros((int(silence_duration * audio_clip.fps), audio_clip.nchannels), dtype=np.float32)
silence = mp.AudioClip(silence_get_frame, duration=silence_duration, fps=audio_clip.fps)
return concatenate_audioclips([audio_clip, silence])
def main(video_url, subtitles_file, output_file):
# Téléchargez la vidéo
temp_video_file = "temp_video.mp4"
download_video_from_url(video_url, temp_video_file)
# Chargez les sous-titres
subtitles = load_subtitles(subtitles_file)
# Chargez la vidéo téléchargée
video = mp.VideoFileClip(temp_video_file)
# Générer et ajouter des fichiers audio pour chaque sous-titre
audio_clips = []
for start, end, text in subtitles:
cleaned_text = clean_filename(text)
audio_file = f"{host_path}Sound/{cleaned_text}.mp3"
if not os.path.exists(audio_file):
generate_speech(text)
audio = mp.AudioFileClip(audio_file)
audio = audio.set_start(start)
audio_clips.append(audio)
composite_audio = mp.CompositeAudioClip(audio_clips)
final_video = video.set_audio(composite_audio)
# Exporte la vidéo avec les sous-titres ajoutés
final_video.write_videofile(output_file)
# Ferme et supprime le fichier vidéo temporaire
video.close()
os.remove(temp_video_file)