Download app.py from mmty/VideoComp: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/spaces/mmty/VideoComp/resolve/main/app.py
- Command line
-
hf download hf://spaces/mmty/VideoComp/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mmty/VideoComp/resolve/main/app.py
2.85 kB
| import streamlit as st | |
| from moviepy.editor import VideoFileClip | |
| import os | |
| from io import BytesIO | |
| from tempfile import NamedTemporaryFile | |
| import requests | |
| # Function to compress the video | |
| def compress_video(input_path, output_path, target_bitrate="500k"): | |
| video = VideoFileClip(input_path) | |
| video.write_videofile(output_path, bitrate=target_bitrate) | |
| return output_path | |
| # Function to download video from URL with enhanced error handling | |
| def download_video(url): | |
| headers = { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" | |
| } | |
| try: | |
| response = requests.get(url, headers=headers, stream=True) | |
| response.raise_for_status() | |
| video_data = BytesIO(response.content) | |
| return video_data | |
| except requests.exceptions.HTTPError as http_err: | |
| st.error(f"HTTP error occurred: {http_err}") | |
| except Exception as err: | |
| st.error(f"An error occurred: {err}") | |
| return None | |
| st.title("Advanced Video Compressor") | |
| # File uploader | |
| uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"]) | |
| remote_url = st.text_input("Or enter a remote video URL") | |
| if st.button("Compress Video"): | |
| if uploaded_file is not None: | |
| with NamedTemporaryFile(delete=False) as temp_input_file: | |
| temp_input_file.write(uploaded_file.read()) | |
| temp_input_path = temp_input_file.name | |
| with NamedTemporaryFile(delete=False) as temp_output_file: | |
| temp_output_path = temp_output_file.name | |
| compressed_file_path = compress_video(temp_input_path, temp_output_path) | |
| st.success("Video compressed successfully!") | |
| st.download_button("Download Compressed Video", data=open(compressed_file_path, 'rb'), file_name="compressed_video.mp4") | |
| # Clean up temporary files | |
| os.remove(temp_input_path) | |
| os.remove(compressed_file_path) | |
| elif remote_url: | |
| video_data = download_video(remote_url) | |
| if video_data: | |
| with NamedTemporaryFile(delete=False) as temp_input_file: | |
| temp_input_file.write(video_data.read()) | |
| temp_input_path = temp_input_file.name | |
| with NamedTemporaryFile(delete=False) as temp_output_file: | |
| temp_output_path = temp_output_file.name | |
| compressed_file_path = compress_video(temp_input_path, temp_output_path) | |
| st.success("Video compressed successfully!") | |
| st.download_button("Download Compressed Video", data=open(compressed_file_path, 'rb'), file_name="compressed_video.mp4") | |
| # Clean up temporary files | |
| os.remove(temp_input_path) | |
| os.remove(compressed_file_path) | |
| else: | |
| st.error("Please upload a video file or enter a remote video URL.") |