File size: 2,846 Bytes
b3d1ef2
 
 
 
 
bb048ec
b3d1ef2
 
 
 
 
 
 
bb048ec
b3d1ef2
bb048ec
 
 
 
 
 
b3d1ef2
 
bb048ec
 
 
 
 
b3d1ef2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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.")