edit video
To upload files, please first save the app
import streamlit as st
import tempfile
import os
from datetime import timedelta
st.title("Video Editor")
st.write("Upload a video file to edit its playback settings")
# File uploader
uploaded_file = st.file_uploader("Choose a video file", type=['mp4', 'mov', 'avi', 'mkv'])
if uploaded_file is not None:
# Save uploaded file to temporary location
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
tmp_file.write(uploaded_file.read())
video_path = tmp_file.name
st.success("Video uploaded successfully!")
# Video editing controls
st.subheader("Video Controls")
col1, col2 = st.columns(2)
with col1:
start_time = st.number_input("Start time (seconds)", min_value=0, value=0)
autoplay = st.checkbox("Autoplay", value=False)
muted = st.checkbox("Muted", value=False)
with col2:
end_time = st.number_input("End time (seconds)", min_value=0, value=0)
loop = st.checkbox("Loop", value=False)
# Subtitle upload
st.subheader("Subtitles (Optional)")
subtitle_file = st.file_uploader("Upload subtitle file", type=['vtt', 'srt'])
# Display video with settings
st.subheader("Video Preview")
video_kwargs = {
'autoplay': autoplay,
'loop': loop,
'muted': muted
}
# Add start time if specified
if start_time > 0:
video_kwargs['start_time'] = start_time
# Add end time if specified
if end_time > 0:
video_kwargs['end_time'] = end_time
# Add subtitles if uploaded
if subtitle_file is not None:
with tempfile.NamedTemporaryFile(delete=False, suffix='.vtt') as subtitle_tmp:
subtitle_tmp.write(subtitle_file.read())
video_kwargs['subtitles'] = subtitle_tmp.name
# Display the video
with open(video_path, 'rb') as video_file:
video_bytes = video_file.read()
st.video(video_bytes, **video_kwargs)
# Video information
st.subheader("Video Information")
st.write(f"Original filename: {uploaded_file.name}")
st.write(f"File size: {len(video_bytes) / (1024*1024):.2f} MB")
# Clean up temporary files
try:
os.unlink(video_path)
if 'subtitles' in video_kwargs:
os.unlink(video_kwargs['subtitles'])
except:
pass
else:
st.info("Please upload a video file to get started")
# Show example with a sample video URL
st.subheader("Example with YouTube Video")
st.write("Here's an example with a YouTube video:")
example_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
st.video(example_url, start_time=10, end_time=30)
st.code(f'st.video("{example_url}", start_time=10, end_time=30)')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?