Skip to content

Instantly share code, notes, and snippets.

@daBONDi
Created May 29, 2021 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daBONDi/7b251d13c44dfd4bd752b799ea77064f to your computer and use it in GitHub Desktop.
Save daBONDi/7b251d13c44dfd4bd752b799ea77064f to your computer and use it in GitHub Desktop.
Awesome Powershell FFMPEG Multithreading Converting Shit Script use it bam pitch
# have fun with this shit code
# Somebody already thit this better
$ffmpeg_executable_path = ".\ffmpeg-4.4-full_build\ffmpeg-4.4-full_build\bin\ffmpeg.exe"
$maximum_parallel_executions = 5
$input_folder = @(
".\input1",
".\input2"
)
$output_folder = ".\output"
$output_file_ext = "mov"
$ProcessQueueFileList = New-Object System.Collections.ArrayList
#Test Executeable
if (-not (Test-Path $ffmpeg_executable_path)) {
Write-Error -Message "Could not locate ffmpeg binary"
}
#Test Outputfolder
if ( -not (Test-Path $output_folder)) {
Write-Error -Message "Please define Output Folder"
}
function GenerateFileList() {
# Process Input Folder
foreach ($input in $input_folder) {
if (-not (Test-Path -Path $input)) {
Write-Error -Message "Could not found input Folder $input"
}
else {
$files = Get-ChildItem -Path $input -File:$true
foreach($file in $files)
{
$file.FullName
}
}
}
}
$ProcessQueueFileList = GenerateFileList
$ProcessingQueue = New-Object System.Collections.ArrayList
foreach($ProcessFilePath in $ProcessQueueFileList){
#Determinate OutputFilename
$OutputFileName = [io.path]::GetFileNameWithoutExtension( $ProcessFilePath )
$OutputFullName = Join-Path $output_folder $($OutputFileName + "." + $output_file_ext)
# Edge Case same Filename in the outputfolder
if(Test-Path -Path $OutputFullName){
$file_name_ok=$false
$i = 1
while(-not $file_name_ok){
$OutputFullName = Join-Path $output_folder $($OutputFileName + "_" + $i + "." + $output_file_ext)
if(Test-Path $OutputFullName){
$file_name_ok = $false
$i++
}else{
$file_name_ok = $true
}
}
}
$procinfo = New-Object System.Diagnostics.ProcessStartInfo
$procinfo.Filename = $ffmpeg_executable_path
#$procinfo.RedirectStandardInput = $true
#$procinfo.RedirectStandardOutput = $true
$procinfo.Arguments = @(
'-i', """$ProcessFilePath"""
'-c:v prores',
"""$OutputFullName"""
)
#$procinfo.UseShellExecute = $false
$procinfo.WindowStyle = "Minimized"
$proc = New-Object System.Diagnostics.Process
$proc.StartInfo = $procinfo
$proc.Start() | Out-Null
$ProcessingQueue.Add($proc) | Out-Null
$AddJob=$false
while(-not $AddJob){
if((Measure-Object($ProcessingQueue | ? HasExited -eq $false)).Count -lt $maximum_parallel_executions){
$AddJob = $true
}else{
Start-Sleep 2
}
}
}
$finished = $false
while(-not $finished){
$found = $false
foreach($p in $ProcessingQueue){
if($p.HasExited -eq $false){
$found = $true
}
}
if($found){
$finished = $false
}else{
$finished = $true
}
Write-Host "Waiting for Process Finishing"
Start-Sleep -Seconds 1
}
Write-Host "-------------------------"
Write-Host "File Errors from FFMPEG"
Write-Host "-------------------------"
foreach($p in $ProcessingQueue){
if($p.ExitCode -ne 0){
Write-Host "$([string]::Concat($p.StartInfo.Arguments)) :: $($p.ExitCode)"
Write-Host "-----"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment