Skip to content

Instantly share code, notes, and snippets.

@jeroenbourgois
Created June 9, 2021 17:49
Show Gist options
  • Save jeroenbourgois/414606a4ce7c5ecb9511adf06cb63ac5 to your computer and use it in GitHub Desktop.
Save jeroenbourgois/414606a4ce7c5ecb9511adf06cb63ac5 to your computer and use it in GitHub Desktop.
Generate video with title screen
#!/bin/bash -e
#
# Titlevid
#
# Utility wrapper to prepend any video with a fade in
# from black and a title screen
#
# version: 0.1
# created: 08/06/2021
# last update: 08/06/2021
# author: Jack + Joe
#
# CHANGELOG
#
# 0.1 (08/06/2021)
# Initial version
TITLE=""
TMP_DIR="/tmp"
DURATION=5
V=8
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
usage() {
cat <<-EOF
Usage: titlevid [OPTIONS]
Options:
-i Path to input file
-o Path to output file
-V Verbose output, takes ffmpeg numbers (default 8)
8 = quiet
16 = error
24 = warning
32 = full info
40 = debug
-t, --title string Text on the title screen (max 15 chars!)
-d, --duration integer Duration of the intro sequence (default 5)
-h, --help This screen
-v, --version Show the version
EOF
return;
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
usage
exit
;;
-i|--input)
INPUT="$2"
shift # past argument
shift # past value
;;
-V)
V="$2"
shift # past argument
shift # past value
;;
-o|--output)
OUTPUT="$2"
shift # past argument
shift # past value
;;
-v|--version)
echo "titlevid version 0.1, build June 8 2021"
exit
;;
-d|--duration)
DURATION="$2"
shift # past argument
shift # past value
;;
-t|--title)
TITLE="$2"
shift # past argument
shift # past value
;;
*)
COMMAND+=("$1")
# shift # past value
# shift # past value
shift
;;
esac
done
if [ -z "$INPUT" ] || [ -z "$OUTPUT" ]
then
usage
exit
else
filename_ext=$(basename -- "$INPUT")
extension="${filename_ext##*.}"
filename="${filename_ext%.*}"
SIZE=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $INPUT)
FPS=$(ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of csv=s=x:p=0 $INPUT)
FPS=$(echo $FPS | bc -l)
# generate title
echo "==> Generating title"
ffmpeg -y -v $V -f lavfi -i color=size=${SIZE}:duration=$DURATION:rate=$FPS:color=black -vf "drawtext=fontfile=/System/Library/Fonts/SFNS.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='${TITLE}'" -frames:v 1 /tmp/title.png
echo "==> Concat title with video... (demuxing, this can take a while)"
ffmpeg -y -v $V -loop 1 -framerate $FPS -t $DURATION -i /tmp/title.png -t $DURATION -f lavfi -i aevalsrc=0 -i $INPUT -filter_complex '[0:0] [1:0] [2:0] [2:1] concat=n=2:v=1:a=1' /tmp/concat.mp4
# fade in
echo "==> Fade in video... (this can take a while)"
ffmpeg -y -v $V -i /tmp/concat.mp4 -vf "fade=t=in:st=0:d=3" -c:a copy $OUTPUT
echo "Done! You can find your video at: $OUTPUT"
rm /tmp/concat.mp4 /tmp/title.png
fi
@jeroenbourgois
Copy link
Author

Usage:

./titlevid -t "Slam dunk, da funk?" -i ~/Downloads/testvid.mp4 -o ~/Desktop/testing.mp4

@jeroenbourgois
Copy link
Author

testing.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment