File size: 2,526 Bytes
bc71799
 
 
7e97124
 
 
bc71799
7e97124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc71799
7e97124
 
bc71799
7e97124
 
 
 
 
 
 
bc71799
 
7e97124
 
 
 
 
 
 
 
 
 
 
 
bc71799
7e97124
bc71799
7e97124
 
bc71799
7e97124
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import streamlit as st
from moviepy.editor import VideoFileClip
import speech_recognition as sr
from moviepy.editor import TextClip, concatenate_videoclips
from tempfile import NamedTemporaryFile
import os

# Function to extract subtitles using speech recognition
def generate_subtitles(video_path):
    recognizer = sr.Recognizer()
    clip = VideoFileClip(video_path)
    audio = clip.audio
    audio_file = NamedTemporaryFile(delete=False, suffix='.wav')
    audio.write_audiofile(audio_file.name)
    
    with sr.AudioFile(audio_file.name) as source:
        audio_data = recognizer.record(source)
        try:
            # Recognize speech using Google Web Speech API
            text = recognizer.recognize_google(audio_data)
        except sr.UnknownValueError:
            text = "Could not understand audio"
        except sr.RequestError:
            text = "Could not request results"
    
    os.remove(audio_file.name)
    return text

# Streamlit App
st.title("Video Trimmer with Subtitles")

# Video upload
uploaded_file = st.file_uploader("Choose a video...", type=["mp4", "avi", "mov", "mkv"])

if uploaded_file is not None:
    # Display uploaded video
    st.video(uploaded_file)

    # Load video file
    video = VideoFileClip(uploaded_file)

    # Time options for trimming
    time_options = [15, 30, 60]  # in seconds
    trim_time = st.selectbox("Select trim duration", time_options)

    # User selects trim time
    start_time = 0
    end_time = trim_time
    trimmed_video = video.subclip(start_time, end_time)

    # Display trimmed video
    st.video(trimmed_video)

    # User selects subtitle font style
    font_style = st.selectbox("Choose subtitle font style", ["Arial", "Courier", "Times-Roman", "Impact"])

    # Generate subtitles
    subtitles_text = generate_subtitles(uploaded_file)
    
    # Add subtitles to video
    subtitle_clip = TextClip(subtitles_text, fontsize=24, color='white', font=font_style)
    subtitle_clip = subtitle_clip.set_position('bottom').set_duration(trimmed_video.duration)
    
    final_video = concatenate_videoclips([trimmed_video.set_audio(trimmed_video.audio), subtitle_clip])
    
    # Display final video
    st.video(final_video)
    
    # Button to generate video
    if st.button("Generate Video"):
        # Save the final video
        final_output = NamedTemporaryFile(delete=False, suffix='.mp4')
        final_video.write_videofile(final_output.name, codec="libx264")
        st.download_button("Download Final Video", final_output.name)