Spaces:
Build error
Build error
mrbeliever
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,74 @@
|
|
1 |
import streamlit as st
|
2 |
-
import youtube_dl
|
3 |
from moviepy.editor import VideoFileClip
|
4 |
import speech_recognition as sr
|
5 |
-
from
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
'format': 'bestvideo+bestaudio/best',
|
13 |
-
'outtmpl': 'downloaded_video.mp4'
|
14 |
-
}
|
15 |
-
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
16 |
-
ydl.download([url])
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
trimmed_video = video.subclip(start_time, end_time)
|
21 |
-
trimmed_video.write_videofile("trimmed_video.mp4")
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
def generate_subtitles(text):
|
36 |
-
# Use Hugging Face's GPT-Neo model for text generation
|
37 |
-
generated_text = generator(text, max_length=150, num_return_sequences=1)
|
38 |
-
return generated_text[0]['generated_text']
|
39 |
-
|
40 |
-
# Streamlit Interface
|
41 |
-
st.title("YouTube Video Trimmer and Subtitle Generator")
|
42 |
-
url = st.text_input("Enter YouTube URL")
|
43 |
-
start_time = st.selectbox("Select Start Time", [0, 15, 30, 60])
|
44 |
-
end_time = st.selectbox("Select End Time", [15, 30, 60, 90])
|
45 |
-
|
46 |
-
font_style = st.selectbox("Select Font Style", ["Arial", "Helvetica", "Times New Roman", "Courier"])
|
47 |
-
|
48 |
-
if st.button("Generate"):
|
49 |
-
# Download and trim the video
|
50 |
-
download_video(url)
|
51 |
-
trim_video("downloaded_video.mp4", start_time, end_time)
|
52 |
|
53 |
-
|
54 |
-
transcription = transcribe_audio("trimmed_video.mp4")
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
|
59 |
-
#
|
60 |
-
st.
|
61 |
-
|
62 |
-
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from moviepy.editor import VideoFileClip
|
3 |
import speech_recognition as sr
|
4 |
+
from moviepy.editor import TextClip, concatenate_videoclips
|
5 |
+
from tempfile import NamedTemporaryFile
|
6 |
+
import os
|
7 |
|
8 |
+
# Function to extract subtitles using speech recognition
|
9 |
+
def generate_subtitles(video_path):
|
10 |
+
recognizer = sr.Recognizer()
|
11 |
+
clip = VideoFileClip(video_path)
|
12 |
+
audio = clip.audio
|
13 |
+
audio_file = NamedTemporaryFile(delete=False, suffix='.wav')
|
14 |
+
audio.write_audiofile(audio_file.name)
|
15 |
+
|
16 |
+
with sr.AudioFile(audio_file.name) as source:
|
17 |
+
audio_data = recognizer.record(source)
|
18 |
+
try:
|
19 |
+
# Recognize speech using Google Web Speech API
|
20 |
+
text = recognizer.recognize_google(audio_data)
|
21 |
+
except sr.UnknownValueError:
|
22 |
+
text = "Could not understand audio"
|
23 |
+
except sr.RequestError:
|
24 |
+
text = "Could not request results"
|
25 |
+
|
26 |
+
os.remove(audio_file.name)
|
27 |
+
return text
|
28 |
+
|
29 |
+
# Streamlit App
|
30 |
+
st.title("Video Trimmer with Subtitles")
|
31 |
+
|
32 |
+
# Video upload
|
33 |
+
uploaded_file = st.file_uploader("Choose a video...", type=["mp4", "avi", "mov", "mkv"])
|
34 |
+
|
35 |
+
if uploaded_file is not None:
|
36 |
+
# Display uploaded video
|
37 |
+
st.video(uploaded_file)
|
38 |
|
39 |
+
# Load video file
|
40 |
+
video = VideoFileClip(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Time options for trimming
|
43 |
+
time_options = [15, 30, 60] # in seconds
|
44 |
+
trim_time = st.selectbox("Select trim duration", time_options)
|
45 |
+
|
46 |
+
# User selects trim time
|
47 |
+
start_time = 0
|
48 |
+
end_time = trim_time
|
49 |
trimmed_video = video.subclip(start_time, end_time)
|
|
|
50 |
|
51 |
+
# Display trimmed video
|
52 |
+
st.video(trimmed_video)
|
53 |
+
|
54 |
+
# User selects subtitle font style
|
55 |
+
font_style = st.selectbox("Choose subtitle font style", ["Arial", "Courier", "Times-Roman", "Impact"])
|
56 |
+
|
57 |
+
# Generate subtitles
|
58 |
+
subtitles_text = generate_subtitles(uploaded_file)
|
59 |
+
|
60 |
+
# Add subtitles to video
|
61 |
+
subtitle_clip = TextClip(subtitles_text, fontsize=24, color='white', font=font_style)
|
62 |
+
subtitle_clip = subtitle_clip.set_position('bottom').set_duration(trimmed_video.duration)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
final_video = concatenate_videoclips([trimmed_video.set_audio(trimmed_video.audio), subtitle_clip])
|
|
|
65 |
|
66 |
+
# Display final video
|
67 |
+
st.video(final_video)
|
68 |
|
69 |
+
# Button to generate video
|
70 |
+
if st.button("Generate Video"):
|
71 |
+
# Save the final video
|
72 |
+
final_output = NamedTemporaryFile(delete=False, suffix='.mp4')
|
73 |
+
final_video.write_videofile(final_output.name, codec="libx264")
|
74 |
+
st.download_button("Download Final Video", final_output.name)
|