# For batch:
# foreach($f in Get-ChildItem .) { .\hdr.ps1 $f }
$infile = $Args[0] # Input video
# Get some video length info using Info
$Info = "d:\Portable\FFMpeg\MediaInfo_CLI_23.07_Windows_x64\Info.exe"
$ffmpeg = "d:\Portable\FFMpeg\ffmpeg-2023-09-07-git-9c9f48e7f2-full_build\bin\ffmpeg.exe"
#$length_in_ms = $Info --Output='General;%Duration%' "$infile" | Out-String
$length_in_ms = & $Info --Language=raw --Full --Inform='Video;%Duration%' "$infile" | Out-String
#Info";%Duration/String4%"
$length_in_secs = $length_in_ms/1000
# Create output folder
$outfolder = "./screenshots/"
If(!(test-path $outfolder)) {
md $outfolder
}
# Create this many screenshots, or base it on video length
#$how_many = 20 # maybe make this the 2nd arg in the future?
$how_many = [math]::Round($length_in_secs/100)
Write-Host "Will create $how_many screenshots"
$used_seconds = @()
$i = 1
while($i -le $how_many) {
# Get a random second of the video that hasnt been already used
$random_sec = get-random -maximum $length_in_secs
$rounded = [math]::Round($random_sec)
if ($used_seconds.Contains($rounded) -eq $True) {
while ($used_seconds.Contains($rounded) -eq $True) {
#Write-Host $rounded "already used"
#Write-Host $used_seconds
$random_sec = get-random -maximum $length_in_secs
$rounded = [math]::Round($random_sec)
}
#Write-Host "Found unused" $rounded
}
$used_seconds += $rounded
# Outfile that screenshot will be saved as
$outfile = $outfolder + $infile + "_" + $rounded + ".png"
Write-Host $i/$how_many $outfile
#$result = & $ffmpeg -loglevel error -ss $rounded -i "$infile" -frames:v 1 $outfile | Out-String
& $ffmpeg -loglevel error -ss $rounded -i "$infile" -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -frames:v 1 $outfile
$i++
}