Skip to content

Instantly share code, notes, and snippets.

@lambdan
Last active July 28, 2020 17:32
Show Gist options
  • Save lambdan/70a36e53b90ebbff97bc9e73b2fa4414 to your computer and use it in GitHub Desktop.
Save lambdan/70a36e53b90ebbff97bc9e73b2fa4414 to your computer and use it in GitHub Desktop.
Create random screenshots out of video (requires ffmpeg and mediainfo)
# Requires 'ffmpeg' and 'mediainfo' in path
# For batch:
# foreach($f in Get-ChildItem .) { .\random_screenshots.ps1 $f }
$infile = $Args[0] # Input video
# Get some video length info using Mediainfo
$length_in_ms = mediainfo --Output='General;%Duration%' "$infile" | Out-String
$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 = 10 # 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
ffmpeg -loglevel error -ss $rounded -i "$infile" -frames:v 1 $outfile
$i++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment