Skip to content

Instantly share code, notes, and snippets.

@pirate
Created January 30, 2021 01:40
Show Gist options
  • Select an option

  • Save pirate/6fe7d480d46079a47f108131f41409c4 to your computer and use it in GitHub Desktop.

Select an option

Save pirate/6fe7d480d46079a47f108131f41409c4 to your computer and use it in GitHub Desktop.
Re-encode a bunch of video files into x265 MP4 without losing EXIF metadata
#!/usr/bin/env bash
# Requires: ffmpeg, exiftool (install via apt/brew first)
# Usage:
# $ cd ~/Videos
# $ ./compress_videos.sh
# [+] Converting all .mp4 files in ~/Videos to .x265.mp4 files...
# - √ GP013838.mp4 (2.5GB) -> GP013838.x265.mp4 (142MB)
# - √ ...
# [√] Done converting all .mp4 files in $PWD.
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
IFS=$'\n'
echo "[+] Converting all .mp4 files in $PWD to .x265.mp4 files..."
for filename in *.{mp4,MP4}; do
if echo "$filename" | grep "\.x265\.mp4"; then
continue
fi
without_ext="$(echo $filename | cut -d'.' -f1)"
x265_filename="${without_ext}.x265.mp4"
orig_size="$(du -h $filename | awk '{print $1}')"
if ! [[ -f "$x265_filename" ]]; then
ffmpeg \
-i "$filename" \
-map_metadata 0 \
-movflags use_metadata_tags \
-c:v libx265 \
-c:a copy \
"$x265_filename"
fi
exiftool -TagsFromFile "$filename" "-all:all>all:all" "$x265_filename"
x265_size=="$(du -h $x265_filename | awk '{print $1}')"
echo " - √ $filename ($orig_size) -> $x265_filename ($x265_size)"
done
echo "[√] Done converting all .mp4 files in $PWD."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment