Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from moviepy.editor import VideoFileClip
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from tempfile import NamedTemporaryFile
|
| 7 |
+
|
| 8 |
+
# Function to compress the video
|
| 9 |
+
def compress_video(input_path, output_path, target_bitrate="500k"):
|
| 10 |
+
video = VideoFileClip(input_path)
|
| 11 |
+
video.write_videofile(output_path, bitrate=target_bitrate)
|
| 12 |
+
return output_path
|
| 13 |
+
|
| 14 |
+
# Function to download video from URL
|
| 15 |
+
def download_video(url):
|
| 16 |
+
response = requests.get(url, stream=True)
|
| 17 |
+
if response.status_code == 200:
|
| 18 |
+
video_data = BytesIO(response.content)
|
| 19 |
+
return video_data
|
| 20 |
+
else:
|
| 21 |
+
st.error("Error downloading the video. Please check the URL.")
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
st.title("Advanced Video Compressor")
|
| 25 |
+
|
| 26 |
+
# File uploader
|
| 27 |
+
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"])
|
| 28 |
+
remote_url = st.text_input("Or enter a remote video URL")
|
| 29 |
+
|
| 30 |
+
if st.button("Compress Video"):
|
| 31 |
+
if uploaded_file is not None:
|
| 32 |
+
with NamedTemporaryFile(delete=False) as temp_input_file:
|
| 33 |
+
temp_input_file.write(uploaded_file.read())
|
| 34 |
+
temp_input_path = temp_input_file.name
|
| 35 |
+
|
| 36 |
+
with NamedTemporaryFile(delete=False) as temp_output_file:
|
| 37 |
+
temp_output_path = temp_output_file.name
|
| 38 |
+
|
| 39 |
+
compressed_file_path = compress_video(temp_input_path, temp_output_path)
|
| 40 |
+
st.success("Video compressed successfully!")
|
| 41 |
+
st.download_button("Download Compressed Video", data=open(compressed_file_path, 'rb'), file_name="compressed_video.mp4")
|
| 42 |
+
|
| 43 |
+
# Clean up temporary files
|
| 44 |
+
os.remove(temp_input_path)
|
| 45 |
+
os.remove(compressed_file_path)
|
| 46 |
+
|
| 47 |
+
elif remote_url:
|
| 48 |
+
video_data = download_video(remote_url)
|
| 49 |
+
if video_data:
|
| 50 |
+
with NamedTemporaryFile(delete=False) as temp_input_file:
|
| 51 |
+
temp_input_file.write(video_data.read())
|
| 52 |
+
temp_input_path = temp_input_file.name
|
| 53 |
+
|
| 54 |
+
with NamedTemporaryFile(delete=False) as temp_output_file:
|
| 55 |
+
temp_output_path = temp_output_file.name
|
| 56 |
+
|
| 57 |
+
compressed_file_path = compress_video(temp_input_path, temp_output_path)
|
| 58 |
+
st.success("Video compressed successfully!")
|
| 59 |
+
st.download_button("Download Compressed Video", data=open(compressed_file_path, 'rb'), file_name="compressed_video.mp4")
|
| 60 |
+
|
| 61 |
+
# Clean up temporary files
|
| 62 |
+
os.remove(temp_input_path)
|
| 63 |
+
os.remove(compressed_file_path)
|
| 64 |
+
else:
|
| 65 |
+
st.error("Please upload a video file or enter a remote video URL.")
|