Spaces:
Sleeping
Sleeping
Commit ·
a457107
1
Parent(s): d0eaa3b
Update dub.py
Browse files
dub.py
CHANGED
|
@@ -5,12 +5,6 @@ from pydub import AudioSegment
|
|
| 5 |
import moviepy.editor as mp
|
| 6 |
import speech_recognition as sr
|
| 7 |
|
| 8 |
-
def text_to_speech(text, lang='es'):
|
| 9 |
-
tts = gTTS(text=text, lang=lang, slow=False)
|
| 10 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
|
| 11 |
-
tts.save(temp_file.name)
|
| 12 |
-
return temp_file.name
|
| 13 |
-
|
| 14 |
def transcribe_and_dub(video_file):
|
| 15 |
temp_folder = tempfile.mkdtemp()
|
| 16 |
|
|
@@ -21,19 +15,14 @@ def transcribe_and_dub(video_file):
|
|
| 21 |
audio_file = os.path.join(temp_folder, "audio.wav")
|
| 22 |
video.audio.write_audiofile(audio_file)
|
| 23 |
|
| 24 |
-
process = os.popen(f'ffmpeg -i "{audio_file}" -ar 16000 -ac 1 "{os.path.join(temp_folder, "audio16000.wav")}"')
|
| 25 |
-
process.close()
|
| 26 |
-
|
| 27 |
-
audio_file = os.path.join(temp_folder, "audio16000.wav")
|
| 28 |
-
|
| 29 |
with sr.AudioFile(audio_file) as source:
|
| 30 |
audio = recognizer.record(source)
|
| 31 |
|
| 32 |
text = recognizer.recognize_google(audio, language='es')
|
| 33 |
-
print(f"
|
| 34 |
|
| 35 |
dubbed_audio_file = text_to_speech(text)
|
| 36 |
-
print(f"
|
| 37 |
|
| 38 |
original_audio = AudioSegment.from_file(audio_file, format="wav")
|
| 39 |
dubbed_audio = AudioSegment.from_mp3(dubbed_audio_file)
|
|
@@ -44,10 +33,10 @@ def transcribe_and_dub(video_file):
|
|
| 44 |
|
| 45 |
dubbed_video_file = os.path.join(temp_folder, "dubbed_video.mp4")
|
| 46 |
video_with_dubbed_audio.write_videofile(dubbed_video_file, codec="libx264", audio_codec="aac", verbose=False)
|
| 47 |
-
print(f"
|
| 48 |
|
| 49 |
return dubbed_video_file
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
-
print(f"Error
|
| 53 |
-
return None
|
|
|
|
| 5 |
import moviepy.editor as mp
|
| 6 |
import speech_recognition as sr
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def transcribe_and_dub(video_file):
|
| 9 |
temp_folder = tempfile.mkdtemp()
|
| 10 |
|
|
|
|
| 15 |
audio_file = os.path.join(temp_folder, "audio.wav")
|
| 16 |
video.audio.write_audiofile(audio_file)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with sr.AudioFile(audio_file) as source:
|
| 19 |
audio = recognizer.record(source)
|
| 20 |
|
| 21 |
text = recognizer.recognize_google(audio, language='es')
|
| 22 |
+
print(f"Texto reconocido: {text}")
|
| 23 |
|
| 24 |
dubbed_audio_file = text_to_speech(text)
|
| 25 |
+
print(f"Archivo de audio doblado: {dubbed_audio_file}")
|
| 26 |
|
| 27 |
original_audio = AudioSegment.from_file(audio_file, format="wav")
|
| 28 |
dubbed_audio = AudioSegment.from_mp3(dubbed_audio_file)
|
|
|
|
| 33 |
|
| 34 |
dubbed_video_file = os.path.join(temp_folder, "dubbed_video.mp4")
|
| 35 |
video_with_dubbed_audio.write_videofile(dubbed_video_file, codec="libx264", audio_codec="aac", verbose=False)
|
| 36 |
+
print(f"Archivo de video doblado: {dubbed_video_file}")
|
| 37 |
|
| 38 |
return dubbed_video_file
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
+
print(f"Error durante la transcripción y el doblaje: {str(e)}")
|
| 42 |
+
return None
|