import streamlit as st
from moviepy.editor import VideoFileClip
import os
import tempfile
def convert_and_save_video(uploaded_file):
# Create a temporary file to hold the uploaded video
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(uploaded_file.read())
tfile.close() # Close the file after writing
# Load video file using MoviePy
clip = VideoFileClip(tfile.name)
# Convert and Save the video to the appropriate format
if uploaded_file.type == 'video/mp4':
converted_file = os.path.splitext(tfile.name)[0] + ".webm"
clip.write_videofile(converted_file, codec='libvpx')
elif uploaded_file.type == 'video/webm':
converted_file = os.path.splitext(tfile.name)[0] + ".mp4"
clip.write_videofile(converted_file, codec='libx264')
else:
st.error("Unsupported file format")
converted_file = None
# Return the name of the converted file
return converted_file
# Streamlit app
st.title("MP4 to WebM Converter")
uploaded_file = st.file_uploader("Choose a video file", type=['mp4','webm'])
if uploaded_file is not None:
st.video(uploaded_file)
st.write(uploaded_file)
with st.spinner('Converting...'):
webm_file = convert_and_save_video(uploaded_file)
st.success('Conversion complete!')
st.video(webm_file)
Something like this: