JaMugen commited on
Commit
8c6d6da
·
1 Parent(s): 5bdd7ea

Update Milestone5API.py

Browse files
Files changed (1) hide show
  1. Milestone5API.py +16 -3
Milestone5API.py CHANGED
@@ -9,16 +9,29 @@ from TTS.api import TTS
9
  from moviepy.editor import VideoFileClip
10
  from pydub import AudioSegment
11
  def replace_video_audio(video_file_path, new_audio_file_path, output_video_file_path):
12
-
13
  video = VideoFileClip(video_file_path)
 
14
 
 
15
  new_audio = AudioSegment.from_wav(new_audio_file_path)
 
16
 
 
17
  new_audio = new_audio[:int(video.duration * 1000)]
 
18
 
 
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)
@@ -35,7 +48,7 @@ def text_to_speech(translated_text_file_path, video_path):
35
 
36
  output_video_path = f"CS370_Milestone5/videos/{file_name_only}_new.mp4"
37
 
38
- # Replace the video's audio with the generated audio
39
  replace_video_audio(video_path, output_file_path, output_video_path)
40
  return output_video_path
41
 
 
9
  from moviepy.editor import VideoFileClip
10
  from pydub import AudioSegment
11
  def replace_video_audio(video_file_path, new_audio_file_path, output_video_file_path):
12
+ print("Loading video...")
13
  video = VideoFileClip(video_file_path)
14
+ print("Video loaded successfully.")
15
 
16
+ print("Loading new audio...")
17
  new_audio = AudioSegment.from_wav(new_audio_file_path)
18
+ print("New audio loaded successfully.")
19
 
20
+ print("Trimming audio...")
21
  new_audio = new_audio[:int(video.duration * 1000)]
22
+ print("Audio trimmed successfully.")
23
 
24
+ print("Setting audio to video...")
25
  video = video.set_audio(new_audio)
26
+ print("Audio set to video successfully.")
27
+
28
+ print("Writing video file...")
29
+ try:
30
+ video.write_videofile(output_video_file_path, codec="libx264", audio_codec="aac", remove_temp=True)
31
+ print("Video file written successfully.")
32
+ except Exception as e:
33
+ print("Error writing video file:", e)
34
 
 
35
  def text_to_speech(translated_text_file_path, video_path):
36
  device = "cuda" if torch.cuda.is_available() else "cpu"
37
  tts = TTS("tts_models/fr/mai/tacotron2-DDC").to(device)
 
48
 
49
  output_video_path = f"CS370_Milestone5/videos/{file_name_only}_new.mp4"
50
 
51
+
52
  replace_video_audio(video_path, output_file_path, output_video_path)
53
  return output_video_path
54