From ca4060f94c47bb68213bc52cdaeab77aa63241ba Mon Sep 17 00:00:00 2001 From: moritz Date: Fri, 28 Aug 2009 15:55:13 +0000 Subject: [PATCH] Add a more fancy playlist script. git-svn-id: https://svn.xiph.org/trunk/ezstream@16523 0101bb08-14d6-0310-b084-bc0e0c8e3800 --- NEWS | 2 + examples/Makefile.am | 2 +- examples/meta.sh | 3 +- examples/play.sh | 3 +- examples/playlist-logger.sh | 123 ++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100755 examples/playlist-logger.sh diff --git a/NEWS b/NEWS index 4833a32..d52b4b8 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,8 @@ Changes in 0.5.6, released on XXXX-XX-XX: Failure to open a resource (e.g. a media file) is no longer fatal and operation will continue as far as possible. Idea from dhorton. (Ticket #1585) + * examples/: + - [NEW] Add a real-world example playlist script with logging feature. Changes in 0.5.5, released on 2009-08-01: diff --git a/examples/Makefile.am b/examples/Makefile.am index 62b76c9..46c4b27 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -6,6 +6,6 @@ dist_examples_DATA = \ ezstream_reencode_theora.xml ezstream_reencode_vorbis.xml \ ezstream_stdin_vorbis.xml ezstream_vorbis.xml \ ezstream_metadata.xml -dist_examples_SCRIPTS = meta.sh play.sh +dist_examples_SCRIPTS = meta.sh play.sh playlist-logger.sh CLEANFILES = core *.core *~ .*~ diff --git a/examples/meta.sh b/examples/meta.sh index b022dff..977637e 100755 --- a/examples/meta.sh +++ b/examples/meta.sh @@ -1,6 +1,7 @@ #!/bin/sh -# Example metadata script that has the behavior required by ezstream. +# Minimalist example metadata script that has the behavior required by +# ezstream. test -z "${1}" && echo "Ezstream presents" test x"${1}" = "xartist" && echo "Great Artist" diff --git a/examples/play.sh b/examples/play.sh index 077d391..d6ad192 100755 --- a/examples/play.sh +++ b/examples/play.sh @@ -1,5 +1,6 @@ #!/bin/sh -# Example playlist script that has the behavior required by ezstream. +# Minimalist example playlist script that has the behavior required by +# ezstream. echo "Great_Artist_-_Great_Song.ogg" diff --git a/examples/playlist-logger.sh b/examples/playlist-logger.sh new file mode 100755 index 0000000..2307cd2 --- /dev/null +++ b/examples/playlist-logger.sh @@ -0,0 +1,123 @@ +#!/bin/sh + +# Example playlist script, by Moritz Grimm +# Public Domain where available. Anywhere else: all possible permissions +# are granted and all warranties whatsoever are disclaimed. Use at your +# own risk. +# +# The script plays a simple playlist, but also logs when and which new +# song is queued. Because it writes the entire playlist each time it is +# run, the script will probably not perform well with huge (500+kB) +# playlist files. +# +# Before using it, the configuration variables below should be adjusted +# appropriately. +# +# When using this script in multiple instances of ezstream, use +# different STATE_DIR locations or rename the script to a unique name. + + +######################### +## BEGIN CONFIGURATION ####################################################### +######################### + + +# STATE_DIR +# Directory where this script keeps state. The directory should only be +# writeable by the user and nobody else (therefore, /tmp is not +# suitable.) +STATE_DIR="." + +# PLAYLIST +# The playlist that should be played by this script. +PLAYLIST="playlist.txt" + +# LOGFILE (optional) +# File to log song changes to. If not set, logging to file is disabled +# (and this script can be replaced by $PLAYLIST.) +LOGFILE="ezstream.log" + +# REPEAT (optional) +# Set to 1 to repeat the playlist indefinitely, set to 0 or leave commented +# out to allow ezstream to determine what to do at end-of-playlist. +#REPEAT=1 + + +####################### +## END CONFIGURATION ######################################################### +####################### + +_myname="$(basename $0)" + +# Check configuration above: +if [ -z "${STATE_DIR}" -o ! -d "${STATE_DIR}" ]; then + echo "${_myname}: STATE_DIR is not configured, does not exist or is not a directory." >&2 + exit 1 +fi +if [ -z "${PLAYLIST}" -o ! -e "${PLAYLIST}" ]; then + echo "${_myname}: PLAYLIST is not configured or does not exist." >&2 + exit 1 +fi +test -n "${REPEAT}" || REPEAT=0 + +# Set up helper files: +_state="${STATE_DIR}/${_myname}.state" +_playlist="`mktemp "${STATE_DIR}/${_myname}.XXXXXXXXXX"`" +if [ $? -ne 0 ]; then + echo "${_myname}: Unable to create temporary file." >&2 + exit 1 +fi +trap 'rm -f ${_playlist}' 0 +trap 'rm -f ${_playlist}; exit 1' 2 15 + +# Strip comments and empty lines from PLAYLIST, to support .m3u: +sed -e 's,#.*,,g' < ${PLAYLIST} | grep -v '^[[:space:]]*$' >> ${_playlist} +if [ $? -ne 0 ]; then + echo "${_myname}: Unable to prepare playlist." >&2 + exit 1 +fi + +# Create state file, if it does not exist: +test -f "${_state}" || touch "${_state}" +if [ $? -ne 0 ]; then + echo "${_myname}: Unable to create state file." >&2 + exit 1 +fi + +# Read current track no. from state file: +read _track_no < "${_state}" +if [ -z "${_track_no}" ]; then + _track_no=1 +fi + +# Count number of tracks in the playlist: +_num_tracks="$(wc -l < ${_playlist})" +if [ ${_num_tracks} -eq 0 ]; then + # Nothing to do, really. + exit 0 +fi + +# Handle the end-of-playlist case: +if [ ${_track_no} -gt ${_num_tracks} ]; then + if [ ${REPEAT} -ne 1 ]; then + # We're done. + rm -f "${_state}" + exit 0 + fi + _track_no=1 +fi + +# Get the current track from the playlist: +_track="$(head -n ${_track_no} ${_playlist} | tail -n 1)" + +# Output: +echo "${_track}" +test -z "${LOGFILE}" || \ + echo "$(date '+%b %d %H:%M:%S') playlist=\"${PLAYLIST}\" no=${_track_no} track=\"${_track}\"" \ + >> "${LOGFILE}" + +# Increment track number and store: +: $((_track_no += 1)) +echo "${_track_no}" > "${_state}" + +exit 0