Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install required libraries
|
2 |
+
# pip install -U openai-whisper gtts
|
3 |
+
|
4 |
+
import whisper
|
5 |
+
from gtts import gTTS
|
6 |
+
import subprocess
|
7 |
+
|
8 |
+
# Step 1: Extract Text from Video using Whisper
|
9 |
+
def extract_text_from_video(video_path):
|
10 |
+
# Load the Whisper model
|
11 |
+
model = whisper.load_model("base")
|
12 |
+
|
13 |
+
# Transcribe the video file's audio
|
14 |
+
result = model.transcribe(video_path)
|
15 |
+
|
16 |
+
# Extract the transcribed text
|
17 |
+
text = result["text"]
|
18 |
+
|
19 |
+
# Save the transcribed text to a file (optional)
|
20 |
+
with open("video_text.txt", "w") as f:
|
21 |
+
f.write(text)
|
22 |
+
|
23 |
+
return text
|
24 |
+
|
25 |
+
# Step 2: Generate Voice-Over using gTTS
|
26 |
+
def generate_voice_over(text, output_audio_path="voice_over.mp3"):
|
27 |
+
# Generate audio with gTTS
|
28 |
+
tts = gTTS(text=text, lang="en")
|
29 |
+
tts.save(output_audio_path)
|
30 |
+
print(f"Voice-over saved as {output_audio_path}")
|
31 |
+
return output_audio_path
|
32 |
+
|
33 |
+
# Step 3: Combine Voice-Over with Original Video using FFmpeg
|
34 |
+
def add_voice_over_to_video(video_path, audio_path, output_video_path="output_video_with_voice.mp4"):
|
35 |
+
# Use FFmpeg to combine video with new audio
|
36 |
+
ffmpeg_command = [
|
37 |
+
"ffmpeg",
|
38 |
+
"-i", video_path,
|
39 |
+
"-i", audio_path,
|
40 |
+
"-c:v", "copy",
|
41 |
+
"-map", "0:v:0",
|
42 |
+
"-map", "1:a:0",
|
43 |
+
"-shortest",
|
44 |
+
output_video_path
|
45 |
+
]
|
46 |
+
subprocess.run(ffmpeg_command)
|
47 |
+
print(f"Final video with voice-over saved as {output_video_path}")
|
48 |
+
|
49 |
+
# Run the complete process
|
50 |
+
def main(video_path):
|
51 |
+
# Step 1: Extract text from video
|
52 |
+
text = extract_text_from_video(video_path)
|
53 |
+
print("Extracted Text:", text)
|
54 |
+
|
55 |
+
# Step 2: Generate voice-over from extracted text
|
56 |
+
audio_path = generate_voice_over(text)
|
57 |
+
|
58 |
+
# Step 3: Add voice-over to the video
|
59 |
+
add_voice_over_to_video(video_path, audio_path)
|
60 |
+
|
61 |
+
# Provide the path to your input video file
|
62 |
+
main("input_video.mp4")
|