Skip to content

Instantly share code, notes, and snippets.

@sixteenmillimeter
Last active November 25, 2020 22:36
Show Gist options
  • Save sixteenmillimeter/81e57e52ca85e05e941544c96793097e to your computer and use it in GitHub Desktop.
Save sixteenmillimeter/81e57e52ca85e05e941544c96793097e to your computer and use it in GitHub Desktop.
Bash script for rendering video from CCapture.js for Twitter
#!/bin/bash
# Requirements:
# ffmpeg
# identify
# mktemp
# tar
# Generate .tar of frames using https://github.com/spite/ccapture.js/
# Use the path of the .tar file as the first and only arguement in this script
# Should be a UUID, so something like:
# bash ccapture_twitter.sh 82413b97-686b-c58b-2338-2becc7da4092.tar [twitter.mp4]
FRAMERATE=24
TAR_FILE="${1}"
VIDEO_FILE="${2}"
if [ "${TAR_FILE}" == "" ]; then
echo "Please include a .tar archive."
exit 1
fi
if [ "${VIDEO_FILE}" == "" ]; then
VIDEO_FILE="twitter.mp4"
fi
UNCOMP_FILE=`mktemp`.mp4
TAR_DIR=`mktemp -d`
mkdir -p "${TAR_DIR}"
echo "Extracting ${TAR_FILE}..."
tar -xf "${TAR_FILE}" -C "${TAR_DIR}"
echo "Determining image size..."
IMAGE_SIZE=`identify "${TAR_DIR}/0000000.png" | awk '{print $3}'`
if [[ *"x"* == "${IMAGE_SIZE}" ]]; then
IMAGE_SIZE_ARG=""
IMAGE_SIZE=""
else
IMAGE_SIZE_ARG="-s ${IMAGE_SIZE}"
fi
echo "Rendering uncompressed ${IMAGE_SIZE} video..."
ffmpeg \
-loglevel panic \
-r ${FRAMERATE} \
-f image2 ${IMAGE_SIZE_ARG} \
-i "${TAR_DIR}/%07d.png" \
-vcodec libx264 \
-crf 1 \
-pix_fmt yuv422p \
"${UNCOMP_FILE}"
echo "Transcoding video for Twitter as ${VIDEO_FILE}..."
ffmpeg \
-loglevel panic \
-i "${UNCOMP_FILE}" \
-pix_fmt yuv420p \
-vcodec libx264 \
-acodec aac \
-vb 2048k\
-minrate 1024k \
-maxrate 4096k \
-bufsize 1024k \
-ar 44100 \
-ac 2 \
-strict experimental \
-r ${FRAMERATE} \
"${VIDEO_FILE}"
echo "Cleaning up..."
rm "${UNCOMP_FILE}"
rm -r "${TAR_DIR}"
echo "Created ${VIDEO_FILE}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment