Spaces:
Sleeping
Sleeping
Commit ·
b42a9c7
1
Parent(s): 308bf5c
Create dub.py
Browse files
dub.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gtts import gTTS
|
| 2 |
+
import os
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
from pydub.playback import play
|
| 5 |
+
|
| 6 |
+
def text_to_speech(text, language='en'):
|
| 7 |
+
tts = gTTS(text=text, lang=language, slow=False)
|
| 8 |
+
tts.save('output.mp3')
|
| 9 |
+
return 'output.mp3'
|
| 10 |
+
|
| 11 |
+
def play_audio(audio_file):
|
| 12 |
+
os.system(f'start {audio_file}')
|
| 13 |
+
|
| 14 |
+
def dub_video_segments(cut_files, cuts):
|
| 15 |
+
for i, cut_file in enumerate(cut_files):
|
| 16 |
+
text_to_dub = f"Este es el segmento {i+1}. Reproduciendo ahora."
|
| 17 |
+
audio_file = text_to_speech(text_to_dub)
|
| 18 |
+
|
| 19 |
+
# Cargar el segmento de video y audio
|
| 20 |
+
video_segment = VideoFileClip(cut_file)
|
| 21 |
+
audio_segment = AudioSegment.from_file(audio_file, format="mp3")
|
| 22 |
+
|
| 23 |
+
# Doblar el segmento
|
| 24 |
+
video_segment = video_segment.set_audio(audio_segment)
|
| 25 |
+
|
| 26 |
+
# Guardar el nuevo archivo doblado
|
| 27 |
+
dubbed_file = f"{cut_file.replace('.mp4', '_dubbed.mp4')}"
|
| 28 |
+
video_segment.write_videofile(dubbed_file, codec="libx264", audio_codec="aac", verbose=False)
|
| 29 |
+
|
| 30 |
+
# Reproducir el resultado
|
| 31 |
+
play_audio(dubbed_file)
|