#!/usr/bin/env sh

# $1: image filename, $2: post id
gen_thumb () {
  if [ ! -f "thumb_${1}" ]; then
    echo "Generating thumbnail for ${1}"
    convert -define jpeg:size=100x65 "${1}" -thumbnail 100x65^ -gravity center -extent 100x65 "thumb_${1}"
    mogrify -unsharp 0.25x0.08+8.3+0.045 "thumb_${1}"
  fi
}

# echo "Working on file: $1"
# numframes=$(
#   ffprobe -v error \
#   -select_streams v:0 \
#   -count_packets \
#   -show_entries stream=nb_read_packets \
#   -of csv=p=0 "$1")
#
# echo "Total number of frames : ${numframes}"
# offset=(numframes/10)
#
# for ((i=1; i<=10; i++)); do
#  let extractframe=(${offset}*${i})
#  echo "Extracting frame : ${extractframe}"
#
#  ffmpeg -i "$1" \
#      -nostats \
#      -loglevel 0 \
#      -vf "select=eq(n\,${extractframe})" \
#      -vframes 1 \
#      poster_${extractframe}.png
# done
# ----------------------------------------------------------------------------

# Using the thumbnail filter

# ffmpeg has lots of built-in filters to help you manipulate your media
# files. One of them is the thumbnail filter. It will select the most
# representative frame in a given sequence of consecutive frames automatically
# from the video.

# To use the thumbnail filter, add -vf "thumbnail" to the basic command:

# 1ffmpeg -i input.mp4 \
# 2       -vf "thumbnail=300" \
# 3       -frames:v 1 thumbnail-300.png

# ffmpeg analyzes the frames in batches of 300 and picks the most
# representative frame out of them. The process continues until the end of the
# video and we get the best frame among all the batches.

# Attaching your chosen thumbnail to the video

# 1ffmpeg -i input.mp4 \
# 2       -i thumbnail.png \
# 3       -map 1 \
# 4       -map 0 \
# 5       -c copy \
# 6       -disposition:0 attached_pic output.mp4
