Created
May 30, 2025 07:54
-
-
Save pleabargain/d83e9fe3b7ed94334ff58c9a5e132ebf to your computer and use it in GitHub Desktop.
powershell check for ffmpeg then list all vid files in directory then prompt user to compress video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check if FFmpeg is installed | |
$ffmpegInstalled = Get-Command ffmpeg -ErrorAction SilentlyContinue | |
if (-not $ffmpegInstalled) { | |
Write-Host "FFmpeg is not installed or not found in PATH. Please install it before running this script." | |
exit | |
} | |
# Get all video files in the current directory | |
$videoFiles = Get-ChildItem -Filter *.mp4 | |
# Check if video files exist | |
if ($videoFiles.Count -eq 0) { | |
Write-Host "No video files found in the current directory." | |
exit | |
} | |
# Display video files to user | |
Write-Host "Select a video file to compress:" | |
for ($i = 0; $i -lt $videoFiles.Count; $i++) { | |
Write-Host "$($i+1). $($videoFiles[$i].Name)" | |
} | |
# Get user selection | |
$selection = Read-Host "Enter the number of the file" | |
$selectedFile = $videoFiles[$selection - 1].FullName | |
$originalFileName = $videoFiles[$selection - 1].Name | |
# Prompt for compression percentage | |
$percentages = @("10", "25", "50") | |
Write-Host "Select a compression percentage:" | |
for ($i = 0; $i -lt $percentages.Count; $i++) { | |
Write-Host "$($i+1). $($percentages[$i])%" | |
} | |
$compressionChoice = Read-Host "Enter the number of the percentage" | |
$compressionPercentage = [int]$percentages[$compressionChoice - 1] | |
# Define output file with prepended compression percentage | |
# Replace spaces with underscores in filename | |
$cleanFileName = $originalFileName -replace ' ', '_' | |
$outputFile = "$compressionPercentage`_$cleanFileName" | |
# Run FFmpeg compression command | |
$compressionFactor = (100 - $compressionPercentage) / 100.0 | |
$ffmpegCommand = "ffmpeg -i `"$selectedFile`" -vcodec libx264 -crf $(30 * $compressionFactor) `"$outputFile`"" | |
Invoke-Expression $ffmpegCommand | |
Write-Host "Compression completed! Output file saved as $outputFile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment