Spaces:
Running
Running
import streamlit as st | |
import moviepy.editor as mp | |
import tempfile | |
import os | |
def main(): | |
st.title("Simple Video Editor") | |
# File uploaders | |
video_file = st.file_uploader("Upload a video file (MP4)", type=["mp4"]) | |
audio_file = st.file_uploader("Upload an audio file (MP3 or WAV)", type=["mp3", "wav"]) | |
if video_file and audio_file: | |
# Save uploaded files to temporary locations | |
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") | |
temp_video.write(video_file.read()) | |
temp_video_path = temp_video.name | |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=f".{audio_file.name.split('.')[-1]}") | |
temp_audio.write(audio_file.read()) | |
temp_audio_path = temp_audio.name | |
# Load video and audio | |
video = mp.VideoFileClip(temp_video_path) | |
audio = mp.AudioFileClip(temp_audio_path) | |
# Display video info | |
st.write(f"Video duration: {video.duration:.2f} seconds") | |
st.write(f"Audio duration: {audio.duration:.2f} seconds") | |
# Trim video | |
st.subheader("Trim Video") | |
video_start = st.slider("Video start time (seconds)", 0.0, video.duration, 0.0) | |
video_end = st.slider("Video end time (seconds)", 0.0, video.duration, video.duration) | |
trimmed_video = video.subclip(video_start, video_end) | |
# Trim audio | |
st.subheader("Trim Audio") | |
audio_start = st.slider("Audio start time (seconds)", 0.0, audio.duration, 0.0) | |
audio_end = st.slider("Audio end time (seconds)", 0.0, audio.duration, audio.duration) | |
trimmed_audio = audio.subclip(audio_start, audio_end) | |
# Combine video and audio | |
if st.button("Combine Video and Audio"): | |
final_clip = trimmed_video.set_audio(trimmed_audio) | |
# Save the final video | |
output_path = "output_video.mp4" | |
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac") | |
# Provide download link | |
st.success("Video processing complete!") | |
with open(output_path, "rb") as file: | |
btn = st.download_button( | |
label="Download processed video", | |
data=file, | |
file_name="processed_video.mp4", | |
mime="video/mp4" | |
) | |
# Display video player | |
st.subheader("Preview Processed Video") | |
st.video(output_path) | |
# Clean up temporary files | |
video.close() | |
audio.close() | |
os.unlink(temp_video_path) | |
os.unlink(temp_audio_path) | |
if __name__ == "__main__": | |
main() |