Skip to content

Instantly share code, notes, and snippets.

@ar-juan
Created October 30, 2023 20:04
Show Gist options
  • Save ar-juan/aab74af46b097848c1da02d61bc69283 to your computer and use it in GitHub Desktop.
Save ar-juan/aab74af46b097848c1da02d61bc69283 to your computer and use it in GitHub Desktop.
Script to resize videos, keeping all metadata. I use it to make iCloud videos smaller.
#!/bin/bash
# Source directory for input files
src_dir="/Volumes/ExterneSSD/Resize/ToBeConverted"
# Destination directory for converted files
dest_dir="/Volumes/ExterneSSD/Resize/Resized"
# Directory for moved original files
converted_originals_dir="/Volumes/ExterneSSD/Resize/ConvertedOriginals"
# Create a subfolder with the current datetime in "original_dir"
timestamp=$(date +"%Y-%m-%d-%H-%M")
converted_originals_subdir="$converted_originals_dir/$timestamp"
dest_subdir="$dest_dir/$timestamp"
mkdir -p "$converted_originals_subdir"
mkdir -p "$dest_subdir"
# Suffix to add to the converted file name
suffix="_resized"
# Iterate over each file in the source directory
for file in "$src_dir"/*; do
if [ -f "$file" ]; then
# Get the file extension
ext="${file##*.}"
# Check if the file name ends with "_resized"
if [[ "$file" == *"_resized.${ext}" ]]; then
continue # Skip files already processed
fi
# Generate the destination file path with the suffix
dest_file="${dest_subdir}/$(basename "$file" .${ext})${suffix}.${ext}"
echo "call ffmpeg"
# Convert the file using FFmpeg (modify the FFmpeg command as needed)
ffmpeg -i "$file" -c:v libx265 -crf 28 -c:a aac -b:a 128k -tag:v hvc1 "$dest_file"
echo "call exiftool"
# Copy metadata from the original file to the converted file using ExifTool
exiftool -tagsFromFile "$file" -extractEmbedded -all:all -FileModifyDate -overwrite_original "$dest_file"
mv "$file" "$converted_originals_subdir/"
echo "Processed: $file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment