#!/bin/bash
#                       /usr/local/bin/music2video
# https://crystalfaeries.net/posix/bin/music2video
# celeste:crystalfaery MUSIC2VIDEO 2018-08-15 22:43:21+00:00
# From: https://www.commandlinefu.com/commands/view/24067/convert-mp3s-an-image-to-mp4-videos-using-ffmpeg

# For EACH music file, merge it with THE image to make A video file.
# Optional output (file)Type/extension, e.g. "-t ogv",
# No option = default to mp4, equivalent to ("-t mp4")
# FIRST ARGUMENT = path to image file
# 2-n   ARGUMENTs are audio file paths
# e.g.: "music2video -t ogv /home/galleries/image.jpg $HOME/crystalfaeries.net/audio/*.ogg"
# would convert all the .ogg's in audio directory to .ogv's nondestructively.
# WE EXPECT TO FAIL WITH NON-MP4 TYPES, NEED TO CHANGE ENCODING PARAMETER
let help=14		# line#-1
filetype="mp4"		# default output file "type" = extension
case	"${1}" in
-v|--version)
	tail -n +4 $0 | head -n 1 
	exit	$?
	;;
-h|--help)
	head -n $help $0
	exit	$?
	;;
-t|--type)
	shift || exit $?	# dispose the flag
	filetype="${1}"	# user specified output file type/extension
	shift || exit $?	# dispose the type
	;;
*)
imagename="${1}"	# FIRST ARGUMENT = path to image file
shift || exit $?	# 2-n   ARGUMENTs are audio file paths
for name in "$@"
    do
	ffmpeg			\
	--optimize		\
	-loop 1			\
	-i "${imagename}"	\
	-i "${name}"		\
	-shortest		\
	-c:v libx264		\
	-preset ultrafast	\
	-c:a copy		\
	"${name%.*}.${filetype}"	# what kine vid we making?
    done;;
esac
exit	$?			# 1/4 - assed error handling :-)

