Created
January 31, 2024 09:07
-
-
Save dprado75/617e3c46698bfa5323d31fc904848e02 to your computer and use it in GitHub Desktop.
Powershell script to transform a compilation of security camera videos into a timelapse with a few caviats - (1) extract a few frames from each security cam video (2) add a small label with a timestamp to each selected frame (3) rename the files sequentially (4) build a final video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string]$SourceFolder, | |
[string]$OutputFolder | |
) | |
# Ensure output folder exists | |
if (-not (Test-Path -Path $OutputFolder)) { | |
New-Item -ItemType Directory -Path $OutputFolder | |
} | |
# Change to the source directory | |
Set-Location $SourceFolder | |
# Script 1: Extract frames from MP4 files | |
$mp4Files = Get-ChildItem -Filter *.mp4 -Recurse | |
foreach ($file in $mp4Files) { | |
$subFolderName = Split-Path $file.DirectoryName -Leaf | |
$baseName = $subFolderName + "_" + $file.BaseName | |
ffmpeg -ss 00:00:00 -i $file.FullName -vframes 1 -q:v 31 "$OutputFolder\$($baseName).00.jpg" | |
ffmpeg -ss 00:00:02 -i $file.FullName -vframes 1 -q:v 31 "$OutputFolder\$($baseName).02.jpg" | |
} | |
# Script 2: Reformat JPEG filenames | |
$jpgFiles = Get-ChildItem -Path $OutputFolder -Filter *.jpg | |
foreach ($file in $jpgFiles) { | |
$fileName = $file.BaseName | |
$parts = $fileName -split '_' | |
$datePart = $parts[0] -replace '(\d{2})-(\d{2})-(\d{2})', 'Jan-$3' | |
$timePart = $parts[1] -replace '(\d{2})-(\d{2})-(\d{2})', '$1-$2-$3' | |
$newName = $datePart + '_' + $timePart + '_' + $parts[2..($parts.Count - 1)] -join '_' | |
Rename-Item $file.FullName "$OutputFolder\$newName.jpg" | |
} | |
# Script 3: Rename files sequentially | |
$counter = 1 | |
$jpgFiles = Get-ChildItem -Path $OutputFolder -Filter *.jpg | |
foreach ($file in $jpgFiles) { | |
$newFileName = "image_{0:D4}.jpg" -f $counter | |
Rename-Item $file.FullName "$OutputFolder\$newFileName" | |
$counter++ | |
} | |
# Script 4: Create timelapse video | |
Set-Location $OutputFolder | |
ffmpeg -framerate 15 -i image_%04d.jpg -c:v libx264 -crf 17 -pix_fmt yuv420p my-timelapse15.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment