# TimelapseGenerator.ps1
# Generates a customizable timelapse

$Camera = "Skiakersgutua"
$Style = "Monthly"

if ($Style -eq "Monthly") {
    $Rate = "2"
}
else { $Rate = "10"}


$TempProcessingDir = "C:\Temp\AutoTimelapse\" # Photos are copied here for temporary processing
$TimelapsePhotoSrc = "\\megabyte.home.zyrex.org\Vault\Vault\Upload\Webcam\Timelapse\" # Here lies all the photos
$CompletedTimelapseDir = "\\megabyte.home.zyrex.org\Mediavault\Privat\Timelapses\"

# Get Current Month
$MonthCur = Get-Date -Format MM
# Get Last month
$MonthProcess = $MonthCur - 1


# Copy photos to correct folder
$photos = Get-ChildItem $TimelapsePhotoSrc
$Copyphotos = $photos | Where-Object {$_.name -Like "$($Camera)_????$($MonthProcess)??_12-00-??.jpg"}

foreach ($lapse in $Copyphotos) {
    Copy-Item $lapse.fullname $TempProcessingDir\
    }
# Rename photos
$i = 1
Get-ChildItem $TempProcessingDir | Sort-Object | ForEach-Object { Rename-Item $_.FullName -NewName ("$($Camera)_{0:D5}.jpg" -f $i++) }

# Make timelapse 
# -crf bestemmer kvalitet. 20 er default, 51 er dÃ¥rligst
# fÃ¸rste -r er fin pÃ¥ 10 for kjapp timelapse, -r 5 er fin for litt roligere. andre -r kan vÃ¦re 30 eller 25? (Quick 25, normal 10)
& "./ffmpeg.exe" -r $rate -i "$tempProcessingDir\$($Camera)_%05d.jpg" -s hd1080 -c:v libx264 -r 30 -pix_fmt yuv420p -preset slow -crf 20 \\megabyte.home.zyrex.org\Mediavault\Privat\Timelapses\Gaardsplass_2020-$MonthProcess.mkv

# Cleanup
Remove-Item $TempProcessingDir\* -Force
