Skip to content

Instantly share code, notes, and snippets.

@carneloot
Last active December 30, 2025 20:07
Show Gist options
  • Select an option

  • Save carneloot/b151b9b1c364757cc2009a968f34dd4c to your computer and use it in GitHub Desktop.

Select an option

Save carneloot/b151b9b1c364757cc2009a968f34dd4c to your computer and use it in GitHub Desktop.
A simple powershell script to compress video
# Requires Windows PowerShell 5.1 or PowerShell Core (pwsh)
param(
[Parameter(Mandatory=$true)]
[Alias("i", "in")] # Aliases for InputFile
[string]$InputFile,
[ValidateSet('default', 'whatsapp', 'gpu-hevc', 'gpu-h264')]
[Alias("p")] # Alias for Preset
[string]$Preset = 'default' # Default compression preset
)
# --- Configuration for FFmpeg ---
$ffmpegPath = "ffmpeg" # Assumes ffmpeg is in your PATH. If not, specify the full path, e.g., "C:\ffmpeg\bin\ffmpeg.exe"
$videoCodec = "libx264"
$crf = 23 # For libx264/libx265
$encoderPreset = "medium" # For libx264/libx265
$audioCodec = "aac"
$audioBitrate = "128k"
$vf = $null # Video filter for scaling
$hwaccel = $null # Hardware acceleration flag
# --- Apply Preset Settings ---
switch ($Preset) {
"whatsapp" {
Write-Host "Applying WhatsApp optimization settings..."
$videoCodec = "libx264" # Ensure H.264 for broadest WhatsApp compatibility
$crf = 25 # Slightly higher CRF for smaller file size
$encoderPreset = "medium"
$audioBitrate = "192k" # Adjusted audio bitrate as recommended
$vf = "scale=1280:-1" # Scale to 720p width, maintain aspect ratio
$hwaccel = $null
}
"gpu-hevc" {
Write-Host "Applying GPU accelerated HEVC settings..."
$videoCodec = "hevc_nvenc"
$hwaccel = "-hwaccel cuda" # Assuming NVIDIA GPU, change to "auto" if unsure
$encoderPreset = "p5" # For nvenc presets (p1-p7)
$crf = $null # Explicitly nullify CRF for nvenc where it's not applicable
}
"gpu-h264" {
Write-Host "Applying GPU accelerated H264 settings..."
$videoCodec = "h264_nvenc"
$hwaccel = "-hwaccel cuda" # Assuming NVIDIA GPU
$encoderPreset = "p5" # For nvenc presets (p1-p7)
$crf = $null # Explicitly nullify CRF for nvenc where it's not applicable
}
"default" {
Write-Host "Applying default compression settings..."
# Settings are already initialized to default values above.
}
}
# --- Construct FFmpeg Command ---
$outputFileNameBase = [System.IO.Path]::GetFileNameWithoutExtension($InputFile)
$outputFileDirectory = [System.IO.Path]::GetDirectoryName($InputFile)
$outputFileNameSuffix = "_compressed"
if ($Preset -ne 'default') {
$outputFileNameSuffix = "_$($Preset)"
}
$outputFile = Join-Path $outputFileDirectory "$($outputFileNameBase)$($outputFileNameSuffix).mp4"
# Build argument list
$ffmpegArgs = @()
if ($hwaccel) { $ffmpegArgs += $hwaccel.Split(" ") }
$ffmpegArgs += "-i", "`"$InputFile`""
if ($vf) { $ffmpegArgs += "-vf", "`"$vf`"" }
$ffmpegArgs += "-c:v", $videoCodec
if ($crf -ne $null) { # Only add CRF if it's applicable (not null)
$ffmpegArgs += "-crf", $crf
}
# Encoder preset handling: -preset for libx26x, -preset:v for nvenc
if ($videoCodec -match "nvenc") {
$ffmpegArgs += "-preset:v", $encoderPreset
} else {
$ffmpegArgs += "-preset", $encoderPreset
}
$ffmpegArgs += "-c:a", $audioCodec
$ffmpegArgs += "-b:a", $audioBitrate
$ffmpegArgs += "-movflags", "+faststart"
$ffmpegArgs += "`"$outputFile`""
Write-Host "Input File: $($InputFile)"
Write-Host "Output File: $($outputFile)"
Write-Host "FFmpeg Command: $($ffmpegPath) $($ffmpegArgs -join ' ')"
# --- Execute FFmpeg ---
try {
& $ffmpegPath $ffmpegArgs
Write-Host "`nCompression complete! Output saved to: $($outputFile)"
} catch {
Write-Error "FFmpeg command failed: $($_.Exception.Message)"
exit 1
}

Video Compression Script Usage

This script compress.ps1 uses FFmpeg to compress video files with various presets, including optimized settings for WhatsApp and GPU-accelerated encoding.

Requirements

  • 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-hevc and gpu-h264 presets, you'll need an NVIDIA GPU with the appropriate drivers and FFmpeg compiled with NVENC support.

How to Use

  1. Save the script: Save the provided PowerShell code as compress.ps1 in a convenient location.
  2. Open PowerShell: Navigate to the directory where you saved compress.ps1.
  3. Execution Policy (if needed): If you encounter errors running scripts, you might need to adjust your PowerShell execution policy. Run this command once:
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    (Type Y and press Enter when prompted).

Basic Syntax

.\compress.ps1 -InputFile <path_to_video> -Preset <preset_type>

Parameter Aliases

You can use aliases for shorter commands:

  • -InputFile can be -i or -in
  • -Preset can be -p

Examples

Default Compression

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

WhatsApp Optimized Compression

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 whatsapp

Output file: my_original_video_whatsapp.mp4

GPU Accelerated HEVC (H.265) Compression

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), p5 preset (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-hevc

Output file: my_original_video_gpu-hevc.mp4

GPU Accelerated H.264 Compression

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), p5 preset.
  • 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-h264

Output file: my_original_video_gpu-h264.mp4

Understanding the Presets

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment