video edit
To upload files, please first save the app
import streamlit as st
import cv2
import numpy as np
from PIL import Image
import tempfile
import os
st.set_page_config(page_title="Video Editor", layout="wide")
st.title("🎬 Video Editor")
st.write("Upload a video and apply various editing effects")
# File upload
uploaded_file = st.file_uploader("Choose a video file", type=['mp4', 'avi', 'mov', 'mkv'])
if uploaded_file is not None:
# Save uploaded file temporarily
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
tmp_file.write(uploaded_file.read())
temp_video_path = tmp_file.name
# Create columns for layout
col1, col2 = st.columns([1, 1])
with col1:
st.subheader("Original Video")
st.video(temp_video_path)
# Video editing options
st.subheader("📝 Editing Options")
# Editing controls
edit_col1, edit_col2, edit_col3 = st.columns(3)
with edit_col1:
brightness = st.slider("Brightness", -100, 100, 0)
contrast = st.slider("Contrast", 0.0, 3.0, 1.0, 0.1)
with edit_col2:
saturation = st.slider("Saturation", 0.0, 2.0, 1.0, 0.1)
blur_strength = st.slider("Blur", 0, 50, 0)
with edit_col3:
start_time = st.number_input("Start Time (seconds)", min_value=0, value=0)
end_time = st.number_input("End Time (seconds)", min_value=1, value=10)
speed = st.selectbox("Speed", [0.5, 1.0, 1.5, 2.0], index=1)
# Effect options
st.subheader("🎨 Effects")
effect_col1, effect_col2 = st.columns(2)
with effect_col1:
grayscale = st.checkbox("Grayscale")
sepia = st.checkbox("Sepia")
with effect_col2:
edge_detection = st.checkbox("Edge Detection")
invert_colors = st.checkbox("Invert Colors")
if st.button("🎬 Apply Effects", type="primary"):
with st.spinner("Processing video... This may take a while"):
try:
# Open video file
cap = cv2.VideoCapture(temp_video_path)
# Get video properties
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Calculate frame range
start_frame = int(start_time * fps)
end_frame = int(end_time * fps)
# Set up output video
output_path = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4').name
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps * speed, (width, height))
# Process frames
frame_count = 0
progress_bar = st.progress(0)
total_frames = end_frame - start_frame
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
while frame_count < total_frames:
ret, frame = cap.read()
if not ret:
break
# Apply effects
processed_frame = frame.copy()
# Brightness and contrast
if brightness != 0 or contrast != 1.0:
processed_frame = cv2.convertScaleAbs(processed_frame, alpha=contrast, beta=brightness)
# Saturation
if saturation != 1.0:
hsv = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2HSV)
hsv[:, :, 1] = cv2.multiply(hsv[:, :, 1], saturation)
processed_frame = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
# Blur
if blur_strength > 0:
kernel_size = blur_strength * 2 + 1
processed_frame = cv2.GaussianBlur(processed_frame, (kernel_size, kernel_size), 0)
# Color effects
if grayscale:
gray = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2GRAY)
processed_frame = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
if sepia:
sepia_filter = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
processed_frame = cv2.transform(processed_frame, sepia_filter)
processed_frame = np.clip(processed_frame, 0, 255).astype(np.uint8)
if edge_detection:
gray = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
processed_frame = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
if invert_colors:
processed_frame = cv2.bitwise_not(processed_frame)
# Write frame
out.write(processed_frame)
frame_count += 1
progress_bar.progress(frame_count / total_frames)
# Release everything
cap.release()
out.release()
# Display processed video
with col2:
st.subheader("Edited Video")
st.video(output_path)
# Download button
with open(output_path, 'rb') as f:
st.download_button(
label="📥 Download Edited Video",
data=f.read(),
file_name=f"edited_{uploaded_file.name}",
mime="video/mp4"
)
st.success("✅ Video processing complete!")
# Clean up temporary files
try:
os.unlink(output_path)
except:
pass
except Exception as e:
st.error(f"❌ Error processing video: {str(e)}")
st.write("Please try with a different video file or different settings.")
# Clean up original temp file
try:
os.unlink(temp_video_path)
except:
pass
else:
st.info("👆 Please upload a video file to start editing")
# Show example features
st.subheader("🌟 Features")
feature_col1, feature_col2, feature_col3 = st.columns(3)
with feature_col1:
st.write("**🎨 Color Adjustments**")
st.write("• Brightness control")
st.write("• Contrast adjustment")
st.write("• Saturation tuning")
with feature_col2:
st.write("**✂️ Video Editing**")
st.write("• Trim video clips")
st.write("• Speed adjustment")
st.write("• Blur effects")
with feature_col3:
st.write("**🎭 Visual Effects**")
st.write("• Grayscale conversion")
st.write("• Sepia tone")
st.write("• Edge detection")
st.write("• Color inversion")
st.markdown("---")
st.write("💡 **Tip**: Start with small video clips for faster processing. Large videos may take several minutes to process.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?