File size: 1,641 Bytes
dbbf1c7
 
62375dd
dbbf1c7
 
 
9eef4b5
dbbf1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eef4b5
 
 
 
dbbf1c7
 
 
 
 
 
 
62375dd
 
 
 
ba7e3be
62375dd
 
9eef4b5
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
import streamlit as st
import subprocess
import os

def combine_video_subtitle(video_file, subtitle_file):
    # Output video file name
    output_file = os.path.join(st.session_state.temp_dir, "output_combined.mp4")
    
    # Run ffmpeg command to combine video and subtitle
    cmd = [
        "ffmpeg",
        "-i", video_file.name,
        "-i", subtitle_file.name,
        "-c:v", "copy",
        "-c:a", "copy",
        "-c:s", "mov_text",
        "-map", "0:v:0",
        "-map", "0:a:0",
        "-map", "1",
        output_file
    ]
    
    subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return output_file

# Streamlit UI
st.title("Video Subtitle Combiner")
st.write("Combine a video file and a subtitle file using ffmpeg.")

# Create a temporary directory to store the output file
st.session_state.temp_dir = st.session_state.get("temp_dir", os.path.join(os.getcwd(), "temp"))
os.makedirs(st.session_state.temp_dir, exist_ok=True)

video_file = st.file_uploader("Upload Video File", type=["mp4", "avi", "mov"])
subtitle_file = st.file_uploader("Upload Subtitle File", type=["srt"])

if video_file and subtitle_file:
    st.write("Combining... Please wait.")
    output_filename = combine_video_subtitle(video_file, subtitle_file)
    st.success("Video and subtitle combined successfully!")
    
    # Display the combined video
    with open(output_filename, "rb") as f:
        video_bytes = f.read()
    st.video(video_bytes, format="video/mp4")
    
    # Provide a link to download the combined video
    st.download_button("Download Combined Video", video_bytes, file_name="output_combined.mp4")