Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,25 @@ from transformers import pipeline
|
|
2 |
from gtts import gTTS
|
3 |
import subprocess
|
4 |
import streamlit as st
|
|
|
5 |
|
6 |
-
# Step 1: Extract
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def extract_text_from_audio(audio_path):
|
8 |
# Load the ASR pipeline from Hugging Face with a Whisper-like model
|
9 |
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
@@ -18,7 +35,7 @@ def extract_text_from_audio(audio_path):
|
|
18 |
|
19 |
return text
|
20 |
|
21 |
-
# Step
|
22 |
def generate_voice_over(text, output_audio_path="voice_over.mp3"):
|
23 |
# Generate audio with gTTS
|
24 |
tts = gTTS(text=text, lang="en")
|
@@ -26,7 +43,7 @@ def generate_voice_over(text, output_audio_path="voice_over.mp3"):
|
|
26 |
print(f"Voice-over saved as {output_audio_path}")
|
27 |
return output_audio_path
|
28 |
|
29 |
-
# Step
|
30 |
def add_voice_over_to_video(video_path, audio_path, output_video_path="output_video_with_voice.mp4"):
|
31 |
# Use FFmpeg to combine video with new audio
|
32 |
ffmpeg_command = [
|
@@ -36,29 +53,33 @@ def add_voice_over_to_video(video_path, audio_path, output_video_path="output_vi
|
|
36 |
"-c:v", "copy",
|
37 |
"-map", "0:v:0",
|
38 |
"-map", "1:a:0",
|
39 |
-
"-shortest",
|
40 |
output_video_path
|
41 |
]
|
42 |
subprocess.run(ffmpeg_command)
|
43 |
print(f"Final video with voice-over saved as {output_video_path}")
|
44 |
|
45 |
-
# Step
|
46 |
def main(video_path):
|
47 |
-
# Step 1: Extract
|
48 |
-
|
|
|
|
|
|
|
49 |
print("Extracted Text:", text)
|
50 |
|
51 |
-
# Step
|
52 |
-
|
53 |
|
54 |
-
# Step
|
55 |
-
add_voice_over_to_video(video_path,
|
56 |
|
57 |
# Streamlit interface to upload video file
|
58 |
uploaded_file = st.file_uploader("Upload a video file", type=["mp4"])
|
59 |
if uploaded_file is not None:
|
|
|
60 |
with open("input_video.mp4", "wb") as f:
|
61 |
f.write(uploaded_file.getbuffer())
|
62 |
|
63 |
-
# Call the main function after video is uploaded
|
64 |
main("input_video.mp4")
|
|
|
2 |
from gtts import gTTS
|
3 |
import subprocess
|
4 |
import streamlit as st
|
5 |
+
import os
|
6 |
|
7 |
+
# Step 1: Extract audio from the video
|
8 |
+
def extract_audio_from_video(video_path, audio_path="extracted_audio.mp3"):
|
9 |
+
# Use FFmpeg to extract audio from the video file
|
10 |
+
ffmpeg_command = [
|
11 |
+
"ffmpeg",
|
12 |
+
"-i", video_path, # Input video
|
13 |
+
"-vn", # Disable video processing
|
14 |
+
"-acodec", "libmp3lame", # Set audio codec to mp3
|
15 |
+
"-ar", "44100", # Set audio sample rate
|
16 |
+
"-ac", "2", # Set number of audio channels
|
17 |
+
audio_path
|
18 |
+
]
|
19 |
+
subprocess.run(ffmpeg_command)
|
20 |
+
print(f"Audio extracted to {audio_path}")
|
21 |
+
return audio_path
|
22 |
+
|
23 |
+
# Step 2: Extract text from the audio using Hugging Face Transformers (Whisper)
|
24 |
def extract_text_from_audio(audio_path):
|
25 |
# Load the ASR pipeline from Hugging Face with a Whisper-like model
|
26 |
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
|
|
35 |
|
36 |
return text
|
37 |
|
38 |
+
# Step 3: Generate voice-over using gTTS
|
39 |
def generate_voice_over(text, output_audio_path="voice_over.mp3"):
|
40 |
# Generate audio with gTTS
|
41 |
tts = gTTS(text=text, lang="en")
|
|
|
43 |
print(f"Voice-over saved as {output_audio_path}")
|
44 |
return output_audio_path
|
45 |
|
46 |
+
# Step 4: Combine voice-over with original video using FFmpeg
|
47 |
def add_voice_over_to_video(video_path, audio_path, output_video_path="output_video_with_voice.mp4"):
|
48 |
# Use FFmpeg to combine video with new audio
|
49 |
ffmpeg_command = [
|
|
|
53 |
"-c:v", "copy",
|
54 |
"-map", "0:v:0",
|
55 |
"-map", "1:a:0",
|
56 |
+
"-shortest", # Ensure the video ends when the audio ends
|
57 |
output_video_path
|
58 |
]
|
59 |
subprocess.run(ffmpeg_command)
|
60 |
print(f"Final video with voice-over saved as {output_video_path}")
|
61 |
|
62 |
+
# Step 5: Run the complete process
|
63 |
def main(video_path):
|
64 |
+
# Step 1: Extract audio from the video
|
65 |
+
audio_path = extract_audio_from_video(video_path)
|
66 |
+
|
67 |
+
# Step 2: Extract text from the audio
|
68 |
+
text = extract_text_from_audio(audio_path)
|
69 |
print("Extracted Text:", text)
|
70 |
|
71 |
+
# Step 3: Generate voice-over from extracted text
|
72 |
+
voice_over_path = generate_voice_over(text)
|
73 |
|
74 |
+
# Step 4: Add voice-over to the video
|
75 |
+
add_voice_over_to_video(video_path, voice_over_path)
|
76 |
|
77 |
# Streamlit interface to upload video file
|
78 |
uploaded_file = st.file_uploader("Upload a video file", type=["mp4"])
|
79 |
if uploaded_file is not None:
|
80 |
+
# Save the uploaded file as input_video.mp4
|
81 |
with open("input_video.mp4", "wb") as f:
|
82 |
f.write(uploaded_file.getbuffer())
|
83 |
|
84 |
+
# Call the main function after the video is uploaded
|
85 |
main("input_video.mp4")
|