Skip to content

kamranrouhani/Google-Photos---Video-Compressor-Script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Google Photos Video Compressor

A powerful Python script to compress videos exported from Google Takeout while preserving metadata. Reduces video file sizes by up to 90% using modern H.265 encoding, with intelligent size-based compression strategies.

🎯 Features

  • Size-based compression tiers:

    • Small files (< 15MB): Copied without compression
    • Medium files (15MB - 3GB): Normal compression (1080p, H.265)
    • Large files (> 3GB): Aggressive compression (720p, H.265)
  • Metadata preservation:

    • Reads Google Takeout JSON metadata files
    • Preserves creation dates and GPS coordinates
    • Falls back to native video metadata for non-Takeout videos
  • Incremental processing:

    • Skips already-processed files
    • Perfect for adding new videos to existing collections
  • Comprehensive logging:

    • CSV log file with detailed processing information
    • Summary statistics with space savings and compression ratios
    • Individual file processing times
  • Interactive configuration:

    • Prompts for settings with sensible defaults
    • No need to edit code for basic usage

πŸ“‹ Requirements

  • Operating System: Linux (tested on Ubuntu 22.04/Pop!_OS)
  • Python: 3.6 or higher
  • Dependencies:
    • ffmpeg - Video encoding
    • exiftool (libimage-exiftool-perl) - Metadata manipulation

πŸš€ Installation

1. Install dependencies

Ubuntu/Debian/Pop!_OS:

sudo apt update
sudo apt install ffmpeg libimage-exiftool-perl python3 -y

Fedora:

sudo dnf install ffmpeg perl-Image-ExifTool python3 -y

Arch Linux:

sudo pacman -S ffmpeg perl-image-exiftool python

2. Download the script

# Clone the repository
git clone https://github.com/yourusername/google-photos-compressor.git
cd google-photos-compressor

# Make script executable
chmod +x compress_videos.py

πŸ“– Usage

Basic Usage

Simply run the script and follow the prompts:

python3 compress_videos.py

You'll be asked for:

  • Input directory: Where your videos are located
  • Output directory: Where compressed videos will be saved
  • Small file threshold: Files under this size (MB) will be copied without compression
  • Large file threshold: Files over this size (GB) get aggressive compression
  • Skip existing: Whether to skip already-processed files

Example Session

======================================================================
VIDEO COMPRESSION CONFIGURATION
======================================================================

Press Enter to use default values shown in [brackets]

Input directory [/path/to/your/videos]: /home/user/google_takeout/Videos
Output directory [/path/to/compressed_videos]: /home/user/compressed_videos
Small file threshold in MB (files under this will be copied) [15]: 
Large file threshold in GB (files over this get aggressive compression) [3]: 
Skip files that already exist in output? (y/n) [y]: 

======================================================================
CONFIGURATION SUMMARY
======================================================================
Input directory:       /home/user/google_takeout/Videos
Output directory:      /home/user/compressed_videos
Small file threshold:  15 MB
Large file threshold:  3 GB
Skip existing files:   True
======================================================================

Proceed with these settings? (y/n): y

Advanced Usage: Edit Defaults

If you want to skip the prompts, edit the default values at the top of the script:

DEFAULT_INPUT_DIR = "/media/mydrive/google_takeout/Videos"
DEFAULT_OUTPUT_DIR = "/media/mydrive/compressed_videos"
DEFAULT_SMALL_FILE_THRESHOLD_MB = 15
DEFAULT_LARGE_FILE_THRESHOLD_GB = 3
DEFAULT_SKIP_EXISTING = True

Then just press Enter through all prompts to use these defaults.

🎬 Use Cases

1. Google Takeout Export

Scenario: You exported 89GB of videos from Google Photos and want to re-upload smaller versions.

# 1. Export from Google Takeout (you already did this)
# 2. Extract videos to a folder
# 3. Run the script
python3 compress_videos.py

Result: 89GB β†’ 8-9GB (~90% reduction) while preserving all dates and GPS data.

2. Adding New Phone Videos

Scenario: You have new videos on your phone that aren't in Google Photos yet.

# 1. Copy new videos to your original input folder
# 2. Run the script with SKIP_EXISTING=True
python3 compress_videos.py

The script will only process new videos, skipping already-compressed ones.

3. Different Compression Levels

Scenario: You want to preserve more quality for important videos.

Edit thresholds:

  • Increase SMALL_FILE_THRESHOLD_MB to copy more files without compression
  • Decrease LARGE_FILE_THRESHOLD_GB to apply aggressive compression to fewer files

πŸ“Š Output

Compressed Videos

All videos are saved to your output directory with the same filenames as the originals.

Log File

A CSV log file is created in the output directory: compression_log_YYYYMMDD_HHMMSS.csv

Columns:

  • Filename
  • Action (COPY, COMPRESS_NORMAL, COMPRESS_AGGRESSIVE, SKIPPED)
  • Input/Output sizes (bytes and human-readable)
  • Reduction percentage
  • Processing duration
  • Status (SUCCESS, FAILED, SKIPPED)
  • Error message (if any)

Example:

Filename,Action,Input Size (bytes),Output Size (bytes),Input Size,Output Size,Reduction %,Duration (sec),Status,Error Message
VID_20220702_201431.mp4,COMPRESS_NORMAL,156234567,18234567,148.99 MB,17.39 MB,88.32,45.23,SUCCESS,
VID_20230104_163239.mp4,COPY,8234567,8234567,7.85 MB,7.85 MB,0.00,0.12,SUCCESS,

Summary Statistics

Appended to the log file:

SUMMARY
====================================================================================================
Total Files Found: 847
Skipped (already processed): 0
Processed: 847
Successful: 845
  - Copied (< threshold): 23
  - Normal Compression: 789
  - Aggressive Compression: 33
Failed: 2

Total Input Size: 89.23 GB (95,812,345,678 bytes)
Total Output Size: 8.91 GB (9,567,234,567 bytes)
Total Space Saved: 80.32 GB
Overall Reduction: 90.01%

Total Processing Time: 5:43:12
Average Time per File: 24.35 seconds

πŸ”§ How It Works

Compression Strategy

  1. Small files (< 15MB): Direct copy

    • Already small, compression overhead not worth it
    • Preserves 100% quality
  2. Normal files (15MB - 3GB): H.265, 1080p, CRF 28

    • Downscales 4K to 1080p
    • Uses H.265 (HEVC) codec for modern compression
    • CRF 28 = high quality, good compression
  3. Large files (> 3GB): H.265, 720p, CRF 32

    • Downscales to 720p for maximum compression
    • CRF 32 = slightly lower quality, maximum compression
    • Best for very long 4K videos

Metadata Handling

Google Takeout videos (with JSON):

  • Reads .supplemental-metadata.json files
  • Extracts photoTakenTime β†’ sets video creation date
  • Extracts geoData β†’ sets GPS coordinates
  • Writes metadata using exiftool

Regular videos (phone recordings):

  • FFmpeg's -map_metadata 0 copies all existing metadata
  • Preserves creation dates, GPS if present in original

❓ FAQ

Q: Will this work on Windows/Mac?
A: The script is designed for Linux. For Windows, you'd need WSL (Windows Subsystem for Linux). For Mac, it should work with minor modifications (install ffmpeg/exiftool via Homebrew).

Q: How long does it take?
A: Approximately 5-6 hours for 89GB on a modern laptop. H.265 encoding is CPU-intensive.

Q: Can I pause and resume?
A: Yes! Enable SKIP_EXISTING=True and re-run the script. It will skip already-processed videos.

Q: What if I want different quality settings?
A: Edit the convert_video() function and adjust:

  • crf value (lower = better quality, larger files)
  • scale resolution (e.g., scale=-2:480 for even smaller files)
  • preset (slower presets = better compression)

Q: Do I lose metadata?
A: No! The script preserves:

  • Creation dates
  • GPS coordinates
  • All other metadata from source videos

Q: What video formats are supported?
A: Input: .mp4, .mov, .3gp
Output: .mp4 (H.265)

πŸ› Troubleshooting

"ERROR: ffmpeg not found"

sudo apt install ffmpeg -y

"ERROR: exiftool not found"

sudo apt install libimage-exiftool-perl -y

Videos have wrong dates after compression

  • Check if JSON files exist alongside videos
  • Verify JSON files contain photoTakenTime field
  • For phone videos without JSON, dates come from file metadata

Script is slow

  • H.265 encoding is CPU-intensive, this is normal
  • Consider adjusting preset to fast or ultrafast (larger files but faster)
  • Use aggressive compression tier more liberally

Out of disk space

  • Ensure output directory has enough space
  • Process videos in batches
  • Delete originals only after verifying compressed versions

πŸ“ License

MIT License - Feel free to modify and distribute!

🀝 Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

πŸ’‘ Credits

Created to solve the "Google Photos storage full" problem - went from 89GB to 9GB and saved €75/year in storage costs!

⚠️ Disclaimer

  • Always backup your original videos before compression
  • Test on a few videos first to ensure quality is acceptable
  • Compression is lossy - you cannot recover original quality
  • Results may vary based on source video quality and content type

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages