An open API service indexing awesome lists of open source software.

https://github.com/visual1mpact/8mb-video-compressor

A simple Bash script that compresses videos to ~8MB using ffmpeg, designed for Termux and lightweight environments.
https://github.com/visual1mpact/8mb-video-compressor

8mb-limit aac android-cli bash bitrate-calculation cpu-multithreading discord-upload ffmpeg ffmpeg-script file-size-optimization h264 libx264 linux low-bitrate media-processing termux two-pass-encoding video-compression video-encoder video-optimization

Last synced: 6 days ago
JSON representation

A simple Bash script that compresses videos to ~8MB using ffmpeg, designed for Termux and lightweight environments.

Awesome Lists containing this project

README

          

# ๐ŸŽฌ 8MB Video Compressor (Termux)

A smart Bash script that compresses videos to a **user-defined target size** using `ffmpeg`, designed for **Termux** and lightweight environments.

> ๐Ÿ’ฌ **Discord Note:** Discord currently allows **10MB uploads** for free users. The script defaults to **8MB** for safety but lets you choose another target size interactively at runtime.

Perfect for:
* Discord uploads (10MB free limit โ€” 8MB default)
* Quick file sharing
* Mobile encoding via Termux
* Low-resource Linux systems

---

## ๐Ÿ“ฆ Features

* ๐ŸŽฏ **User-selectable target output size (default 8MB)**
* ๐Ÿ” 2-pass encoding for accurate size targeting
* ๐Ÿ“Š Real-time progress bar
* ๐Ÿง  Automatic bitrate calculation
* ๐Ÿ”Š Smart audio/video bitrate allocation
* ๐Ÿ“ Auto resolution detection
* ๐Ÿ“‰ Auto downscale to 720p if width > 1280px
* โšก Uses all available CPU cores
* ๐Ÿ“ฑ Optimized for Termux & minimal systems
* ๐Ÿงน Automatic temporary file cleanup

---

## ๐Ÿ›  Requirements

You need:

* `bash`
* `ffmpeg`
* `ffprobe`
* `coreutils` (for `nproc`, usually preinstalled)

Install in Termux:

```sh
pkg update
pkg install ffmpeg
````

---

## ๐Ÿš€ Usage

```sh
chmod +x compress8mb.sh
./compress8mb.sh input.mp4 output.mp4
```

When running, you will be prompted:

```text
Enter target size in MB (default 8 MB):
```

* Press **Enter** to keep 8MB
* Type `10` or any other number to change target size

Example:

```sh
./compress8mb.sh video.mp4 compressed.mp4
```

---

## โš™๏ธ How It Works

1. Prompts for target size (default 8MB)
2. Reads video duration using `ffprobe`
3. Converts target size into total available kilobits
4. Reserves **64 kbps** for audio
5. Assigns remaining bitrate to video
6. Downscales to **720p** if width > 1280px
7. Performs **2-pass encoding**:

* Pass 1: analysis
* Pass 2: final encode with progress tracking
8. If file exceeds target:

* Reduces video bitrate by 5%
* Re-encodes automatically
9. Cleans up temporary files

---

## ๐Ÿงฎ Bitrate Formula

```bash
Target size (MB) ร— 8192 = total kilobits
Total kilobits รท duration (seconds) = total bitrate (kbps)
Video bitrate = total bitrate โˆ’ audio bitrate
```

---

## ๐Ÿ“Š Example Output

```text
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Duration: 120s
Target: 8MB
Video Bitrate: 482k
Audio Bitrate: 64k
Threads: 24
Scaling: 720p
Codec: libx264 (2-pass)
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Progress: [########################## ] 54% (65s/120s)
Done: compressed.mp4 (~8MB)
```

---

## โš ๏ธ Notes

* Output size will **never exceed target**
* Very long videos will have lower quality (bitrate constrained)
* Very short videos may look very high quality
* Uses all logical CPU cores detected via `nproc`
* Designed for reliability over raw speed

---

## ๐Ÿ”ง Customization

You can **change the default prompt value** or modify audio/preset options:

```bash
# Default prompt value
DEFAULT_TARGET_MB=8

# Change audio bitrate (kbps)
AUDIO_BITRATE=64

# Change x264 preset
-preset fast
```

---

## ๐Ÿ†š Why 2-Pass?

2-pass encoding:

* Improves bitrate distribution
* Ensures accurate file size
* Produces better quality at low bitrates
* Guarantees โ‰ค TARGET_MB output

This version prioritizes **accuracy and reliability** while remaining lightweight.

---

## ๐Ÿ“„ License

MIT License โ€” free to use and modify.

---