Skip to content

Instantly share code, notes, and snippets.

@speeddragon
Last active April 4, 2025 14:44
Show Gist options
  • Save speeddragon/4640f4df2219c70ee286690aeff6cbf7 to your computer and use it in GitHub Desktop.
Save speeddragon/4640f4df2219c70ee286690aeff6cbf7 to your computer and use it in GitHub Desktop.
Add time code to Google Pixel or iPhone videos
#!/bin/bash
#
# Add time code to Google Pixel videos by extracting it from
# the filename.
# When executing via Finder, run in the folder there it is located
cd -- "$(dirname "$BASH_SOURCE")"
pxl_filename_to_timecode() {
# $1 = PXL_20201202_123357972.jpg
TIME=$(cut -d'_' -f3 <<< $1)
# $TIME = 123357972.jpg
HOUR=$(cut -c 1-2 <<< $TIME)
MINUTE=$(cut -c 3-4 <<< $TIME)
SECOND=$(cut -c 5-6 <<< $TIME)
MS=$(cut -c 7-9 <<< $TIME)
TC_MS=$(expr $MS \* 59 / 999)
if [[ $TC_MS -lt 10 ]]; then
echo "$HOUR:$MINUTE:$SECOND:0$TC_MS"
else
echo "$HOUR:$MINUTE:$SECOND:$TC_MS"
fi
}
iphone_videos_to_timecode() {
# Only works in OSX
# Get creation date
# 03/31/2025 11:53:40
TIME=$(GetFileInfo -d "$1")
HOUR=$(cut -c 12-13 <<<$TIME)
MINUTE=$(cut -c 15-16 <<<$TIME)
SECOND=$(cut -c 18-19 <<<$TIME)
echo "$HOUR:$MINUTE:$SECOND:000"
}
# For Google Pixel videos
for filename in *.mp4; do
DELIMITER=$(echo "_tc")
if [[ "$filename" =~ .*"$DELIMITER".* ]];then
echo "Ignore file $filename"
else
TIMECODE=$(pxl_filename_to_timecode "$filename")
FILE_EXTENSION=$(echo "$filename" | awk -F'[.]' 'NF>1 {print $NF}')
FILE_NAME=$(echo "$filename" | cut -d. -f1)
NEW_FILENAME=$(echo "${FILE_NAME}${DELIMITER}.${FILE_EXTENSION}")
if ! [ -f $NEW_FILENAME ]; then
echo "Added timecode ${TIMECODE} to ${filename} in ${NEW_FILENAME}"
ffmpeg -i "$filename" -timecode $TIMECODE -codec copy "$NEW_FILENAME" 2> /dev/null
else
echo "Timecoded file ${NEW_FILENAME} already exists!"
fi
fi
done
# For iPhone videos
for filename in *.MOV; do
DELIMITER=$(echo "_tc")
if [[ "$filename" =~ .*"$DELIMITER".* ]];then
echo "Ignore file $filename"
else
TIMECODE=$(iphone_to_timecode "$filename")
FILE_EXTENSION=$(echo "$filename" | awk -F'[.]' 'NF>1 {print $NF}')
FILE_NAME=$(echo "$filename" | cut -d. -f1)
NEW_FILENAME=$(echo "${FILE_NAME}${DELIMITER}.${FILE_EXTENSION}")
if ! [ -f $NEW_FILENAME ]; then
echo "Added timecode ${TIMECODE} to ${filename} in ${NEW_FILENAME}"
ffmpeg -i "$filename" -timecode $TIMECODE -codec copy "$NEW_FILENAME" 2> /dev/null
else
echo "Timecoded file ${NEW_FILENAME} already exists!"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment