Last active
August 17, 2021 20:17
-
-
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
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
[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 | |
} |
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 toslower
, 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
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.