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")