Skip to content

Instantly share code, notes, and snippets.

@steveseguin
Created August 1, 2024 12:09
Show Gist options
  • Save steveseguin/7ca1df1df9ec6042f27ecc8d258e3f30 to your computer and use it in GitHub Desktop.
Save steveseguin/7ca1df1df9ec6042f27ecc8d258e3f30 to your computer and use it in GitHub Desktop.
Concatenate sequential files into a single file. Windows Powershell. Intended for VDO.Ninja users.
# Check if a filename was provided
if ($args.Count -ne 1) {
Write-Host "Usage: .\script.ps1 <filename.webm>"
exit 1
}
# Get the full path of the base filename
$baseFilename = Resolve-Path $args[0]
$workingDirectory = Split-Path $baseFilename -Parent
$baseFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($baseFilename)
$baseFileExtension = [System.IO.Path]::GetExtension($baseFilename)
# Prepare output filename
$outputFile = Join-Path $workingDirectory "${baseFileNameWithoutExtension}_concatenated${baseFileExtension}"
# Function to append file content in chunks
function Append-FileContent {
param (
[string]$sourceFile,
[string]$destinationFile
)
try {
$buffer = New-Object byte[] 1MB
$sourceStream = [System.IO.File]::OpenRead($sourceFile)
$destStream = [System.IO.File]::OpenWrite($destinationFile)
$destStream.Position = $destStream.Length
$bytesWritten = 0
while ($bytesRead = $sourceStream.Read($buffer, 0, $buffer.Length)) {
$destStream.Write($buffer, 0, $bytesRead)
$bytesWritten += $bytesRead
}
Write-Host "Appended $bytesWritten bytes from $sourceFile"
return $bytesWritten
}
catch {
Write-Host "Error processing file $sourceFile : $_"
return 0
}
finally {
if ($sourceStream) { $sourceStream.Close() }
if ($destStream) { $destStream.Close() }
}
}
# Delete the output file if it already exists
if (Test-Path $outputFile) {
Remove-Item $outputFile
Write-Host "Deleted existing output file: $outputFile"
}
# Create a new output file
New-Item $outputFile -ItemType File | Out-Null
Write-Host "Created new output file: $outputFile"
$totalBytesWritten = 0
$filesConcatenated = 0
# Concatenate the base file
if (Test-Path $baseFilename) {
$bytesWritten = Append-FileContent $baseFilename $outputFile
$totalBytesWritten += $bytesWritten
$filesConcatenated++
Write-Host "Copied base file: $baseFilename ($bytesWritten bytes)"
}
# Look for additional parts
$i = 1
while ($true) {
$partFile = Join-Path $workingDirectory "${baseFileNameWithoutExtension}${baseFileExtension}_${i}"
if (Test-Path $partFile) {
Write-Host "Found part file: $partFile"
$acl = Get-Acl $partFile
Write-Host "File permissions: $($acl.AccessToString)"
$bytesWritten = Append-FileContent $partFile $outputFile
$totalBytesWritten += $bytesWritten
$filesConcatenated++
Write-Host "Appended file: $partFile ($bytesWritten bytes)"
$i++
}
else {
Write-Host "No more part files found. Last checked: $partFile"
break
}
}
# Check if we actually concatenated anything
if ($filesConcatenated -eq 0 -and $totalBytesWritten -eq 0) {
Write-Host "No files found to concatenate."
Remove-Item $outputFile
exit 1
}
$outputFileInfo = Get-Item $outputFile
Write-Host "Concatenation complete. Output file: $outputFile"
Write-Host "Total files concatenated: $filesConcatenated"
Write-Host "Total bytes written: $totalBytesWritten"
Write-Host "Output file size: $($outputFileInfo.Length) bytes"
if ($totalBytesWritten -ne $outputFileInfo.Length) {
Write-Host "WARNING: Mismatch between bytes written and output file size!"
}
# List all files in the directory that start with the base filename
Write-Host "Listing all related files in the directory:"
Get-ChildItem -Path $workingDirectory -Filter "$baseFileNameWithoutExtension*" | ForEach-Object {
Write-Host "$($_.Name) - $($_.Length) bytes"
}
@steveseguin
Copy link
Author

  • download as combine.ps1; inside the same folder as the video files
  • open up windows powershell
  • from the correct folder, run ./combine.sh "videofile.webm"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment