Created
November 15, 2022 10:15
-
-
Save Philmist/f5009396f759770cccd2e0ff4e40b015 to your computer and use it in GitHub Desktop.
Powershellとわかりあえなかった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, | |
Position=0, | |
HelpMessage="Literal path to movie file.")] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$MovieFile, | |
[Parameter(Mandatory=$true, | |
HelpMessage="Name of directory for generating image files.")] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$DirectoryName, | |
[Parameter(Mandatory=$true)] | |
[UInt32] | |
$TargetFrameNumber, | |
[Parameter()] | |
[UInt32] | |
$NumberOfImages = 42, | |
[Parameter()] | |
[UInt32] | |
$MovieFramerate = 60, | |
[Parameter()] | |
[ValidateSet("clock", "cclock", "none")] | |
[string[]] | |
$ImageRotation = "clock" | |
) | |
if ((Test-Path -Path $DirectoryName) -and (Test-Path -Path $DirectoryName -PathType Leaf)) { | |
Write-Error $DirectoryName " exists and not directory. exit." | |
Exit | |
} | |
if (-not(Test-Path -Path $DirectoryName -PathType Container)) { | |
Write-Verbose ("Create Directory: {0}" -f $DirectoryName) | |
try { | |
New-Item -Path $DirectoryName -ItemType Directory | |
} | |
catch { | |
throw $_.Exception.Message | |
} | |
} | |
$gci_result = Get-ChildItem -Path $DirectoryName -Filter *.png | |
if ($gci_result.Length -ne 0) { | |
try { | |
Write-Verbose ("Try to remove images: {0} for {1}" -f $DirectoryName, $gci_result.Length) | |
Remove-Item -Path $DirectoryName -Filter *.png | |
} | |
catch { | |
throw $_.Exception.Message | |
} | |
} | |
$HalfOfImages = [int][System.Math]::Floor(($NumberOfImages / 2)) | |
Write-Verbose ("Images: {0} / 2 -> {1}" -f $NumberOfImages, $HalfOfImages) | |
$ss = (($TargetFrameNumber - $HalfOfImages) / $MovieFramerate) | |
$ss = [Math]::Round($ss, 3) | |
$clockwise = @("-vf", ('transpose={0}' -f $ImageRotation)) | |
if ($ImageRotation -eq "none") { | |
$clockwise = @() | |
} | |
$param = @("-ss", $ss, "-i", $MovieFile) + $clockwise + @("-frames:v", $NumberOfImages, ("{0}\%04d.png" -f $DirectoryName)) | |
Write-Host $param | |
& ffmpeg $param |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment