Skip to content

Instantly share code, notes, and snippets.

@jcuffe
Created August 7, 2023 15:44
Show Gist options
  • Save jcuffe/901ededfaef1a0b013c56eee06f74f53 to your computer and use it in GitHub Desktop.
Save jcuffe/901ededfaef1a0b013c56eee06f74f53 to your computer and use it in GitHub Desktop.
Run FFMPEG against an input file with every combination of options contained in a config file
crf 25 26 27
qcomp 0.80 0.85 0.90
subme 2 3
bframes 5
me umh
deblock 6
threads 8
b-adapt 0
aq-mode 0
scenecut 0
rc-lookahead 0
no-mbtree 1
pbratio 1.5
analyse i8x8,i4x4
deadzone-inter 32
deadzone-intra 16 24 32
vbv-maxrate 8000
vbv-bufsize 240000
param(
[string]$inputFilePath,
[string]$configFilePath
)
if (-not $inputFilePath) {
Write-Host "Please provide a valid input file path"
exit
}
if (-not (Test-Path $inputFilePath)) {
Write-Host "Input file not found: $inputFilePath"
exit
}
if (-not $configFilePath) {
Write-Host "Please provide a valid config file path"
exit
}
if (-not (Test-Path $configFilePath)) {
Write-Host "Config file not found: $configFilePath"
exit
}
function Generate-Permutations {
param(
[hashtable]$flags,
[string[]]$flagNames,
[int]$currentIndex = 0,
[Array]$currentPermutation = @(),
[string]$currentIdentifier = ''
)
if ($currentIndex -eq $flagNames.Length) {
return ,@{ 'Permutation' = $currentPermutation; 'Identifier' = $currentIdentifier }
}
$currentFlagName = $flagNames[$currentIndex]
$currentFlagValues = $flags[$currentFlagName]
$permutations = @()
foreach ($item in $currentFlagValues) {
$newIdentifier = $currentIdentifier
if ($currentFlagValues.Length -gt 1) {
$sanitizedItem = $item -replace '[^\w\-]+', ''
$newIdentifier = @(
$currentIdentifier,
"$currentFlagName$sanitizedItem"
) -ne '' -join '_'
}
$generateOptions = @{
flags = $flags
flagNames = $flagNames
currentIndex = ($currentIndex + 1)
currentPermutation = ($currentPermutation + @("$currentFlagName=$item"))
currentIdentifier = $newIdentifier
}
$permutations += @(Generate-Permutations @generateOptions)
}
return $permutations
}
function Parse-ConfigFile {
param(
[string]$filePath
)
$config = @{}
$lines = Get-Content -Path $filePath
foreach ($line in $lines) {
$parts = $line -split ' '
$flagName = $parts[0]
$flagValues = $parts[1..($parts.Length - 1)]
$config[$flagName] = $flagValues
}
return $config
}
$flagsConfig = Parse-ConfigFile -filePath $configFilePath
$flagNames = $flagsConfig.Keys | Sort-Object
$res = Generate-Permutations -flags $flagsConfig -flagNames $flagNames
foreach ($perm in $res) {
$filename = $perm.Identifier
$output = "C:\Users\jcuff\Videos\Halo\bicubic\$filename.mp4"
$x264Params = $perm.Permutation -join ':'
$processOptions = @{
FilePath = "C:\Users\jcuff\ffmpeg.exe"
ArgumentList = @(
"-i `"$inputFilePath`"",
"-ss 00:05:00",
"-t 60",
"-c:a copy",
"-c:v libx264",
"-profile:v high",
"-preset veryfast",
"-x264-params `"$x264Params`"",
"-f mp4",
"-y",
"`"$output`""
)
RedirectStandardOutput = "output.txt"
RedirectStandardError = "error.txt"
Wait = $true
NoNewWindow = $true
}
Write-Host Creating file `"$($perm.Identifier).mp4`"
Write-Host x264 options for OBS: $($perm.Permutation -join ' ')
Start-Process @processOptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment