Last active
January 23, 2022 02:28
-
-
Save yutsuku/52773cbcca53a8f67b5bce371ae98a42 to your computer and use it in GitHub Desktop.
Removes black bars from the video using ffmpeg
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
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$i, | |
[Parameter(Mandatory=$false)] | |
[switch]$preview | |
) | |
if (!(Test-Path $i)) { | |
throw 'Input file does not exist: ' + $i | |
} | |
if (!(Test-Path $i -PathType Leaf)) { | |
throw 'Input path is not a file: ' + $i | |
} | |
$ffmpegCommand = 'ffmpeg' | |
$ffplayCommand = 'ffplay' | |
$filename = Split-Path $i -leaf | |
$filepath = Split-Path $i | |
if ($filepath.Length -eq 0) { | |
$filepath = Get-Location | |
} | |
# ffmpeg is ass with powershell | |
$process = New-Object System.Diagnostics.Process | |
$process.StartInfo.FileName = $ffmpegCommand | |
$process.StartInfo.Arguments = "-hide_banner -nostats -ss 1 -i ""$i"" -vframes 10 -vf cropdetect -f null -" | |
$process.StartInfo.UseShellExecute = $false | |
$process.StartInfo.RedirectStandardError = $true | |
$process.StartInfo.WorkingDirectory = Get-Location | |
Write-Host ($process.StartInfo.FileName + ' ' + $process.StartInfo.Arguments) | |
$process.Start() > $null | |
$output = $process.StandardError.ReadToEnd() | |
$process.WaitForExit() | |
if ($output.Trim()) { | |
# search for pattern in multiline string | |
$output = $output -split "`n" | Select-String -Pattern 'Parsed_cropdetect_' -SimpleMatch | |
# return the right-most element | |
$output = ($output -split " ")[-1] | |
Write-Host $output | |
if ($preview) { | |
& $ffplayCommand -vf $output """$i""" | |
Exit | |
} | |
$newFilename = "crop_$filename" | |
$outPath = Join-Path $filepath $newFilename | |
$process = New-Object System.Diagnostics.Process | |
$process.StartInfo.FileName = $ffmpegCommand | |
$process.StartInfo.Arguments = "-hide_banner -nostats -i ""$i"" -vf $output -acodec copy ""$outPath""" | |
$process.StartInfo.UseShellExecute = $false | |
$process.StartInfo.RedirectStandardError = $true | |
$process.StartInfo.WorkingDirectory = Get-Location | |
Write-Host ($process.StartInfo.FileName + ' ' + $process.StartInfo.Arguments) | |
$process.Start() > $null | |
$output = $process.StandardError.ReadToEnd() | |
$process.WaitForExit() | |
if ($output.Trim()) { | |
Write-Host $output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment