Last active
December 26, 2022 15:37
-
-
Save Delivator/9ce05c4205f573a4be9be04adc8d39cc to your computer and use it in GitHub Desktop.
Re-encodes all .mp4 files in the same folder as the script to decrease their size
This file contains 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
$location = Get-Location | |
$files = Get-ChildItem $location -Filter "*.mp4" | Sort-Object CreationTime | |
$quality = "36" | |
$counter = 0 | |
$total = $files.Length | |
# Change Write-Progress Style | |
$PSStyle.Progress.MaxWidth = 240 | |
Write-Host "Transcoding all mp4 files in this directory to mkv using NVENC AV1 with CQ $quality" | |
# Create directory .\transcoded if it doesn't exist | |
if (-Not (Test-Path ".\transcoded" -PathType Container)) { | |
New-Item ".\transcoded" -ItemType Directory | Out-Null | |
} | |
# Create directory .\original if it doesn't exist | |
if (-Not (Test-Path ".\original" -PathType Container)) { | |
New-Item ".\original" -ItemType Directory | Out-Null | |
} | |
function Show-Progress { | |
$perc = [Math]::Floor(($counter/$total)*100) | |
Write-Progress -Activity "Transcoding Videos" -Status "$perc% ($counter/$total)" -PercentComplete $perc | |
} | |
foreach ($file in $files) { | |
Show-Progress | |
$arguments = "-y -c:v h264_cuvid -hwaccel_output_format cuda -loglevel error -i `"$file`" -fps_mode passthrough -c:v av1_nvenc -cq $quality -c:a copy -map 0:v -map 0:a" | |
# Write-Host "Transcoding `"$($file.name)`" -> `"transcoded\$($file.BaseName).mkv`"" | |
Start-Process -Wait -NoNewWindow -FilePath "ffmpeg.exe" -WorkingDirectory $location -ArgumentList "$arguments `"transcoded\$($file.BaseName).mkv`"" | |
# Write-Host "Moving `"$file`" -> `"original\$($file.name)`"" | |
Move-Item -Path $file -Destination "original\$($file.name)" | |
$counter++ | |
} | |
Show-Progress | |
$before = (Get-ChildItem .\original\ | Measure-Object Length -Sum).sum / 1Gb | |
$after = (Get-ChildItem .\transcoded\ | Measure-Object Length -Sum).sum / 1Gb | |
$smaller = [math]::Round(((($after / $before)-1)*-100),2) | |
Write-Host "`nDone.`n" | |
Write-Host "Before:"("{0:N2} GB" -f ($before)) | |
Write-Host "After:"("{0:N2} GB ($smaller% Smaller)" -f ($after)) |
V3 now using AV1 with Nvidia NVENC Encoding
Quality target is set to 36 tuned for 1440p 60fps videos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
V2 now using HEVC!