Skip to content

Instantly share code, notes, and snippets.

@aynik
Created February 15, 2023 00:36
Show Gist options
  • Save aynik/f547a31ba3d0dcbfeeaf9775291d5ef1 to your computer and use it in GitHub Desktop.
Save aynik/f547a31ba3d0dcbfeeaf9775291d5ef1 to your computer and use it in GitHub Desktop.
Mixtape video with video background
#!/usr/bin/env bash
font_file="~/Library/Fonts/GoNotoCurrent.ttf"
video_background="$1"
shift
function ellipsis (){
awk -v len=$1 '{ if (length($0) > len) print substr($0, 1, len-3) "..."; else print; }'
}
for file in "$@"; do
title="$(exiftool -Title -b "$file" | ellipsis 25)"
artist="$(exiftool -Artist -b "$file" | ellipsis 60)"
album="$(exiftool -Album -b "$file" | ellipsis 70)"
year="$(exiftool -Date -b "$file" | cut -c1-4)"
duration="$(printf '%.3f' $(ffprobe -i "$file" -show_entries format=duration -v quiet -of csv="p=0"))"
half_duration="$(echo "$duration*.5" | bc)"
if [ "$video_background" == "no" ]; then
background_file="~/Pictures/mixtape.jpg"
else
background_file="$(mktemp).mp4"
ffmpeg -y -ss 0 -t "$half_duration" -i "$video_background" -y -filter_complex "[0]split[b][c];[c]reverse[r];[b][r]concat" "$background_file"
fi
ffmpeg -y \
-i "$background_file" \
-i "$file" \
-filter_complex "[1:v]scale=500:500,crop=w=500:h=500[ovrl]; \
[0:V]drawtext=fontsize=36:fontfile='$font_file':fontcolor=white:shadowcolor=black@0.4:shadowx=2:shadowy=2:text='${title//:/\\:}':x=(W/2)-20:y=H-(3.5*(H/10))[f1]; \
[f1]drawtext=fontsize=26:fontfile='$font_file':fontcolor=white:shadowcolor=black@0.4:shadowx=2:shadowy=2:text='${artist//:/\\:}':x=(W/2)-20:y=H-(4*(H/10))[f2]; \
[f2]drawtext=fontsize=22:fontfile='$font_file':fontcolor=white:shadowcolor=black@0.4:shadowx=2:shadowy=2:text='${album//:/\\:}':x=(W/2)-20:y=H-(2.5*(H/10))[f3]; \
[f3]drawtext=fontsize=15:fontfile='$font_file':fontcolor=white:shadowcolor=black@0.4:shadowx=2:shadowy=2:text='${year//:/\\:}':x=(W/2)-20:y=H-(2*(H/10))[bg]; \
[bg]drawbox=x=((iw-w)/12)+1:y=((ih-h)/2)+2:w=500:h=500:color=black@0.2[box]; \
[box][ovrl]overlay=(W-w)/12:(H-h)/2" \
-s "1920x1080" \
-c:v libx264 -preset slow -crf 18 -profile:v high -level 4.0 \
-bf 2 -coder 1 -pix_fmt yuv420p -b:v 10M -threads 4 -cpu-used 0 -r 30 \
-movflags +faststart "${file}.mp4"
done
ffmpeg -safe 0 -f concat \
-i <(for f in "$@"; do echo "file '$PWD/${f}.mp4'"; done) \
-c copy mixtape.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment