mrbeliever commited on
Commit
7e97124
·
verified ·
1 Parent(s): b8787ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -51
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 transformers import pipeline
 
 
6
 
7
- # Initialize the Hugging Face text generation pipeline (using GPT-Neo or GPT-2)
8
- generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def download_video(url):
11
- ydl_opts = {
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
- def trim_video(video_path, start_time, end_time):
19
- video = VideoFileClip(video_path)
 
 
 
 
 
20
  trimmed_video = video.subclip(start_time, end_time)
21
- trimmed_video.write_videofile("trimmed_video.mp4")
22
 
23
- def transcribe_audio(video_path):
24
- recognizer = sr.Recognizer()
25
- with sr.AudioFile(video_path) as source:
26
- audio = recognizer.record(source)
27
- try:
28
- text = recognizer.recognize_google(audio) # This uses Google's free Web Speech API
29
- return text
30
- except sr.UnknownValueError:
31
- return "Sorry, I could not understand the audio."
32
- except sr.RequestError:
33
- return "Error with the API request."
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
- # Transcribe audio to text
54
- transcription = transcribe_audio("trimmed_video.mp4")
55
 
56
- # Generate subtitles using Hugging Face's GPT model
57
- subtitles = generate_subtitles(transcription)
58
 
59
- # Show trimmed video and generated subtitles
60
- st.video("trimmed_video.mp4")
61
- st.subheader("Subtitles:")
62
- st.write(subtitles)
 
 
 
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)