#!/bin/sh # # $Id: flactoogg,v 1.4 2005/02/17 13:07:00 lukem Exp $ # # Copyright 2003-2004 Luke Mewburn # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # flagtoogg, flactomp3, oggtomp3 -- # Convert between the appropriate formats, attempting to retain as # much of the appropriate metadata tags as possible. # # http://www.mewburn.net/luke/src/flactoogg # # REQUIRES: # tool from # ---- ---- # flac http://flac.sourceforge.net/ # gogo http://homepage1.nifty.com/herumi/gogo_e.html # id3ed http://www.azstarnet.com/~donut/programs/id3ed.html # ogg123 http://www.vorbis.com/ (vorbis-tools) # wav_to_ogg oggfile # encode from .wav on stdin to ogg, writing to oggfile # wav_to_ogg() { oggenc -Q -q 1 -o "$1" - } # wav_to_mp3 mp3file # encode from .wav on stdin to mp3, writing to mp3file # wav_to_mp3() { gogo -b 64 stdin "$1" } # tag_mp3_from_oggtags mp3file tagfile # parse tagfile into ID3 tags and apply to mp3file. tag_mp3_from_vorbis() { mp3file="$1" tagfile="$2" album= artist= title= track= while read -r tag; do tagvar="${tag%%=*}" tagval="${tag#*=}" case "${tagvar}" in ALBUM) album="${tagval}" ;; ARTIST) artist="${tagval}" ;; TITLE) title="${tagval}" ;; TRACKNUMBER) track="${tagval}" ;; esac done < "${tagfile}" id3ed -q -a "${album}" -n "${artist}" -s "${title}" -k "${track}" \ "${mp3file}" } flac_to_ogg() { file="$1" filebn="${file%.flac}" fileogg="${filebn}.ogg" if [ "${filebn}" = "${file}" ]; then echo "Skipping ${file}" return fi if [ "${fileogg}" -nt "${file}" ]; then echo "Skipping ${file}" return fi echo "Converting ${file}" tmptag="${filebn}.tmp.tag" tmpogg="${filebn}.tmp.ogg" trap "/bin/rm -f \"${tmptag}\" \"${tmpogg}\"; exit 0" 0 2 3 13 metaflac --export-tags-to="${tmptag}" "${file}" flac -sdc "${file}" | wav_to_ogg "${tmpogg}" if [ -s "${tmptag}" ]; then vorbiscomment -w -c "${tmptag}" "${tmpogg}" "${fileogg}" else mv "${tmpogg}" "${fileogg}" fi rm -f "${tmpogg}" "${tmptag}" trap "-" 0 2 3 13 } flac_to_mp3() { file="$1" filebn="${file%.flac}" filemp3="${filebn}.mp3" if [ "${filebn}" = "${file}" ]; then echo "Skipping ${file}" return fi if [ "${filemp3}" -nt "${file}" ]; then echo "Skipping ${file}" return fi echo "Converting ${file}" tmptag="${filebn}.tmp.tag" tmpmp3="${filebn}.tmp.mp3" trap "/bin/rm -f \"${tmptag}\" \"${tmpmp3}\"; exit 0" 0 2 3 13 metaflac --export-tags-to="${tmptag}" "${file}" flac -sdc "${file}" | wav_to_mp3 "${tmpmp3}" tag_mp3_from_vorbis "${tmpmp3}" "${tmptag}" mv "${tmpmp3}" "${filemp3}" rm -f "${tmpmp3}" "${tmptag}" trap "-" 0 2 3 13 } ogg_to_mp3() { file="$1" filebn="${file%.ogg}" filemp3="${filebn}.mp3" if [ "${filebn}" = "${file}" ]; then echo "Skipping ${file}" return fi if [ "${filemp3}" -nt "${file}" ]; then echo "Skipping ${file}" return fi echo "Converting ${file}" tmptag="${filebn}.tmp.tag" tmpmp3="${filebn}.tmp.mp3" trap "/bin/rm -f \"${tmptag}\" \"${tmpmp3}\"; exit 0" 0 2 3 13 vorbiscomment -l "${file}" > "${tmptag}" oggdec -q -o - "${file}" | wav_to_mp3 "${tmpmp3}" tag_mp3_from_vorbis "${tmpmp3}" "${tmptag}" mv "${tmpmp3}" "${filemp3}" rm -f "${tmpmp3}" "${tmptag}" trap "-" 0 2 3 13 } for f in "$@"; do case "$0" in *flactoogg) flac_to_ogg "${f}" ;; *flactomp3) flac_to_mp3 "${f}" ;; *oggtomp3) ogg_to_mp3 "${f}" ;; esac done