animatedaliensfans commited on
Commit
94cbe98
·
verified ·
1 Parent(s): 3341299

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sounddevice as sd
3
+ import numpy as np
4
+ import wave
5
+ from moviepy.editor import VideoFileClip, AudioFileClip
6
+
7
+ def record_audio(output_path, duration=10, fs=44100):
8
+ """
9
+ Records audio for a specified duration and saves it to the given output path.
10
+ """
11
+ print(f"Recording audio... (Press Ctrl+C to stop)")
12
+
13
+ # Record audio
14
+ try:
15
+ audio_data = sd.rec(int(duration * fs), samplerate=fs, channels=2, dtype='float64')
16
+ sd.wait() # Wait until recording is finished
17
+ print("Recording finished.")
18
+
19
+ # Save audio as a .wav file
20
+ with wave.open(output_path, 'wb') as wf:
21
+ wf.setnchannels(2)
22
+ wf.setsampwidth(2) # 2 bytes for audio
23
+ wf.setframerate(fs)
24
+ wf.writeframes((audio_data * 32767).astype(np.int16).tobytes())
25
+ print(f"Audio saved to {output_path}")
26
+
27
+ except KeyboardInterrupt:
28
+ print("Recording interrupted.")
29
+ except Exception as e:
30
+ print(f"An error occurred during recording: {e}")
31
+
32
+ def dub_video(original_video_path, new_audio_path, output_video_path):
33
+ """
34
+ Replaces the audio in the original video with the new audio.
35
+ """
36
+ if not os.path.isfile(original_video_path):
37
+ print(f"Error: {original_video_path} does not exist.")
38
+ return
39
+
40
+ if not os.path.isfile(new_audio_path):
41
+ print(f"Error: {new_audio_path} does not exist.")
42
+ return
43
+
44
+ print("Dubbing the video...")
45
+
46
+ # Load video and new audio
47
+ video = VideoFileClip(original_video_path)
48
+ new_audio = AudioFileClip(new_audio_path)
49
+
50
+ # Set the new audio to the video
51
+ final_video = video.set_audio(new_audio)
52
+
53
+ # Write the output video file
54
+ final_video.write_videofile(output_video_path, codec='libx264')
55
+ print(f"Dubbing finished! Output saved at: {output_video_path}")
56
+
57
+ def main():
58
+ original_video_path = input("Enter the path to the original video file (e.g., video.mp4): ")
59
+ audio_output_path = "dubbed_audio.wav" # Temporary audio file
60
+ output_video_path = "dubbed_video.mp4" # Output video file
61
+
62
+ # Record new audio
63
+ duration = int(input("Enter the duration for the audio recording (in seconds): "))
64
+ record_audio(audio_output_path, duration)
65
+
66
+ # Dub the video
67
+ dub_video(original_video_path, audio_output_path, output_video_path)
68
+
69
+ if __name__ == "__main__":
70
+ main()