File size: 3,203 Bytes
e28bb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# ๐ŸŽฌ Video Compressor (ffmpeg + Gradio)

This project is a **simple video compression tool** built with [Gradio](https://gradio.app) for the user interface and [FFmpeg](https://ffmpeg.org/) for the actual compression.  
It allows you to upload a video, set the **CRF (Constant Rate Factor)** value for compression quality, and download the compressed video.  
A real-time progress bar shows the compression progress.

---

## ๐Ÿš€ Features
- Upload common video formats: `.mp4`, `.avi`, `.mov`, `.mkv`
- Compress videos using **H.265 (HEVC)** codec (`libx265`)
- Adjust **CRF** (23 = high quality, 32 = lower quality but smaller file size)
- Real-time progress updates during compression
- Automatic file size comparison after compression
- Download compressed video directly
- Built with **Gradio UI** โ†’ lightweight and easy to run

---

## ๐Ÿ›  Requirements

- Python 3.9+ recommended
- Install dependencies:
  ```bash
  pip install gradio
  ```

- Install **FFmpeg** (required for video compression).  
  - On macOS (Homebrew):  
    ```bash
    brew install ffmpeg
    ```
  - On Ubuntu/Debian:  
    ```bash
    sudo apt update && sudo apt install ffmpeg
    ```
  - On Windows: [Download FFmpeg](https://ffmpeg.org/download.html) and add it to your PATH.

---

## ๐Ÿ“‚ Project Structure

```
video-compressor/
โ”‚โ”€โ”€ app.py            # Main Python script (the Gradio app)
โ”‚โ”€โ”€ README.md         # Documentation
```

---

## โ–ถ๏ธ Usage

Run the app locally with:

```bash
python app.py
```

Then open your browser at: [http://127.0.0.1:7860](http://127.0.0.1:7860)

1. **Upload a video file**  
2. **Choose compression level (CRF)**  
   - Lower CRF = higher quality, larger file size  
   - Higher CRF = lower quality, smaller file size  
   - Default: 28 (balanced)  
3. **Click "Compress Video"**  
4. Wait for the **progress bar** to reach 100%  
5. **Download your compressed video**

---

## โš™๏ธ How It Works

1. The app calls **`ffprobe`** to read the video duration.  
2. It runs **`ffmpeg`** with `libx265` codec and user-specified CRF.  
3. While running, stderr output from ffmpeg is parsed (`time=HH:MM:SS.xx`) to update progress.  
4. After finishing, the app calculates:  
   - Original size (MB)  
   - New size (MB)  
   - Reduction percentage  
5. Displays results + download link for the compressed file.

---

## ๐Ÿ“Š Example Results

- Original: 100 MB  
- Compressed: 35 MB  
- Reduced by: **65%** ๐ŸŽ‰  

---

## ๐Ÿงฉ Code Highlights

- **Helper functions** keep the code clean:  
  - `get_video_duration()` โ†’ fetch video length with ffprobe  
  - `build_ffmpeg_command()` โ†’ builds ffmpeg CLI command  
  - `summarize_compression()` โ†’ calculates file size reduction  
- **Unified exception handling** with `safe_none_on_error()`  
- **Progress tracking** uses regex on ffmpeg stderr

---

## ๐Ÿ”ฎ Future Improvements

- Option to **downscale resolution** (1080p โ†’ 720p, 480p)  
- Add support for different codecs (VP9, AV1)  
- Cloud deployment with Hugging Face Spaces or Docker  

---

## ๐Ÿ“œ License

MIT License. Free to use and modify.  

---

## ๐Ÿ‘จโ€๐Ÿ’ป Author

Developed by **Pavel Lakov**  
๐Ÿ”— [GitHub](https://github.com/PavelLakov)