File size: 2,045 Bytes
ce5dfc3
 
7cc95f1
ce5dfc3
 
7cc95f1
e812068
ce5dfc3
7cc95f1
 
 
 
 
 
 
 
 
 
ce5dfc3
 
7cc95f1
 
 
 
ce5dfc3
e812068
 
 
 
 
 
 
 
ce5dfc3
 
7cc95f1
 
 
 
 
ce5dfc3
 
7cc95f1
ce5dfc3
 
 
 
 
 
 
7cc95f1
ce5dfc3
7cc95f1
 
 
 
ce5dfc3
7cc95f1
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
import streamlit as st
from moviepy.editor import VideoFileClip, CompositeVideoClip
import ffmpeg
import tempfile
import os
import shutil
import subprocess

def save_uploaded_file(uploaded_file):
    try:
        with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
            shutil.copyfileobj(uploaded_file, tmp_file)
            return tmp_file.name
    except Exception as e:
        st.error(f"Error saving file: {e}")
        return None

def add_hard_subtitle_to_video(input_video_path, subtitle_file_path):
    output_video = tempfile.mktemp(suffix=".mp4")
    stream = ffmpeg.output(
        ffmpeg.input(input_video_path),
        ffmpeg.input(subtitle_file_path),
        output_video,
        vf=f"subtitles={subtitle_file_path}:force_style='FontName=Arial,FontSize=24'"
    )
    try:
        # Use subprocess to capture stdout and stderr
        out, err = ffmpeg.run(stream, overwrite_output=True, capture_stdout=True, capture_stderr=True)
        return output_video
    except ffmpeg.Error as e:
        # Print stderr to Streamlit to help diagnose the issue
        st.error(f"FFMPEG error: {e.stderr}")
        return None

def video_demo(video, subtitle):
    video_path = save_uploaded_file(video)
    subtitle_path = save_uploaded_file(subtitle)
    
    if video_path and subtitle_path:
        processed_video_path = add_hard_subtitle_to_video(video_path, subtitle_path)
        return processed_video_path
    else:
        return None

# Streamlit UI
st.title("Video Subtitler")

uploaded_video = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
uploaded_subtitle = st.file_uploader("Upload subtitle file", type=["srt", "vtt"])

if uploaded_video and uploaded_subtitle:
    processed_video_path = video_demo(uploaded_video, uploaded_subtitle)
    if processed_video_path:
        st.video(processed_video_path)
    else:
        st.write("An error occurred. Please try again.")
else:
    st.write("Please upload both a video and a subtitle file.")