Skip to content

Instantly share code, notes, and snippets.

@leopku
Created February 8, 2023 15:16
Show Gist options
  • Save leopku/e48f2dec92540f83789430852527daa8 to your computer and use it in GitHub Desktop.
Save leopku/e48f2dec92540f83789430852527daa8 to your computer and use it in GitHub Desktop.
gen_posters.sh
#!/bin/bash
########################################################################
# gen_posters.sh - generate posters for plex video libraries
########################################################################
#
# Grabs a frame from a video file and creates a scaled, cropped
# poster file in the same directory
# Creates poster file in 2:3 aspect ratio
# Works on mkv, mp4 and avi
#
# Prequisites: ffmpeg, ffprobe
#
# Change directory to the base of the tree you want to create posters in
# run: bash gen_posters.sh
########################################################################
# Grab the frame at SECONDS
export SECONDS=5
# Library type
# Must be MOVIES or SHOWS
export LIBTYPE=SHOWS
# Locate all video files below current directory
find . | egrep -i '(mp4$|mkv$|avi$)' |
while read LINE
do
# Grab a frame and create a scaled version in current directory
DIR=$(dirname "$LINE")
ffmpeg -y -nostdin -ss 5 -i "$LINE" \
-vf "scale=640:-2" \
-frames:v 1 tmpfile.jpg > /dev/null 2>&1
# Get file dims
WIDTH=$(ffprobe -v error -select_streams v -show_entries stream=width,height \
-of csv=p=0:s=x tmpfile.jpg | cut -d'x' -f1)
HEIGHT=$(ffprobe -v error -select_streams v -show_entries stream=width,height \
-of csv=p=0:s=x tmpfile.jpg | cut -d'x' -f2)
NEW_WIDTH=$(echo "$HEIGHT*0.66" | bc | cut -d'.' -f1)
STARTX=$(echo "((640-$NEW_WIDTH)/2)" | bc | cut -d'.' -f1)
# Episode and movie posters are named differently
# per the plex standard
if [ $LIBTYPE = MOVIES ]
then
OUTPUT_FILE="$DIR/cover.jpg"
else
FILE=$(echo "$LINE" | rev | cut -d'.' -f2- | rev)
OUTPUT_FILE="$FILE.jpg"
fi
# Create cover.jpg alongside the video file
ffmpeg -y -nostdin -i tmpfile.jpg \
-vf "crop=$NEW_WIDTH:$HEIGHT:$STARTX:0" \
"$OUTPUT_FILE" > /dev/null 2>&1
echo Created $OUTPUT_FILE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment