reogg - Prepare audio-files for radio loops

Did you ever want to automate live audio streaming from the command-line? Well, I do. To cut it short the requirements here were:

  • command-line (terminal) only
  • generate one long stream - concatenate all songs
  • retain (id3) tags
  • stream Ogg/Vorbis to an icecast server

In principle one can simply concatenate OGG files and use oggfwd to send it to icecast. However most players (in particular mplayer) will produce garbled output when mixing OGG files with different bitrate/samplerate/channels in one stream. Furthermore some input files may not be OGG/Vorbis encoded in the first place.

To remedy the situation, here's a simple shell-script that converts all files in a given folder to a common format and copies over the id3 tags. You'll need to have oggenc, oggdec, mpg321, flac123 and id3tool installed.

You can download the script from the repository, but it's simple enough to be printed here:

view source

#  converts audio files in SRC folder to a common OGG format which allows to
#  concatenate the resulting files.


mkdir -p ${DST}
for file in $(ls ${SRC}); do
  echo "${file}" | grep -e "\.mp3$"  >/dev/null && DEC="mpg321 -q -w- "
  echo "${file}" | grep -e "\.ogg$"  >/dev/null && DEC="oggdec -Q -o- "
  echo "${file}" | grep -e "\.flac$" >/dev/null && DEC="flac123 -q -w- "

  test -z "${DEC}"         && continue;
  test -r "${SRC}/${file}" || continue;

  NFN=$(echo ${file} | sed 's/flac$/ogg/' | sed 's/mp3$/ogg/')

  echo "${file}" | grep -e "\.mp3$"  >/dev/null && \
  TAG=$(id3tool "${SRC}/${file}" \
        | sed 's/"/\\"/g' \
        | awk '/^Song Title/{printf "-t \"%s\" ",substr($0,13);}
               /^Artist/    {printf "-a \"%s\" ",substr($0,10);}
               /^Album/     {printf "-l \"%s\" ",substr($0,9);}
               /^Genre/     {printf "-G \"%s\" ",substr($0,9);}
               /^Track/     {printf "-N \"%s\" ",substr($0,9);}')

  echo "${file}" | grep -e "\.ogg$"  >/dev/null && \
  TAG=$(ogginfo "${SRC}/${file}" \
        | sed 's/"/\\"/g' \
        | awk 'BEGIN{IGNORECASE=1;}
               /Title=/      {printf "-t \"%s\" ",substr($0,8);}
               /Artist=/     {printf "-a \"%s\" ",substr($0,9);}
               /Album=/      {printf "-l \"%s\" ",substr($0,8);}
               /Genre=/      {printf "-G \"%s\" ",substr($0,8);}
               /Tracknumber=/{printf "-N \"%s\" ",substr($0,14);}')

  echo "#-- FILE: ${file}"
  CMD="$DEC \"${SRC}/${file}\""
  CMD+="| oggenc --resample 44100 -q 6 -o \"${DST}/${NFN}\" ${TAG} -"
  echo "${CMD}"
  eval $CMD || exit

To transmit the converted files repeatedly to an icecast server I run the following shell script in a GNU Screen:

view source


FILES=$(ls media/*.ogg)

function loopcat {
  while true; do
    # TODO: optionally randomize $FILES here..
    cat $FILES

while true; do
  loopcat \
  | oggfwd localhost 8000 password -p -g "Genre" -n "Title" -d "Description" mountpoint.ogg;
  echo "---- LOST CONNECTION ----"
  sleep 2

If you find these scripts useful or want to suggest some improvements.. drop me a word:

You could leave a comment if you were logged in.
blog/reogg_-_prepare_audio_files_for_radio_loops.txt · Last modified: 23.12.2011 20:26 (external edit)