2020-11-02 01:38:23 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# Give a file with images and timecodes and creates a video slideshow of them.
|
|
|
|
#
|
|
|
|
# Timecodes must be in format 00:00:00.
|
|
|
|
#
|
|
|
|
# Imagemagick and ffmpeg required.
|
|
|
|
|
2020-11-11 22:03:55 +00:00
|
|
|
# Application cache if not stated elsewhere.
|
|
|
|
cache="${XDG_CACHE_HOME:-$HOME/.cache}/slider"
|
2020-11-02 01:38:23 +00:00
|
|
|
|
2022-09-30 14:22:33 +00:00
|
|
|
while getopts "hvrpi:c:a:o:d:f:t:e:x:s:" o; do case "${o}" in
|
2020-11-11 22:03:55 +00:00
|
|
|
c) bgc="$OPTARG" ;;
|
|
|
|
t) fgc="$OPTARG" ;;
|
2021-02-10 16:47:30 +00:00
|
|
|
f) font="$OPTARG" ;;
|
2020-11-11 22:03:55 +00:00
|
|
|
i) file="$OPTARG" ;;
|
|
|
|
a) audio="$OPTARG" ;;
|
|
|
|
o) outfile="$OPTARG" ;;
|
|
|
|
d) prepdir="$OPTARG" ;;
|
|
|
|
r) redo="$OPTARG" ;;
|
|
|
|
s) ppt="$OPTARG" ;;
|
|
|
|
e) endtime="$OPTARG" ;;
|
|
|
|
x) res="$OPTARG"
|
|
|
|
echo "$res" | grep -qv "^[0-9]\+x[0-9]\+$" &&
|
|
|
|
echo "Resolution must be dimensions separated by a 'x': 1280x720, etc." &&
|
|
|
|
exit 1 ;;
|
|
|
|
p) echo "Purge old build files in $cache? [y/N]"
|
|
|
|
read -r confirm
|
|
|
|
echo "$confirm" | grep -iq "^y$" && rm -rf "$cache" && echo "Done."
|
|
|
|
exit ;;
|
|
|
|
v) verbose=True ;;
|
|
|
|
*) echo "$(basename "$0") usage:
|
|
|
|
-i input timecode list (required)
|
|
|
|
-a audio file
|
|
|
|
-c color of background (use html names, black is default)
|
|
|
|
-t text color for text slides (white is default)
|
|
|
|
-s text font size for text slides (150 is default)
|
2021-03-30 01:36:47 +00:00
|
|
|
-f text font for text slides (sans serif is default)
|
2020-11-11 22:03:55 +00:00
|
|
|
-o output video file
|
|
|
|
-e if no audio given, the time in seconds that the last slide will be shown (5 is default)
|
|
|
|
-x resolution (1920x1080 is default)
|
|
|
|
-d tmp directory
|
|
|
|
-r rerun imagemagick commands even if done previously (in case files or background has changed)
|
|
|
|
-p purge old build files instead of running
|
|
|
|
-v be verbose" && exit 1
|
2020-11-02 01:38:23 +00:00
|
|
|
|
2020-11-11 22:03:55 +00:00
|
|
|
esac done
|
2020-11-02 01:38:23 +00:00
|
|
|
|
2020-11-11 22:03:55 +00:00
|
|
|
# Check that the input file looks like it should.
|
|
|
|
{ head -n 1 "$file" 2>/dev/null | grep -q "^00:00:00 " ;} || {
|
|
|
|
echo "Give an input file with -i." &&
|
|
|
|
echo "The file should look as this example:
|
2020-11-02 01:38:23 +00:00
|
|
|
|
2020-11-11 22:03:55 +00:00
|
|
|
00:00:00 first_image.jpg
|
|
|
|
00:00:03 otherdirectory/next_image.jpg
|
|
|
|
00:00:09 this_image_starts_at_9_seconds.jpg
|
|
|
|
etc...
|
2020-11-02 01:38:23 +00:00
|
|
|
|
2020-11-11 22:03:55 +00:00
|
|
|
Timecodes and filenames must be separated by Tabs." &&
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -n "${audio+x}" ]; then
|
|
|
|
# Check that the audio file looks like an actual audio file.
|
|
|
|
case "$(file --dereference --brief --mime-type -- "$audio")" in
|
|
|
|
audio/*) ;;
|
|
|
|
*) echo "That doesn't look like an audio file."; exit 1 ;;
|
|
|
|
esac
|
|
|
|
totseconds="$(date '+%s' -d $(ffmpeg -i "$audio" 2>&1 | awk '/Duration/ {print $2}' | sed s/,//))"
|
|
|
|
endtime="$((totseconds-seconds))"
|
|
|
|
fi
|
|
|
|
|
|
|
|
prepdir="${prepdir:-$cache/$file}"
|
|
|
|
outfile="${outfile:-$file.mp4}"
|
|
|
|
prepfile="$prepdir/$file.prep"
|
|
|
|
|
|
|
|
[ -n "${verbose+x}" ] && echo "Preparing images... May take a while depending on the number of files."
|
2020-11-02 01:38:23 +00:00
|
|
|
mkdir -p "$prepdir"
|
|
|
|
|
|
|
|
{
|
|
|
|
while read -r x;
|
|
|
|
do
|
|
|
|
# Get the time from the first column.
|
|
|
|
time="${x%% *}"
|
|
|
|
seconds="$(date '+%s' -d "$time")"
|
|
|
|
# Duration is not used on the first looped item.
|
|
|
|
duration="$((seconds - prevseconds))"
|
|
|
|
|
2020-11-11 22:03:55 +00:00
|
|
|
# Get the filename/text content from the rest.
|
|
|
|
content="${x#* }"
|
|
|
|
base="$(basename "$content")"
|
2020-11-02 01:38:23 +00:00
|
|
|
base="${base%.*}.jpg"
|
|
|
|
|
2020-11-11 22:03:55 +00:00
|
|
|
if [ -f "$content" ]; then
|
|
|
|
# If images have already been made in a previous run, do not recreate
|
|
|
|
# them unless -r was given.
|
|
|
|
{ [ ! -f "$prepdir/$base" ] || [ -n "${redo+x}" ] ;} &&
|
|
|
|
convert -size "${res:-1920x1080}" canvas:"${bgc:-black}" -gravity center "$content" -resize 1920x1080 -composite "$prepdir/$base"
|
|
|
|
else
|
|
|
|
{ [ ! -f "$prepdir/$base" ] || [ -n "${redo+x}" ] ;} &&
|
2021-03-30 01:36:47 +00:00
|
|
|
convert -size "${res:-1920x1080}" -background "${bgc:-black}" -fill "${fgc:-white}" -font "${font:-Sans}" -pointsize "${ppt:-150}" -gravity center label:"$content" "$prepdir/$base"
|
2020-11-11 22:03:55 +00:00
|
|
|
fi
|
2020-11-02 01:38:23 +00:00
|
|
|
|
|
|
|
# If the first line, do not write yet.
|
|
|
|
[ "$time" = "00:00:00" ] || echo "file '$prevbase'
|
|
|
|
duration $duration"
|
|
|
|
|
|
|
|
# Keep the information required for the next file.
|
|
|
|
prevbase="$base"
|
|
|
|
prevtime="$time"
|
|
|
|
prevseconds="$(date '+%s' -d "$prevtime")"
|
2020-11-11 22:03:55 +00:00
|
|
|
done < "$file"
|
2020-11-02 01:38:23 +00:00
|
|
|
# Do last file which must be given twice as follows
|
|
|
|
echo "file '$base'
|
2020-11-11 22:03:55 +00:00
|
|
|
duration ${endtime:-5}
|
2020-11-02 01:38:23 +00:00
|
|
|
file '$base'"
|
|
|
|
} > "$prepfile"
|
2020-11-11 22:03:55 +00:00
|
|
|
if [ -n "${audio+x}" ]; then
|
|
|
|
ffmpeg -hide_banner -y -f concat -safe 0 -i "$prepfile" -i "$audio" -c:a aac -vsync vfr -c:v libx264 -pix_fmt yuv420p "$outfile"
|
2020-11-02 01:38:23 +00:00
|
|
|
else
|
2020-11-11 22:03:55 +00:00
|
|
|
ffmpeg -hide_banner -y -f concat -safe 0 -i "$prepfile" -vsync vfr -c:v libx264 -pix_fmt yuv420p "$outfile"
|
2020-11-02 01:38:23 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Might also try:
|
|
|
|
# -vf "fps=${fps:-24},format=yuv420p" "$outfile"
|
|
|
|
# but has given some problems.
|