SplitStream / app.py
naohiro701's picture
Create app.py
7d582a8 verified
import streamlit as st
import ffmpeg
import math
import os
from pathlib import Path
def split_video(input_file_path, output_dir, duration=20):
"""
Splits the input video file into segments of specified duration.
Parameters:
- input_file_path (str): Path to the input video file.
- output_dir (str): Directory to save output segments.
- duration (int): Duration of each segment in seconds.
Returns:
- segment_files (list): List of file paths for each video segment.
"""
probe = ffmpeg.probe(input_file_path)
total_duration = float(probe['format']['duration'])
num_segments = math.ceil(total_duration / duration)
segment_files = []
for i in range(num_segments):
start_time = i * duration
output_file = os.path.join(output_dir, f"segment_{i + 1}.mp4")
ffmpeg.input(input_file_path, ss=start_time, t=duration).output(output_file).run()
segment_files.append(output_file)
return segment_files
# Streamlit UI
st.title("Video Splitter")
st.write("Upload a video file, specify the segment duration, and download the split video segments.")
# File uploader
uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "mov", "avi"])
# Duration input
duration = st.number_input("Segment duration (seconds)", min_value=1, max_value=300, value=20)
if uploaded_file is not None:
# Save the uploaded file to a temporary path
input_file_path = Path("uploaded_video.mp4")
with open(input_file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
# Output directory for segments
output_dir = Path("segments")
output_dir.mkdir(exist_ok=True)
if st.button("Split Video"):
# Split video into segments
segment_files = split_video(str(input_file_path), str(output_dir), duration)
st.success(f"Video split into {len(segment_files)} segments.")
# Provide download links for each segment
for segment in segment_files:
with open(segment, "rb") as file:
btn = st.download_button(
label=f"Download {Path(segment).name}",
data=file,
file_name=Path(segment).name
)