Files
TimelapseGenerator/TimelapseGenerator.ps1

76 lines
2.8 KiB
PowerShell
Raw Normal View History

2021-12-22 12:33:52 +01:00
# TimelapseGenerator.ps1
# Generates a customizable timelapse
2021-12-23 10:57:46 +01:00
#
# Changelog:
# v1.2 - Some bugfixes for running as scheduled task, month-handling, etc.
2021-12-23 10:57:46 +01:00
# v1.1 - A bit more dynamic, updated ffmpeg to newest available version, changed codec to x265, added parameter handling
# v1.0 - Initial concept
param ($Camera,$Style='Daily',[string]$Month)
2021-12-22 12:33:52 +01:00
Set-Location $PSScriptRoot
if (!$Camera) { $Camera = Read-Host "Enter camera you want to process" }
2021-12-22 12:33:52 +01:00
2021-12-23 10:57:46 +01:00
switch ($Style) {
"Daily" { $rate = "10"; break}
"Monthly" { $rate = "2"; break}
# "Yearly" { $rate = "2"; break}
default {"Something else happened"; break}
}
2021-12-22 12:33:52 +01:00
$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\" # Final resting place for the video
2021-12-22 12:33:52 +01:00
2021-12-23 10:57:46 +01:00
2021-12-22 12:33:52 +01:00
# Get Last month
$MonthProcess = (Get-Date).AddMonths(-1)
$MonthProcess = $MonthProcess|Get-Date -Format MM
2021-12-23 10:57:46 +01:00
# Use passed parameter if it exists
if ($Month) {
$MonthProcess = $Month
}
2021-12-22 12:33:52 +01:00
# Copy photos to correct folder
$photos = Get-ChildItem $TimelapsePhotoSrc
2021-12-23 10:57:46 +01:00
if ($style -eq "Monthly") {
$Copyphotos = $photos | Where-Object {$_.name -Like "$($Camera)_????$($MonthProcess)??_12-00-??.jpg"}
}
elseif ($Style -eq "Daily") {
$photos = $photos|Where-Object {$_.length -gt 120000} # Maybe do a check on the size, it will require tuning
2021-12-23 10:57:46 +01:00
$Copyphotos = $photos | Where-Object {$_.name -Like "$($Camera)_????$($MonthProcess)*"}
}
#elseif ($Style -eq "Yearly") {
# # Takes all previous monthlies and combines them to a yearly timelapse
# $MonthlyTimelapses = Get-ChildItem $CompletedTimelapseDir | Where-Object {$_.Name -like "*.mkv"}
# foreach ($file in $MonthlyTimelapses) {
# ""
# }
#}
$regex = '(?<filedate>\d{4}(?:\.|-|_)?\d{2}(?:\.|-|_)?\d{2})[^0-9]'
If($Copyphotos[1] -match $regex) {
$date = $Matches['filedate'] -replace '(\.|-|_)',''
$date = [datetime]::ParseExact($date,'yyyyMMdd',[cultureinfo]::InvariantCulture)
$Year = Get-date $date -Format yyyy
}
2021-12-22 12:33:52 +01:00
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
2021-12-23 10:57:46 +01:00
if ($Style -eq "Daily" -or $style -eq "Monthly") {
& ".\ffmpeg.exe" -r $rate -i "$tempProcessingDir\$($Camera)_%05d.jpg" -s hd1080 -c:v libx265 -crf 28 $CompletedTimelapseDir\$Camera-$Style-$Year-$MonthProcess.mkv
2021-12-23 10:57:46 +01:00
}
elseif ($Style -eq "Yearly") {
& ".\ffmpeg.exe" -f concat -safe 0 -i $MonthlyTimelapses -c copy YearlyTimelapse-$Camera.mkv
2021-12-23 10:57:46 +01:00
}
2021-12-22 12:33:52 +01:00
# Cleanup
Remove-Item $TempProcessingDir\* -Force