#!/bin/bash
#                       /usr/local/bin/media2ogg
# https://crystalfaeries.net/posix/bin/media2ogg
# celeste:crystalfaery MEDIA2OGG	2020-09-14 07:52:13+00:00
# got videos that are really just music with useless video of a static image?
# convert them to .ogg audio files and prompt interactively to delete source file.
# by default with no arguments, we process the the first item in our media playlist.
# e.g. the one you just aborted out-of watching/listening in order to convert it.
# output is to configured directory /audio/ogg/
let help=9	# line# in file - 1

# CONFIGURATION: ( see also local BASH script: /usr/local/bin/monitor )
playlist="${HOME}/.monitor/l.txt"		# text format listing of input file_paths
oggdir="${HOME}/crystalfaeries.net/audio/ogg/"	# output .ogg files to this directory

if    [ $# -eq 0 ]
then	# no arguments provided therefore process the top item of our playlist
	if [ -s "${playlist}" ]
	then	# playlist is not empty
		exec $0 "$(head -n 1 ${playlist})"	# re-exec with an argument this time
		shift				 	# iterate next argument to command
	fi
fi
while [ $# -ne 0 ]
do
	new="$( echo ${oggdir}`basename ${1}` | sed 's/\.....$/.ogg/;s/\....$/.ogg/;s/\...$/.ogg/' )"
	echo 	"converting to:	${new}"
	nice ionice -c 3 ffmpeg -i "${1}" "${new}"
	let status=$?
  case	$status	in
  0)	/usr/local/bin/delete -i "${1}"			# offer opportunity to delete source file
	shift				 		# iterate next argument to command
	;;
  *)	echo	"${1}: error $status"	1>&2	# output filename + status
	exit $status
	;;
  esac
done
exit $?
