Skip to content

Instantly share code, notes, and snippets.

@McBaws
Created September 30, 2022 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McBaws/42d90a8c456dd5b45cd9059a92bf8f4d to your computer and use it in GitHub Desktop.
Save McBaws/42d90a8c456dd5b45cd9059a92bf8f4d to your computer and use it in GitHub Desktop.
Takes screenshots including subtitles for animebytes, automatically determining most suitable subtitle track
# requires mediainfo in path + specifying mpv and output paths in the parameters below.
# trackselect.lua (https://github.com/po5/trackselect) is also required unless subtitle track id is specified.
# trackselect should be placed in the specified mpv path.
# oxipng in path is also required to use -opt.
# set parameters
Param(
[string]$inFile,
[string]$mpvPath = "D:\Videos\Screenshots\ZZZmpv\mpv.exe",
[string]$outDirectory = "D:\Videos\Screenshots\",
[switch]$help,
[int]$screens = 10,
[int]$subIndex = 999996,
[int]$pngCompression = 7,
[switch]$sameFolder,
[switch]$opt
)
$mpvDir = Split-Path -parent $mpvPath
if($help){
Write-Host "`nOptions:`n-screens`t`tSpecify number of screenshots.`n`n-subIndex`t`tSpecify index of subtitle track to be used in input file.`n`n-pngCompression`tSpecify png compression level (1-9).`n`n-sameFolder`t`tCreate screenshot folder in the same directory as the input file.`n`n--mpvPath`t`tSpecify path of mpv executable.`n`n-opt`t`t`tOptimize pngs.`n"
Exit
}
if(!$inFile){
Write-Host "`nPlease specify an input file.`nTry -help for more info.`n"
Exit
}
# get filename and directory of input file
$inFilename = Split-Path $inFile -Leaf
$inDirectory = Split-Path -parent $inFile
# if samefolder flag set, outdir is set to input's dir
if($sameFolder){
$outDirectory = $inDirectory + "\screens\"
}
# create output folder
if(!(Test-Path $outDirectory)) {
New-Item -Path $outDirectory -ItemType Directory
}
# Get length of video in seconds
$lengthInMs = mediainfo --Output='General;%Duration%' "$inFile" | Out-String
$lengthInSecs = $lengthInMs/1000
$usedSeconds = @()
$i = 1
while($i -le $screens) {
# get a random second of the video that hasnt been already used
$randomSeconds = get-random -maximum $lengthInSecs
$rounded = [math]::Round($randomSeconds)
if ($usedSeconds.Contains($rounded) -eq $True) {
while ($usedSeconds.Contains($rounded) -eq $True) {
$randomSeconds = get-random -maximum $lengthInSecs
$rounded = [math]::Round($randomSeconds)
}
}
$usedSeconds += $rounded
# outFile that screenshot will be saved as
$outFile = $outDirectory + $inFilename + "_" + $rounded + ".png"
Write-Host "`n" + $i/$screens $outFile
if($subIndex = 999996){
$luaFile = $mpvDir + "\trackselect.lua"
& $mpvPath --no-config --script=$luaFile --vo=image --vo-image-format=png --vf=format:rgb32 --no-audio --untimed --vo-image-png-compression=$pngCompression --start=$rounded --frames=1 $inFile -o $outFile | Out-Null
}else{
& $mpvPath --no-config --vo=image --vo-image-format=png --vf=format:rgb32 --no-audio --untimed --vo-image-png-compression=$pngCompression --start=$rounded --frames=1 --sid=$subIndex $inFile -o $outFile | Out-Null
}
if($opt){
oxipng $outfile -o max -p
}
#ffmpeg -loglevel error -ss $rounded -copyts -i "$inFile" -vf subtitles:si=0 -pix_fmt rgb24 -frames:v 1 $outFile
$i++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment