Skip to content

Instantly share code, notes, and snippets.

@thediveo
Created May 16, 2019 19:45
Show Gist options
  • Save thediveo/0a7353be8665ab4dd78435c5212c4e87 to your computer and use it in GitHub Desktop.
Save thediveo/0a7353be8665ab4dd78435c5212c4e87 to your computer and use it in GitHub Desktop.
Correctly transcode in-device Android screen recordings (which were created by ffmpeg), so that they can be correctly edited and processed further during post. In particular, enforces a fixed framerate and the correct color coefficients.
#!/bin/bash
# Correctly transcode in-device screen recordings from Android, so they
# do not cause all kinds of mishaps later down the editing and rendering
# pipeline. In particular, enforces a fixed framerate to avoid problems
# with widely varying dynamic frame rates, which can go from anywhere
# a frame every 10s of seconds to 100fps or so. Additionally, we
# enforce the correct color coefficients for the transcoding to avoid
# video tools to guesstimate completely nonsense transformation -- ffmpeg,
# I'm looking at you here.
shopt -s extglob
shopt -s globstar
# Project frame rate; we ensure during transcoding that we get a fixed
# framerate instead of a potentially wildly varying one which can cause
# lots of editing (cut position) problems later in post.
projectfps=25
# Run on all .raw.mp4 video files in all subdirectories. The transcoded
# file will be of the same name, but with only the .mp4 extension, instead
# of .raw.mp4.
for rawvid in **/*.raw.mp4; do
if [[ "$rawvid" =~ (.*)\.raw\.([^.]+) ]]; then
vbasename=${BASH_REMATCH[1]}
vext=${BASH_REMATCH[2]}
vidfile="$vbasename.$vext"
echo "raw footage" "$rawvid"
if [[ -f "$rawvid" ]]; then
# Raise the loglevel, to avoid the ttyp noise ffmpeg is so
# proud of. Other than that...
# - enforce a fixed frame rate
# - make sure to use the correct color coefficients for
# screencast recordings taken directly in-device from Android.
# - ensure that the transcoded video has the correct color
# coefficients explicitly burned in to avoid misinterpretations
# (except for totally brain-damaged video tools ignoring this).
ffmpeg -loglevel warning -y \
-i "$rawvid" \
-r $projectfps \
-crf 18 \
-color_primaries bt470bg \
-color_trc gamma28 \
-colorspace bt470bg \
"$vidfile"
if [[ $? -ne 0 ]]; then
tput setaf 1; echo "transcoding failed, exiting."; tput sgr0
exit 1
fi
tput setaf 2; echo "==> '$vidfile'"; tput sgr0
else
tput setaf 5; echo "Skipping non-existing '$rawvid'"; tput sgr0
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment