Skip to content

Instantly share code, notes, and snippets.

@KirovAir
Created December 7, 2023 21:18
Show Gist options
  • Save KirovAir/82281b8da86291ea866e88fbced928cc to your computer and use it in GitHub Desktop.
Save KirovAir/82281b8da86291ea866e88fbced928cc to your computer and use it in GitHub Desktop.
A snippet to batch convert
# Set the path to the folder containing the video files
$folderPath = "C:\Users\me\Downloads\Movies"
$cacheDir = "C:\Users\me\Downloads\Cache"
$minSize = 3GB # minimal size to trigger a x265 conversion
# Get a list of video files with "264" in the filename and .mkv or .mp4 extension, including subdirectories
$videoFiles = Get-ChildItem -Path $folderPath -Filter "*264*.m*" -Recurse | Where-Object { $_.Extension -eq '.mkv' -or $_.Extension -eq '.mp4' }
# Display the list of files, including their sizes
Write-Host "List of video files to be converted:"
foreach ($file in $videoFiles) {
Write-Host "$($file.FullName) - Size: $($file.Length / 1GB) GB"
}
# Filter files based on size (exclude files below 5GB)
$videoFiles = $videoFiles | Where-Object { $_.Length -ge $minSize }
# Display the updated list of files after size filtering
Write-Host "`nList of video files to be converted (Files below $($minSize) excluded):"
foreach ($file in $videoFiles) {
Write-Host "$($file.FullName) - Size: $($file.Length / 1GB) GB"
}
# Ask for confirmation to continue
$confirmation = Read-Host "Do you want to continue? (Y/N)"
if ($confirmation -ne 'Y') {
Write-Host "Script aborted."
exit
}
# Convert each file
foreach ($file in $videoFiles) {
$outputFileName = $file.BaseName -replace "264", "265" # Replace "264" with "265" in the filename
$outputFilePath = Join-Path -Path $file.Directory.FullName -ChildPath "$outputFileName.mkv"
$cacheFilePath = Join-Path -Path $cacheDir -ChildPath "$outputFileName.mkv" # Output to cache directory initially
# Check if the output file already exists
if (Test-Path $outputFilePath) {
Write-Host "Output file already exists. Skipping conversion for $($file.FullName)"
}
else {
# Build the ffmpeg command
$ffmpegCommand = "ffmpeg -i `"$($file.FullName)`" -c:v libx265 -crf 22 -preset fast -c:a copy -c:s copy -map 0 `"$cacheFilePath`""
# Execute the ffmpeg command
Invoke-Expression $ffmpegCommand
Write-Host "File converted: $($file.FullName) => $($cacheFilePath)"
}
}
Write-Host "Moving.."
foreach ($file in $videoFiles) {
$outputFileName = $file.BaseName -replace "264", "265" # Replace "264" with "265" in the filename
$cacheFilePath = Join-Path -Path $cacheDir -ChildPath "$outputFileName.mkv" # Output to cache directory initially
Move-Item -Path $cacheFilePath -Destination $file.Directory.FullName -Force
Write-Host "File moved to final output directory: $($cacheFilePath) => $($file.Directory.FullName)"
# Rename .nl.srt file if it exists
$srtFilePath = Join-Path -Path $file.Directory.FullName -ChildPath "$($file.BaseName).nl.srt"
$newSrtFilePath = Join-Path -Path $file.Directory.FullName -ChildPath "$outputFileName.nl.srt"
if (Test-Path $srtFilePath) {
Copy-Item -Path $srtFilePath -Destination $newSrtFilePath
Write-Host "Subtitle file copied: $($srtFilePath) => $($newSrtFilePath)"
}
}
$removeOriginalFilesConfirmation = Read-Host "Do you want to REMOVE the original files? (Y/N)"
if ($removeOriginalFilesConfirmation -eq 'Y') {
foreach ($file in $videoFiles) {
Remove-Item -Path $file.FullName -Force
Write-Host "Original file removed: $($file.FullName)"
# Remove .nl.srt file if it exists
$srtFilePath = Join-Path -Path $file.Directory.FullName -ChildPath "$($file.BaseName).nl.srt"
if (Test-Path $srtFilePath) {
Remove-Item -Path $srtFilePath -Force
Write-Host "Original subtitle file removed: $($srtFilePath)"
}
}
}
Write-Host "Conversion completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment