This script compress.ps1 uses FFmpeg to compress video files with various presets, including optimized settings for WhatsApp and GPU-accelerated encoding.
- FFmpeg: You need FFmpeg installed and accessible in your system's PATH. You can download it from ffmpeg.org.
- PowerShell: The script requires Windows PowerShell 5.1 or PowerShell Core (
pwsh). - NVIDIA GPU (Optional): For
gpu-hevcandgpu-h264presets, you'll need an NVIDIA GPU with the appropriate drivers and FFmpeg compiled with NVENC support.
- Save the script: Save the provided PowerShell code as
compress.ps1in a convenient location. - Open PowerShell: Navigate to the directory where you saved
compress.ps1. - Execution Policy (if needed): If you encounter errors running scripts, you might need to adjust your PowerShell execution policy. Run this command once:
(Type
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Yand press Enter when prompted).
.\compress.ps1 -InputFile <path_to_video> -Preset <preset_type>You can use aliases for shorter commands:
-InputFilecan be-ior-in-Presetcan be-p
This uses H.264 (libx264), a CRF of 23 (good quality), and a 128kbps AAC audio.
.\compress.ps1 -InputFile "C:\Videos\my_original_video.mp4"
# Using aliases:
.\compress.ps1 -i "C:\Videos\my_original_video.mp4"Output file: my_original_video_compressed.mp4
This preset is tailored for sharing videos on WhatsApp, aiming for a balance of file size and acceptable quality.
- Video: H.264 (
libx264), scaled to 720p width (e.g., 1280x720 or similar aspect ratio), CRF 25. - Audio: AAC, 192kbps.
.\compress.ps1 -InputFile "C:\Videos\my_original_video.mp4" -Preset whatsapp
# Using aliases:
.\compress.ps1 -i "C:\Videos\my_original_video.mp4" -p whatsappOutput file: my_original_video_whatsapp.mp4
Utilizes NVIDIA's NVENC hardware encoder for faster HEVC (H.265) encoding. This generally provides better compression than H.264 for the same quality, but requires an NVIDIA GPU and FFmpeg with NVENC support.
- Video: HEVC (
hevc_nvenc),p5preset (a good balance for speed/quality). - Audio: AAC, 128kbps.
.\compress.ps1 -InputFile "C:\Videos\my_original_video.mp4" -Preset gpu-hevc
# Using aliases:
.\compress.ps1 -i "C:\Videos\my_original_video.mp4" -p gpu-hevcOutput file: my_original_video_gpu-hevc.mp4
Utilizes NVIDIA's NVENC hardware encoder for faster H.264 encoding. This is generally faster than CPU-based H.264 encoding (libx264), but might produce slightly larger files or lower quality at the same bitrate compared to libx264 (depending on settings).
- Video: H.264 (
h264_nvenc),p5preset. - Audio: AAC, 128kbps.
.\compress.ps1 -InputFile "C:\Videos\my_original_video.mp4" -Preset gpu-h264
# Using aliases:
.\compress.ps1 -i "C:\Videos\my_original_video.mp4" -p gpu-h264Output file: my_original_video_gpu-h264.mp4
| Preset | Description | Recommended Use | Key Settings |
|---|---|---|---|
default |
General-purpose video compression using CPU-based H.264. A good balance of quality and file size for most situations. | Everyday video compression where file size reduction is desired without major quality loss. | Video Codec: libx264, CRF: 23, Video Preset: medium, Audio Codec: aac, Audio Bitrate: 128k. |
whatsapp |
Optimized for sharing via WhatsApp. Focuses on reducing file size significantly while maintaining reasonable visual quality for mobile viewing. Downscales to 720p. | Sending videos via WhatsApp or other platforms with strict file size limits. | Video Codec: libx264, CRF: 25, Video Preset: medium, Video Filter: scale=1280:-1 (720p width), Audio Codec: aac, Audio Bitrate: 192k. |
gpu-hevc |
Leverages NVIDIA GPU hardware acceleration for HEVC (H.265) encoding. Significantly faster encoding than CPU-based HEVC, often with better compression efficiency than H.264. | Fast encoding of high-resolution videos, especially for archival or when H.265 compatibility is assured. Requires NVIDIA GPU. | Video Codec: hevc_nvenc, Hardware Accel: cuda, Video Preset: p5 (NVENC preset), Audio Codec: aac, Audio Bitrate: 128k. (CRF is ignored for NVENC; quality controlled by other parameters not directly exposed here). |
gpu-h264 |
Leverages NVIDIA GPU hardware acceleration for H.264 encoding. Faster than CPU-based H.264 encoding, though libx264 (CPU) might offer slightly better quality/compression ratios at very specific settings. |
Fast encoding of H.264 videos, especially when CPU resources are limited or for live streaming applications. Requires NVIDIA GPU. | Video Codec: h264_nvenc, Hardware Accel: cuda, Video Preset: p5 (NVENC preset), Audio Codec: aac, Audio Bitrate: 128k. (CRF is ignored for NVENC; quality controlled by other parameters not directly exposed here). |