I'm aiming to merge video files that created by a dashcam.
While driving, the camera writes a new file at every n minutes. If n=3, 30 minutes of driving generates 10 files that "date created" attribute within 3 minutes.
An example folder content:
Filename | Date Created | Group |
---|---|---|
1.ts | 2024-09-03 19:00 | Group 1 |
2.ts | 2024-09-03 19:03 | Group 1 |
3.ts | 2024-09-03 19:06 | Group 1 |
4.ts | 2024-09-03 19:20 | Group 2 |
5.ts | 2024-09-03 19:23 | Group 2 |
6.ts | 2024-09-03 19:40 | Group 3 |
7.ts | 2024-09-03 19:44 | Group 4 |
Desired output (in a loop probably):
1.ts, 2.ts, 3.ts
4.ts, 5.ts
6.ts
7.ts
How can I get get 4 groups of filenames from this input in Powershell? I then will combine these group of files to a single video per group so I have a single video per drive session.
(Transport Stream files are directly concenateable without processing with ffmpeg etc. so I can use a simple copy /b "input files" output.ts
)
Thanks
Here you go. You can run the script by copying the code in a text editor, save the file as yourname.ps1 drap&drop it into a powershell window and append your parameter at the end before starting it with enter e.g: myscriptname.ps1 "pathtomyfolder" 4
You can copy the path to the folder via right click on it and "copy adress". the timegap parameter defaults to 3. the video appending i coould not test, for that I would need some sample ts files (joke). U can build that in in the end
param(
[string]$targetFolder,
[int]$timegap=3
)
# Define the main list to hold all the drive collections with titles
$listOfLists = @()
# Get all files in the folder, ordered by creation time (oldest first)
$files = Get-ChildItem -Path $targetFolder | Sort-Object CreationTime
# Initialize variables
$previousFile = $null
$driveCollection = @()
$collectionNumber = 1
foreach ($file in $files) {
if ($previousFile) {
# Calculate the time difference in minutes between the current and previous file
$timeDifference = ($file.CreationTime - $previousFile.CreationTime).TotalMinutes
# If the time difference is more than 3 minutes, start a new drive collection
if ($timeDifference -gt $timegap) {
# Add the current drive collection to the list of lists with a title (if not empty)
if ($driveCollection.Count -gt 0) {
$title = "Collection $collectionNumber"
$listOfLists += $title
$listOfLists += [System.Collections.ArrayList]$driveCollection.Clone()
$collectionNumber++
}
# Start a new drive collection
$driveCollection.Clear()
}
}
# Add the current file with its creation time to the current drive collection
$driveCollection += "$($file.Name) - Created: $($file.CreationTime)"
# Set the current file as the previous file for the next iteration
$previousFile = $file
}
# Add the last drive collection to the list of lists with a title (if not empty)
if ($driveCollection.Count -gt 0) {
$title = "Collection $collectionNumber"
$listOfLists += $title
$listOfLists += [System.Collections.ArrayList]$driveCollection.Clone()
}
# Output the list of lists for verification
$listOfLists