Last active
July 13, 2022 20:49
-
-
Save Randomblock1/3c5c449608840939180baaf604f3f456 to your computer and use it in GitHub Desktop.
Mass convert videos from one type to another
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
# By default, this will convert all MP4 files in the specified directory to AV1-encoded MKV files. | |
# You can specify parameters to the command, or modify the default behavior below. | |
# Run `Get-Help MassFFmpegConvert.ps1` for more information. | |
param( | |
[Parameter(Mandatory)] | |
[String]$path = ".", | |
[Parameter()] | |
[String]$inputExtension = "mp4", | |
[String]$outputExtension = "mkv", | |
[String]$videoCodec = "libsvtav1", | |
[String]$crf = "30", | |
[String]$audioCodec = "copy", | |
[Switch]$notRecursive, | |
[Switch]$dryRun, | |
[Switch]$deleteOriginal | |
) | |
$recursive = !$notRecursive | |
if (Get-Command ffpb -ErrorAction SilentlyContinue) { $executable = "ffpb" } else { $executable = "ffmpeg" } | |
$filesToConvert = Get-ChildItem $path\*.$inputExtension -Recurse:$recursive | |
$numFilesToConvert = $filesToConvert.Count | |
if ($numFilesToConvert -eq 0) { | |
Write-Host "Searched for $inputExtension files, but couldn't find any!" -ForegroundColor Red | |
exit | |
} | |
if ($inputExtension -eq $outputExtension) { | |
Write-Host "You just tried to overwrite a file with itself. The input and output extensions can't be exactly the same!" -ForegroundColor Red | |
exit | |
} | |
Write-Host "Transcoding $inputExtension to $outputExtension with c:v $videoCodec, CRF $crf, c:a $audioCodec" -ForegroundColor Blue | |
$vidNum = 0 | |
foreach ($inputVid in $filesToConvert) { | |
$outputVid = [io.path]::ChangeExtension($inputVid.FullName, ".$outputExtension") | |
$vidNum += 1 | |
$percentProgress = [double][Math]::Ceiling(($vidNum/$numFilesToConvert)*100) | |
Write-Progress -Activity "Transcoding in progress" -Status "$(Resolve-Path -Relative -LiteralPath $inputVid) ($vidNum/$numFilesToConvert)" -PercentComplete $percentProgress | |
if (!$dryRun) { | |
& $executable -hide_banner -hwaccel auto -i $inputVid.FullName -c:v $videoCodec -c:a $audioCodec -crf $crf -map_metadata 0 $outputVid | |
if ($deleteOriginal) { | |
Remove-Item $inputVid | |
} | |
} | |
} | |
Write-Host "Done! Follow me and leave a star if you liked this script. https://github.com/randomblock1" -ForegroundColor DarkGreen | |
<# | |
.SYNOPSIS | |
Transcode all files in a folder recursively with a given file extension. | |
.DESCRIPTION | |
Transcode all files in a folder recursively with a given file extension. | |
Prints the name of each file it transcodes. | |
Uses ffpb to display a progress bar if possible. | |
.PARAMETER path | |
Required. Specifies the folder path to recursively search for files. | |
.PARAMETER inputExtension | |
Specifies the extension. "mp4" is the default. | |
.PARAMETER outputExtension | |
Specifies the extension. "mkv" is the default. | |
.PARAMETER videoCodec | |
Specifies the video codec. "libsvtav1" is the default. | |
.PARAMETER crf | |
Specifies the constant rate factor. "30" is the default. | |
.PARAMETER audioCodec | |
Specifies the audio codec. "copy" is the default. | |
.PARAMETER notRecursive | |
Specifies if it should scan recursively for files. "false" is the default. | |
.PARAMETER dryRun | |
Specifies if it should perform a dry run. "false" is the default. | |
.PARAMETER deleteOriginal | |
Specifies if it should delete the original file after transcoding. "false" is the default. | |
.INPUTS | |
None. | |
.OUTPUTS | |
None. | |
.EXAMPLE | |
PS> MassFFMpegConvert -path .\Videos | |
Transcoding mp4 to mkv with c:v libsvtav1, CRF 30, c:a copy | |
Transcoding .\Videos\test.mp4 | |
test.mp4: 100%|#############| 600/600 [00:05<00:00, 120.0 frames/s] | |
Done! ✅ | |
.EXAMPLE | |
PS> MassFFMpegConvert -path .\Videos -inputExtension webm -outputExtension mp4 -videoCodec libx264 -crf 20 -audioCodec aac | |
Transcoding webm to mp4 with c:v libx264, CRF 20, c:a aac | |
Transcoding .\Videos\test.webm | |
test.webm: 100%|#############| 600/600 [00:05<00:00, 120.0 frames/s] | |
Done! ✅ | |
.EXAMPLE | |
PS> MassFFMpegConvert -path .\Videos -dryRun | |
Transcoding mp4 to mkv with c:v libsvtav1, CRF 30, c:a copy | |
Transcoding .\Videos\test.mp4 | |
Done! ✅ | |
# Notice how nothing is actually transcoded. It's just a dry run. | |
.LINK | |
https://github.com/randomblock1 | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment