Spaces:
Sleeping
Sleeping
Update Milestone5API.py
Browse files- Milestone5API.py +10 -16
Milestone5API.py
CHANGED
|
@@ -19,27 +19,21 @@ def replace_video_audio(video_file_path, new_audio_file_path, output_video_file_
|
|
| 19 |
video = video.set_audio(new_audio)
|
| 20 |
|
| 21 |
video.write_videofile(output_video_file_path, codec="libx264", audio_codec="aac", temp_audiofile='temp_audio.mp3', remove_temp=True)
|
| 22 |
-
def text_to_speech(
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
tts = TTS("tts_models/fr/mai/tacotron2-DDC").to(device)
|
| 25 |
-
text_files_dir = f'CS370_Milestone5/captions/{translated_text}'
|
| 26 |
output_audio_dir = 'CS370_Milestone5/audio'
|
| 27 |
os.makedirs(output_audio_dir, exist_ok=True)
|
| 28 |
-
|
| 29 |
-
for file_name in os.listdir(text_files_dir):
|
| 30 |
-
if file_name.endswith('.txt'):
|
| 31 |
-
file_path = os.path.join(text_files_dir, file_name)
|
| 32 |
-
file_name_only = os.path.splitext(file_name)[0]
|
| 33 |
-
|
| 34 |
-
with open(file_path, 'r', encoding='utf-8') as file:
|
| 35 |
-
translated_text = file.read()
|
| 36 |
-
|
| 37 |
-
output_file_path = os.path.join(output_audio_dir, f"{file_name_only}.wav")
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
tts.tts_to_file(text=translated_text, file_path=output_file_path)
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
replace_video_audio(video_path, output_file_path, output_video_path)
|
| 45 |
return output_video_path
|
|
|
|
| 19 |
video = video.set_audio(new_audio)
|
| 20 |
|
| 21 |
video.write_videofile(output_video_file_path, codec="libx264", audio_codec="aac", temp_audiofile='temp_audio.mp3', remove_temp=True)
|
| 22 |
+
def text_to_speech(translated_text_file_path, video_path):
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
tts = TTS("tts_models/fr/mai/tacotron2-DDC").to(device)
|
|
|
|
| 25 |
output_audio_dir = 'CS370_Milestone5/audio'
|
| 26 |
os.makedirs(output_audio_dir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
file_name_only = os.path.splitext(os.path.basename(translated_text_file_path))[0]
|
| 29 |
+
output_file_path = os.path.join(output_audio_dir, f"{file_name_only}.wav")
|
| 30 |
+
|
| 31 |
+
with open(translated_text_file_path, 'r', encoding='utf-8') as file:
|
| 32 |
+
translated_text = file.read()
|
| 33 |
+
|
| 34 |
+
tts.tts_to_file(text=translated_text, file_path=output_file_path)
|
| 35 |
+
|
| 36 |
+
output_video_path = f"CS370_Milestone5/videos/{file_name_only}_new.mp4"
|
| 37 |
|
| 38 |
replace_video_audio(video_path, output_file_path, output_video_path)
|
| 39 |
return output_video_path
|