Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prodehghan/caa69133ec4ac385d52ed6a6cf1ccc8e to your computer and use it in GitHub Desktop.
Save prodehghan/caa69133ec4ac385d52ed6a6cf1ccc8e to your computer and use it in GitHub Desktop.
Converts/compresses and de-shakes all video files (.mp4 and .mov) in current directory to x265
[CmdletBinding()]
param(
[Alias("i")]$inputFiles,
[Alias("op")]$outputPath
)
if ($null -eq $inputFiles) {
$inputFiles = ("*.mp4", "*.mov")
}
if ($null -eq $outputPath) {
Write-Host -ForegroundColor Cyan "Output path not provided. Defaults to '.\c'"
$outputPath = ".\c"
}
if (!(Test-Path -Path $outputPath -PathType Container)) {
mkdir $outputPath
}
$files = Get-ChildItem $inputFiles
if (!$files.GetType().IsArray) {
$files = @( $files )
}
$index = 0
$total = $files.Length
$stars = "************************************"
foreach ($file in $files) {
$index++
Write-Host "$stars ($index/$total) $($file.Name) $stars" -ForegroundColor Cyan
$vidStabResult = "trf-" + $file.BaseName + ".trf"
$newVidStabResult = "x" + $vidStabResult
if (!(Test-Path -Path $newVidStabResult)) {
Write-Host "VidStab: Analysing..." -ForegroundColor Cyan
ffmpeg -hide_banner -i $file -vf vidstabdetect=result="$vidStabResult" -f null -
Rename-Item $vidStabResult $newVidStabResult
}
else {
Write-Host "Using existing VidStab result file" -ForegroundColor Cyan
}
Write-Host "FFmpeg: Converting..." -ForegroundColor Cyan
ffmpeg -hide_banner -i $file -map_metadata 0 -movflags use_metadata_tags -pix_fmt yuv420p `
-vf vidstabtransform=input="$newVidStabResult":smoothing=8,unsharp=5:5:0.8:3:3:0.4 `
-c:v libx265 -crf 20 -preset slower -c:a aac -b:a 64k `
$([IO.Path]::Combine($outputPath, $file.BaseName + ".mp4"))
# Remove-Item $newVidStabResult
}
@prodehghan
Copy link
Author

  • Creates a directory named "c", and stores all output videos in that directory
  • Uses "vidstab" plugin to stabilize the output video
  • Uses x265 codec for video, with parameters: slow conversion, constant quality, Constant Rate Factor of 26
  • Uses aac coded for audio, with parameters: 64k bitrate
  • Copies all metadata information from the source file to the output file

@prodehghan
Copy link
Author

"ffmpeg" must be in your PATH.

@prodehghan
Copy link
Author

Modified to keep the VidStab result file, so we can tune settings and reencode the file without analyzing again.

The stabilization result is not satisfactory for my shaky mobile videos. Must be tuned.

@prodehghan
Copy link
Author

Parameters:

  • -i or -inputFiles: Files to convert. File names or pattern. Default: *.mp4,*.mov"
  • -op or -outputPath: Path (relative or abosulte) to store converted videos. Default: .\c

Processing:

  • vistab, x265, aac, like before
  • The smoothing parameter of "vidstab" is decreased from 10 to 8, to minimize the cropping that is caused by deshaking. I thought I was decreasing it from 15!
  • Uses "unsharp" plugin to enhance the video. The results are amazing, and I saw no negative impact so far.
  • The -preset is set to slower, so it is slower! But it saves some space.
  • The -crt is set to 20. Change it to 23 or 26 if you want smaller outputs. The change to quality is negligible, even for 26.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment