afzalsomro15684's picture
Update app.py
3a047d2 verified
raw
history blame
3.23 kB
import streamlit as st
from moviepy.editor import VideoFileClip, concatenate_videoclips, TextClip, CompositeVideoClip
from moviepy.video.fx.all import crop
import os
# Streamlit app
st.title("🎥 Simple Video Editor")
st.write("Edit your videos online and make them copyright-free!")
# Upload video
uploaded_file = st.file_uploader("Upload a video:", type=["mp4", "avi", "mov"])
if uploaded_file is not None:
# Save uploaded file
with open("input_video.mp4", "wb") as f:
f.write(uploaded_file.getbuffer())
# Load video
video = VideoFileClip("input_video.mp4")
# Sidebar for editing options
st.sidebar.header("Editing Options")
task = st.sidebar.selectbox("Select a task:", [
"Trim Video", "Crop Video", "Add Text", "Export Video"
])
if task == "Trim Video":
st.header("Trim Video")
start_time = st.number_input("Start Time (seconds):", min_value=0, max_value=int(video.duration))
end_time = st.number_input("End Time (seconds):", min_value=0, max_value=int(video.duration))
if st.button("Trim"):
trimmed_video = video.subclip(start_time, end_time)
trimmed_video.write_videofile("trimmed_video.mp4")
st.video("trimmed_video.mp4")
elif task == "Crop Video":
st.header("Crop Video")
x1 = st.number_input("X1:", min_value=0, max_value=video.size[0])
y1 = st.number_input("Y1:", min_value=0, max_value=video.size[1])
x2 = st.number_input("X2:", min_value=0, max_value=video.size[0])
y2 = st.number_input("Y2:", min_value=0, max_value=video.size[1])
if st.button("Crop"):
cropped_video = crop(video, x1=x1, y1=y1, x2=x2, y2=y2)
cropped_video.write_videofile("cropped_video.mp4")
st.video("cropped_video.mp4")
elif task == "Add Text":
st.header("Add Text")
text = st.text_input("Enter text:")
fontsize = st.number_input("Font Size:", min_value=10, max_value=100, value=50)
color = st.color_picker("Text Color:", "#FFFFFF")
position_x = st.number_input("Position X:", min_value=0, max_value=video.size[0])
position_y = st.number_input("Position Y:", min_value=0, max_value=video.size[1])
if st.button("Add Text"):
text_clip = TextClip(text, fontsize=fontsize, color=color)
text_clip = text_clip.set_position((position_x, position_y)).set_duration(video.duration)
final_video = CompositeVideoClip([video, text_clip])
final_video.write_videofile("text_video.mp4")
st.video("text_video.mp4")
elif task == "Export Video":
st.header("Export Video")
st.write("Your video is ready to download!")
st.video("input_video.mp4")
with open("input_video.mp4", "rb") as f:
st.download_button("Download Video", f, file_name="edited_video.mp4")
# Clean up temporary files
if os.path.exists("input_video.mp4"):
os.remove("input_video.mp4")
if os.path.exists("trimmed_video.mp4"):
os.remove("trimmed_video.mp4")
if os.path.exists("cropped_video.mp4"):
os.remove("cropped_video.mp4")
if os.path.exists("text_video.mp4"):
os.remove("text_video.mp4")