Das Wiki ist im Nur-Lese-Modus, es wird durch ein neues Wiki ersetzt. Mehr Infos....

Konvertierung von Audiodateien (audio2audio.sh)

(by qgirl)

Das Script konvertiert Audiodateien und kann Audio-CDs rippen. Die konvertierten Audiodateien haben den gleichen Namen aber anderes vom gewählten Audioformat abhängiges Suffix.

Folgende Audioformate werden unterstützt:

Zum Konvertieren/Rippen wird GStreamer-0.10 verwendet.

Man kann zwischen 3 Qualitätsstufen bei der Konvertierung wählen:

Folgende Programme/Gstreamer-Plugins werden benötigt:

Aufruf

./audio2audio.sh [OPTION]... URL [URL]...

Beschreibung

OPTIONEN

URLs können Block-Devices (z.B. /dev/cdrom) sein, welche eine Audio-CD enthalten. Es können auch Verzeichnisse mit Audiodateien angegeben werden oder auch einzelne Audiodateien.

Beispiele

Script

# ============================================================================
# This script converts audiofiles into another audio format and can rip audio
# cd's. The converted audio file has the same name but another suffix depending 
# on the selected audio format.
#
# The following audio formats are supported:
# wav, flac, ogg, mp3
#
# Conversion is done by gstreamer.
#
# Authors : qgirl
# ----------------------------------------------------------------------------
# This script comes with ABSOLUTELY NO WARRANTY.
# This is free software, and you are welcome to redistribute it under certain
# conditions. See the GNU General Public Licence for details.
# ----------------------------------------------------------------------------
# Dependencies:
# - gst-plugins-base-0.10 
# - gst-plugins-good-0.10  
# - gst-plugins-ugly-0.10  (for converting to mp3)
# - cdrdao                 (for reading toc of audio cd)
# ----------------------------------------------------------------------------
# Version              Date    Author  Changes
# -------              ------  ------  ---------------------------------------
# Version="0.0.1"    # 061118  qgirl   initial version
  Version="0.1.0"    # 070613  qgirl   rewrite (now using gstreamer)
# ============================================================================

# ----------------------------------------------------------------------------
# Constants and options
# ----------------------------------------------------------------------------

# constants
GST_LAUNCH="gst-launch-0.10"
GST_INSPECT="gst-inspect-0.10"

AUDIOFORMAT[0]="wav"
AUDIOFORMAT[1]="flac"
AUDIOFORMAT[2]="ogg"
AUDIOFORMAT[3]="mp3"

NoOfFormats=${#AUDIOFORMAT[*]}

AUDIODECODER[0]="wavparse"
AUDIODECODER[1]="flacdec"
AUDIODECODER[2]="oggdemux ! vorbisdec"
AUDIODECODER[3]="mad"
AUDIODECODER[${NoOfFormats}]="decodebin"

# encode to min. size
q=$((0 * ${NoOfFormats}))
AUDIOENCODER[q+0]="wavenc"
AUDIOENCODER[q+1]="flacenc quality=8"
AUDIOENCODER[q+2]="vorbisenc quality=-0.1 ! oggmux"
AUDIOENCODER[q+3]="lame vbr=new vbr-quality=9"

# encode with default value of gstreamer plugin
q=$((1 * ${NoOfFormats}))
AUDIOENCODER[q+0]="wavenc"
AUDIOENCODER[q+1]="flacenc"
AUDIOENCODER[q+2]="vorbisenc ! oggmux"
AUDIOENCODER[q+3]="lame"

# encode to high quality
q=$((2 * ${NoOfFormats}))
AUDIOENCODER[q+0]="wavenc"
AUDIOENCODER[q+1]="flacenc quality=0"
AUDIOENCODER[q+2]="vorbisenc quality=0.8 ! oggmux"
AUDIOENCODER[q+3]="lame vbr=new vbr-quality=0"

# options
NewAudioFormat="ogg"
DirectoryForAudioFiles="."
Quality=1

# ----------------------------------------------------------------------------
# Helppage
# ----------------------------------------------------------------------------

showVersion()
{
  echo ""
  echo "${0}-${Version}"
  echo "Copyright (c) 2006 by qgirl."
  echo ""
  echo "This script comes with ABSOLUTELY NO WARRANTY."
  echo "This is free software, and you are welcome to redistribute it under" 
  echo "certain conditions."
  echo "See the GNU General Public Licence for details."
  echo ""
}

showHelp()
{
  echo ""
  echo "Usage: ${0} [OPTION]... URL [URL]..."
  echo ""
  echo "OPTIONS"
  echo "  --help              show this help and exit"
  echo "  --version           show version and exit"
  echo "  --directory PATH    PATH is the directory where the converted audio" 
  echo "                      files will be stored."
  echo "                      (Default path: current directory)"
  echo "  --format FORMAT     FORMAT is the audio format in which the audio files"
  echo "                      given by the URLs shall be converted"
  echo "                      (Default : ogg)"
  echo "  --min-size          using a quality level with high compression"
  echo "                      (Default: default value of the used gst-plugin)"
  echo "  --high-quality      using a quality level for best sound"
  echo "                      (Default: default value of the used gst-plugin)"
  echo ""
  echo "URLs are either block devices (e.g. /dev/cdrom) which contain an audio CD,"
  echo " directories which contains audio files or single audio files."
}

# ----------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------

getSinkFileName()
{
  BaseFileName="${2##*/}"
  BaseFileNameWithoutSuffix="${BaseFileName%\.*}"
  echo "${DirectoryForAudioFiles}/${BaseFileNameWithoutSuffix}.${1}"
}

getFormatNo()
{
  FormatNo=-1
  SearchFormat="${1}"
  shift
  i=0
  for Format in ${*} ; do
    if test "${SearchFormat}" = "${Format}" ; then
      FormatNo=${i}
    fi
    i=$((${i}+1))
  done
  echo ${FormatNo}
}

isFilterAvailable()
{
  DoCheck=true
  for Element in ${*} ; do
    if test "${Element}" = "!" ; then
      DoCheck=true
    elif ${DoCheck} ; then
      Inspect=`${GST_INSPECT} | grep " ${Element}: "`
      if test -z "${Inspect}" ; then
        echo "${Element} not available."
        return 1
      fi
      DoCheck=false
    fi
  done
  return 0
}

convertAudioFile()
{
  AudioSrc="${2}"
  AudioSink=`getSinkFileName ${1} "${AudioSrc}"`
  # get format number of original audiofile from file suffix
  FileSuffix="${AudioSrc##*\.}"
  SrcFormat=`getFormatNo ${FileSuffix} ${AUDIOFORMAT[*]}`
  # get format number for target format
  SinkFormat=`getFormatNo ${1} ${AUDIOFORMAT[*]}`
  # looking for filter
  if test ${SrcFormat} -eq ${SinkFormat} ; then
    # no convert needed, only copy if source and sink directory differ
    SrcDir=`dirname "${AudioSrc}"`
    SinkDir=`dirname "${AudioSink}"`
    if test "${SrcDir}" != "${SinkDir}" ; then
      cp "${AudioSrc}" "${AudioSink}"
    fi
  else
    EncoderNo=$((${Quality} * ${NoOfFormats} + ${SinkFormat}))
    if test ${SrcFormat} -lt 0 ; then
      Filter="decodebin ! progressreport update-freq=2 ! audioconvert ! ${AUDIOENCODER[${EncoderNo}]}"
    else
      Filter="${AUDIODECODER[${SrcFormat}]} ! progressreport update-freq=2 ! audioconvert ! ${AUDIOENCODER[${EncoderNo}]}"
    fi
    if isFilterAvailable ${Filter} ; then
      echo "${AudioSrc} ! ${Filter} ! ${AudioSink}"
      ${GST_LAUNCH} filesrc location="${AudioSrc}" ! ${Filter} ! filesink location="${AudioSink}"
    fi
  fi
}

ripAudioCD()
{
  SinkFormat=`getFormatNo ${1} ${AUDIOFORMAT[*]}`
  TocFile="${DirectoryForAudioFiles}/cd.toc"
  if test -f "${TocFile}" ; then
    rm "${TocFile}"
  fi
  NoOfTracks=`cdrdao read-toc --fast-toc --device /dev/hdb "${TocFile}"; cat "${TocFile}" | grep -c -e "^TRACK AUDIO"`
  rm "${TocFile}"
  for ((TrackNo=1; TrackNo <= ${NoOfTracks}; TrackNo++)) ; do
    TrackFileName=`printf "track_%.3d.wav" ${TrackNo}`
    NewAudioFile=`getSinkFileName ${1} ${TrackFileName}`
    EncoderNo=$((${Quality} * ${NoOfFormats} + ${SinkFormat}))
    Filter="progressreport update-freq=2 ! audioconvert ! ${AUDIOENCODER[${EncoderNo}]}"
    if isFilterAvailable ${Filter} ; then
      echo "${AudioFile} ! ${Filter} ! ${NewAudioFile}"
      ${GST_LAUNCH} cdparanoiasrc device="${2}" track=${TrackNo} ! ${Filter} ! filesink location="${NewAudioFile}"
    fi
  done
}

# ----------------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------------

if test ${#} -eq 0 ; then
  showHelp
  exit 0
else
  while test ! -z "${1}" ; do
    case ${1} in
      --help)
        showHelp
        exit 0
        ;;
      --version)
        showVersion
        exit 0
        ;;
      --format)
        shift
        NewAudioFormat=${1}
        ;;
      --min-size)
        Quality=0
        ;;
      --high-quality)
        Quality=2
        ;;
      --directory)
        shift
        DirectoryForAudioFiles=${1}
        ;;
      *)
        if test -b "${1}" -o -d "${1}" -o -f "${1}"; then
          if test -z "${UrlList}" ; then
            UrlList="${1}"
          else
            UrlList="${UrlList} ${1}"
          fi
        else
          echo "${0}: Invalid parameter -- '${1}' will be ignored" >&2
          echo "»${0} --help« for further information" >&2
        fi
        ;;
    esac
    shift
  done
fi

SinkFormat=`getFormatNo ${NewAudioFormat} ${AUDIOFORMAT[*]}`
if test ${SinkFormat} -lt 0 ; then
  echo "${0}: Not supported audio format --'${NewAudioFormat}'" >&2
  echo "»${0} --help« for further information" >&2
  exit -1
fi

for Url in "${UrlList}" ; do
  if test -b "${Url}" ; then
    ripAudioCD ${NewAudioFormat} "${Url}"
  elif test -d "${Url}" ; then
    for FileUrl in "${Url}"/*.* ; do
      echo "convert ${FileUrl}"
      if test -f "${FileUrl}"; then
        convertAudioFile ${NewAudioFormat} "${FileUrl}"
      fi
    done
  else
    convertAudioFile ${NewAudioFormat} "${Url}"
  fi
done

exit 0

Audio2Audio (last edited 2008-01-03 15:04:14 by goeb)