import streamlit as st import moviepy.editor as mp def reduce_video_size(input_file, output_file, target_size_mb=199): video_clip = mp.VideoFileClip(input_file) audio_clip = video_clip.audio # Reduce video quality to minimize file size reduced_video_clip = video_clip.resize(width=320, height=240) # Set a target bit rate for the video to achieve the desired size target_bitrate = (target_size_mb * 8 * 1024 * 1024) / (video_clip.duration) reduced_video_clip.write_videofile(output_file, audio_codec='aac', bitrate=f"{target_bitrate}k", codec="libx264") # Close the video clips to release resources video_clip.close() reduced_video_clip.close() # Streamlit UI st.title("Video Size Reducer") uploaded_file = st.file_uploader("Upload a video file (.mp4)", type="mp4") if uploaded_file: st.text("Uploaded file: " + uploaded_file.name) # Set output file path output_file_path = f"reduced_{uploaded_file.name}" if st.button("Reduce Video Size"): st.text("Reducing video size... Please wait.") try: reduce_video_size(uploaded_file.name, output_file_path) st.success(f"Video size reduced successfully. Download link: [Download Reduced Video]({output_file_path})") except Exception as e: st.error(f"Error: {str(e)}")