Skip to content

Instantly share code, notes, and snippets.

@rytsikau
Last active December 12, 2021 17:18
Show Gist options
  • Save rytsikau/4a1b6f977d9cb9c51cc1020ffa931635 to your computer and use it in GitHub Desktop.
Save rytsikau/4a1b6f977d9cb9c51cc1020ffa931635 to your computer and use it in GitHub Desktop.
Simple video recompress + crop, resize, deinterlace and set aspect ratio
'--------------------------------------------------------------------------------------------------'
' ee.fastvid (v.20211211) Simple video crop, resize, deinterlace and set the aspect ratio '
'--------------------------------------------------------------------------------------------------'
set-executionpolicy unrestricted -scope currentuser
$erroractionpreference = "silentlycontinue"; remove-variable *; $erroractionpreference = "continue"
$ffmpegDir = "p:\!programs\!consoles\ffmpeg"
$outputDir = "c:\!downloads"
$outputFormat = ".mp4"
$audioCompress = "-c:a aac"
$videoCompress = "-c:v h264_qsv -q:v 22"
#---------------------------------------------------------------------------------------------------
$input = $args[0]
write-host "Input file:`n $input"
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
$ffprobeStr = "`"$ffmpegDir\ffprobe.exe`" -v quiet -select_streams v:0" `
+ " -show_entries stream=width,height -of csv=p=0 `"$input`""
$ffprobeRes = cmd /c $ffprobeStr
$width = $ffprobeRes.split(',')[0]
$height = $ffprobeRes.split(',')[1]
write-host "Input video size:`n $width*$height"
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
$output = $outputDir + "\" + [System.IO.Path]::GetFileNameWithoutExtension($input) + "__" `
+ $(get-date).ToString("yyyyMMdd-HHmmss") + $outputFormat
write-host "Output file:`n $output"
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
write-host ""
write-host ""
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
write-host "Set the pixels to crop as 'Left Right Top Bottom' OR press ENTER to skip"
write-host " " -NoNewline
$str = read-host
if ($str -like "")
{
$crop = ""
}
elseif ($str -match [regex]'[0-9]+ [0-9]+ [0-9]+ [0-9]+')
{
$cropLeft = [int]$str.split(' ')[0]
$cropRight = [int]$str.split(' ')[1]
$cropTop = [int]$str.split(' ')[2]
$cropBottom = [int]$str.split(' ')[3]
$croppedWidth = $width - $cropLeft - $cropRight
$croppedHeight = $height - $cropTop - $cropBottom
$crop = "$croppedWidth`:$croppedHeight`:$cropLeft`:$cropTop"
}
else
{
write-host "`nERROR (invalid string)"
exit
}
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
write-host "Set width and height ('480 360' '640 360' '768 -1' etc.) OR press ENTER to skip"
write-host " " -NoNewline
$str = read-host
if ($str -like "")
{
$scale = ""
}
elseif ($str -match [regex]'-?[0-9]+ -?[0-9]+')
{
$scale = $str.replace(" ", ":")
}
else
{
write-host "`nERROR (invalid string)"
exit
}
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
write-host "Set the aspect ratio ('16 9' '4 3' etc.) OR press ENTER to skip"
write-host " " -NoNewline
$str = read-host
if ($str -like "")
{
$setdar = ""
}
elseif ($str -match [regex]'[0-9]+ [0-9]+')
{
$setdar = $str.replace(" ", "/")
}
else
{
write-host "`nERROR (invalid string)"
exit
}
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
$videoFilters = "-vf yadif=parity=auto,"
if ($crop -notlike "") { $videoFilters += "crop=$crop," }
if ($scale -notlike "") { $videoFilters += "scale=$scale," }
if ($setdar -notlike "") { $videoFilters += "setdar=$setdar," }
$videoFilters = $videoFilters.TrimEnd(',') + " "
$ffmpegStr = "`"$ffmpegDir\ffmpeg.exe`" -loglevel quiet -stats -i `"$input`" -map 0 -map -v" + `
" -map V -map -0:d -c copy $audioCompress $videoFilters $videoCompress `"$output`""
#---------------------------------------------------------------------------------------------------
write-host "`n`n$ffmpegStr`n`n"
cmd /c $ffmpegStr
write-host "`n`nFINISHED"
$host.UI.RawUI.WindowTitle = "FINISHED"
'--------------------------------------------------------------------------------------------------'
@echo off
if [%1]==[] goto manual
set FILE=%1
goto start
:manual
echo [input file can also be specified as an argument]
set /p FILE=Enter input video path here:
if "%FILE%"=="" goto finish
:start
powershell "& '%CD%\ee.fastvid.ps1' '%FILE%'"
:finish
pause & exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment