🎬 Video Compression Masterclass

Reduce 1.6GB Videos to 300MB on Ubuntu 24.04

⏱️ Duration: 45 minutes
📊 Level: Beginner to Intermediate
💻 OS: Ubuntu 24.04
🛠️ Tool: FFmpeg
0

Course Overview

Learn how to compress large video files from 1.6GB down to 300MB using FFmpeg on Ubuntu 24.04. Perfect for slow internet connections and cloud storage uploads like Google Drive.

1.6 GB Original Size
24 hours upload
300 MB Compressed Size
~4.5 hours upload

🎯 Learning Objectives

  • Install and configure FFmpeg on Ubuntu 24.04
  • Understand video compression concepts and codecs
  • Use two-pass encoding for optimal quality
  • Calculate exact bitrates for target file sizes
  • Create automated compression scripts
  • Troubleshoot common compression issues
1

Installation & Setup

Step 1: Update System Packages

sudo apt update && sudo apt upgrade -y

Step 2: Install FFmpeg

sudo apt install ffmpeg -y

Step 3: Verify Installation

ffmpeg -version
💡 Pro Tip: FFmpeg is a powerful multimedia framework that can decode, encode, transcode, mux, demux, stream, filter and play almost anything.
2

Understanding Compression Concepts

Concept Description Impact
Bitrate Amount of data processed per second Lower = Smaller file, Lower quality
Codec Compression algorithm (H.264, H.265) H.265 = 30% smaller than H.264
Resolution Video dimensions (1920x1080, etc.) Lower = Much smaller file
CRF Constant Rate Factor (quality setting) 18-28 is good range (lower=better)
3

Method 1: Two-Pass Encoding (Recommended)

Two-pass encoding analyzes the video twice to achieve the exact target file size with optimal quality distribution.

Step 1: Get Video Duration

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 your_video.mp4

Step 2: Calculate Bitrate

For a 300MB target with 128kbps audio:

# Formula: (Target_MB × 8192 - Audio_kbps × Duration) / Duration # Example for 600 second video: # (300 × 8192 - 128 × 600) / 600 = ~3900 kbps

Step 3: First Pass (Analysis)

ffmpeg -y -i input.mp4 -c:v libx264 -b:v 3900k -pass 1 -an -f null /dev/null

Step 4: Second Pass (Encoding)

ffmpeg -i input.mp4 -c:v libx264 -b:v 3900k -pass 2 -c:a aac -b:a 128k output.mp4
⚠️ Note: Two-pass encoding takes longer but produces better quality at the target file size.
4

Method 2: CRF Encoding (Quick & Easy)

Constant Rate Factor is faster and easier, focusing on quality rather than exact file size.

ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset slow -c:a aac -b:a 128k output.mp4

CRF Quality Guide:

  • CRF 18-23: High quality, larger file (~500-800MB)
  • CRF 24-28: Good quality, medium file (~300-500MB) ← Recommended
  • CRF 29-32: Lower quality, small file (~200-300MB)
💡 Pro Tip: Test with -t 15 to encode only 15 seconds first and check quality before running the full video.
5

Automated Compression Script

Create a reusable script that automatically calculates the perfect bitrate for 300MB output.

Create the Script:

nano compress_to_300mb.sh

Paste This Code:

#!/bin/bash INPUT="$1" OUTPUT="${2:-compressed_$(basename "$INPUT")}" TARGET_MB=300 AUDIO_BITRATE=128 if [ -z "$INPUT" ] || [ ! -f "$INPUT" ]; then echo "Usage: $0 <input_video> [output_name.mp4]" exit 1 fi # Get duration in seconds DURATION=$(ffprobe -v error -show_entries format=duration \ -of default=noprint_wrappers=1:nokey=1 "$INPUT") # Calculate video bitrate VIDEO_BITRATE=$(awk "BEGIN {printf \"%d\", ($TARGET_MB * 8192 - $AUDIO_BITRATE * $DURATION) / $DURATION}") echo "📏 Duration: ${DURATION}s | 🎯 Bitrate: ${VIDEO_BITRATE}k" # Two-pass encoding ffmpeg -y -i "$INPUT" -c:v libx264 -b:v "${VIDEO_BITRATE}k" \ -pass 1 -an -f null /dev/null ffmpeg -i "$INPUT" -c:v libx264 -b:v "${VIDEO_BITRATE}k" \ -pass 2 -c:a aac -b:a "${AUDIO_BITRATE}k" "$OUTPUT" # Clean up rm -f ffmpeg2pass-0.log ffmpeg2pass-0.log.mbtree echo "✅ Done! Saved as: $OUTPUT"

Make Executable & Run:

chmod +x compress_to_300mb.sh ./compress_to_300mb.sh your_camera_video.mp4
6

Advanced Techniques

Downscale Resolution

Reduce from 4K to 1080p for massive size reduction:

ffmpeg -i input.mp4 -vf "scale=1920:1080" \ -c:v libx264 -crf 26 -c:a aac -b:a 128k output.mp4

Use H.265 for Smaller Files

30% smaller than H.264 at same quality:

ffmpeg -i input.mp4 -c:v libx265 -crf 28 \ -preset slow -tag:v hvc1 -c:a aac -b:a 128k output.mp4

Change Frame Rate

Reduce from 60fps to 30fps:

ffmpeg -i input.mp4 -r 30 -c:v libx264 \ -crf 26 -c:a aac output.mp4
⚠️ Compatibility Note: H.265 (HEVC) produces smaller files but may not play on all devices. H.264 is universally compatible.
7

Verification & Quality Check

Check File Size:

ls -lh output.mp4 du -h output.mp4

Check Video Properties:

ffprobe -v error -show_entries format=size,duration \ -of csv=p=0 output.mp4

Detailed Information:

ffprobe -i output.mp4
✅ Success Criteria: File should be ~300MB and playable with good quality.
8

Practice Exercise

📝 Quick Quiz

1. Which method produces the best quality at an exact file size?

2. What CRF value would you use for good quality at ~300MB?

3. Which codec produces smaller files?

🎯 Hands-On Challenge:

  1. Compress a test video to exactly 300MB using two-pass encoding
  2. Create your own custom script with different target sizes (500MB, 200MB)
  3. Compare quality between CRF 24, 26, and 28
  4. Test H.265 vs H.264 compression ratios
9

Troubleshooting Guide

Problem Solution
FFmpeg not found sudo apt install ffmpeg
Output file too large Increase CRF value or decrease bitrate
Quality too poor Decrease CRF (18-23) or increase bitrate
Encoding very slow Use -preset faster instead of slow
No audio in output Add -c:a aac -b:a 128k to command

Course Completion Checklist

  • ✓ Installed FFmpeg on Ubuntu 24.04
  • ✓ Understood compression concepts (bitrate, codec, CRF)
  • ✓ Successfully used two-pass encoding
  • ✓ Created automated compression script
  • ✓ Compressed 1.6GB video to ~300MB
  • ✓ Verified output quality and file size
  • ✓ Uploaded to Google Drive successfully

🎓 Congratulations!

You've completed the Video Compression Masterclass