Skip to content

Instantly share code, notes, and snippets.

@steveseguin
Created August 1, 2024 12:11
Show Gist options
  • Save steveseguin/8083172a20ad7c9ebcb449e22fc8fe67 to your computer and use it in GitHub Desktop.
Save steveseguin/8083172a20ad7c9ebcb449e22fc8fe67 to your computer and use it in GitHub Desktop.
Concatenate sequential files into a single file. macOS script. Intended for VDO.Ninja users.
#!/bin/bash
# Check if a filename was provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <filename.webm>"
exit 1
fi
# Get the base filename and directory
baseFilename="$1"
workingDirectory=$(dirname "$baseFilename")
baseFilenameWithoutExtension="${baseFilename%.*}"
baseFileExtension="${baseFilename##*.}"
# Prepare output filename
outputFile="${workingDirectory}/${baseFilenameWithoutExtension}_concatenated.${baseFileExtension}"
# Function to append file content
append_file_content() {
local sourceFile="$1"
local destinationFile="$2"
if [ -f "$sourceFile" ]; then
cat "$sourceFile" >> "$destinationFile"
local bytesWritten=$(stat -f %z "$sourceFile")
echo "Appended $bytesWritten bytes from $sourceFile"
return $bytesWritten
else
echo "Error: File $sourceFile not found"
return 0
fi
}
# Delete the output file if it already exists
if [ -f "$outputFile" ]; then
rm "$outputFile"
echo "Deleted existing output file: $outputFile"
fi
# Create a new output file
touch "$outputFile"
echo "Created new output file: $outputFile"
totalBytesWritten=0
filesConcatenated=0
# Concatenate the base file
if [ -f "$baseFilename" ]; then
append_file_content "$baseFilename" "$outputFile"
bytesWritten=$?
totalBytesWritten=$((totalBytesWritten + bytesWritten))
filesConcatenated=$((filesConcatenated + 1))
echo "Copied base file: $baseFilename ($bytesWritten bytes)"
fi
# Look for additional parts
i=1
while true; do
partFile="${workingDirectory}/${baseFilenameWithoutExtension}.${baseFileExtension}_${i}"
if [ -f "$partFile" ]; then
echo "Found part file: $partFile"
echo "File permissions: $(ls -l "$partFile" | awk '{print $1}')"
append_file_content "$partFile" "$outputFile"
bytesWritten=$?
totalBytesWritten=$((totalBytesWritten + bytesWritten))
filesConcatenated=$((filesConcatenated + 1))
echo "Appended file: $partFile ($bytesWritten bytes)"
i=$((i + 1))
else
echo "No more part files found. Last checked: $partFile"
break
fi
done
# Check if we actually concatenated anything
if [ $filesConcatenated -eq 0 ] && [ $totalBytesWritten -eq 0 ]; then
echo "No files found to concatenate."
rm "$outputFile"
exit 1
fi
outputFileSize=$(stat -f %z "$outputFile")
echo "Concatenation complete. Output file: $outputFile"
echo "Total files concatenated: $filesConcatenated"
echo "Total bytes written: $totalBytesWritten"
echo "Output file size: $outputFileSize bytes"
if [ $totalBytesWritten -ne $outputFileSize ]; then
echo "WARNING: Mismatch between bytes written and output file size!"
fi
# List all files in the directory that start with the base filename
echo "Listing all related files in the directory:"
ls -l "${workingDirectory}/${baseFilenameWithoutExtension}"* | awk '{print $9, "-", $5, "bytes"}'
@steveseguin
Copy link
Author

An easy installer:
curl -sL https://gist.githubusercontent.com/steveseguin/8083172a20ad7c9ebcb449e22fc8fe67/raw/3af5f5ec41b2883c2af4e739a7967e82e44524c6/combine.sh -o combine.sh && chmod +x combine.sh

To use: ./combine.sh "filename.webm"

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