La Vita è Bella

2006-01-10

Script: mp32ogg

I wrote this script to convert mp3 files into ogg files. ogg is a open source audio format that is smaller and seems better than mp3.

Another reason I wrote it is that the audio player for PalmOS, AeroPlayer, is free with ogg plug-in. But the mp3 plugin is not free.

You'll need the following softwares to use this script: mplayer, oggenc, id3v2, iconv. If you are using Debian or Ubuntu, you can apt-get them. iconv is used to conver id3 tags that in GB18030 into UTF-8, if you don't need such feature, you can ignore it, and comment line 9.

It will convert all files under current directory into ogg, and try to keep the tag info. It will try id3v2 first, then id3v1.

It first use mplayer to dump mp3 file into wav file, then use oggenc to convert wav file into ogg file.

It can be ran with a parameter, to specify the ogg quality. If omitted, it's 2.

It's based on the script by stoneboy @ newsmth forum. My work is try to keep tag info.

Enjoy it :-)

 1  #!/bin/sh
 2  #
 3  # re-encode.sh: this script will re-encode audio files to
 4  # ogg formatted files.
 5  
 6  DUMPFILE=audiodump.wav
 7  QUALITY=${1:-2}
 8  CONVERT="tee"
 9  CONVERT=" iconv -f gb18030 -t utf8 - "
10  SOURCE=mp3
11  TARGET=ogg
12  DECODER=`which mplayer`
13  ENCODER=`which oggenc`
14  
15  # to check if your decoder and encoder really exist.
16  # if yes then proceed, else quit.
17  
18  if [ -x "${DECODER}" -a -x "${ENCODER}" ]; then
19          for file in *.${SOURCE}; do
20                  echo "Your files to be encoded from ${SOURCE} to ${TARGET}."
21                  echo "Encoder is ${ENCODER}."
22                  echo "Your encoded file name will be ${file%.${SOURCE}}.${TARGET}"
23                  echo "Press Ctrl-c if I am wrong:)"
24                  #sleep 1
25  
26                  TITLE=`id3v2 -l "${file}" | grep TIT2 | ${CONVERT} | sed -e "s/^[^:]*: \(.*\)$/\1/g"`
27                  if [ -z "${TITLE}" ]; then
28                          TITLE=`id3v2 -l "${file}" | grep Title | grep Artist | ${CONVERT} | sed -e "s/^Title\s*: \(.*\)Artist\s*: \(.*\)$/\1/g"`
29                  fi
30                  TITLE=`echo $TITLE | sed -e "s/\s*$//g" | sed -e "s/\s/\\ /g"`
31                  TITLE="--title=${TITLE}"
32                  
33                  ARTIST=`id3v2 -l "${file}" | grep TPE1 | ${CONVERT} | sed -e "s/^[^:]*: \(.*\)$/\1/g"`
34                  if [ -z "${ARTIST}" ]; then
35                          ARTIST=`id3v2 -l "${file}" | grep Title | grep Artist | ${CONVERT} | sed -e "s/^Title\s*: \(.*\)Artist\s*: \(.*\)$/\2/g"`
36                  fi
37                  ARTIST=`echo $ARTIST | sed -e "s/\s*$//g" | sed -e "s/\s/\\ /g"`
38                  ARTIST="--artist=${ARTIST}"
39                  
40                  ALBUM=`id3v2 -l "${file}" | grep TALB | ${CONVERT} | sed -e "s/^[^:]*: \(.*\)$/\1/g"`
41                  if [ -z "${ALBUM}" ]; then
42                          ALBUM=`id3v2 -l "${file}" | grep Album | grep Year | ${CONVERT} | sed -e "s/^Album\s*: \(.*\)Year\s*: \(.*\),\s*Genre\s*:.*$/\1/g"`
43                  fi
44                  ALBUM=`echo $ALBUM | sed -e "s/\s*$//g" | sed -e "s/\s/\\ /g"`
45                  ALBUM="--album=${ALBUM}"
46                  
47                  TRACK=`id3v2 -l "${file}" | grep TRCK | ${CONVERT} | sed -e "s/^[^:]*: \(\d*\)/\1/g"`
48                  if [ -z "${TRACK}" ]; then
49                          TRACK=`id3v2 -l "${file}" | grep Comment | grep Track | ${CONVERT} | sed -e "s/^Comment\s*: \(.*\)Track\s*: \(.*\)$/\2/g"`
50                  fi
51                  TRACK=`echo $TRACK | sed -e "s/\s*$//g" | sed -e "s/\s/\\ /g"`
52                  TRACK="--tracknum=${TRACK}"
53                  
54                  YEAR=`id3v2 -l "${file}" | grep TYER | ${CONVERT} | sed -e "s/^[^:]*: \(\d*\)/\1/g"`
55                  if [ -z "${YEAR}" ]; then
56                          YEAR=`id3v2 -l "${file}" | grep Album | grep Year | ${CONVERT} | sed -e "s/^Album\s*: \(.*\)Year\s*: \(.*\),\s*Genre\s*:.*$/\2/g"`
57                  fi
58                  YEAR=`echo $YEAR | sed -e "s/\s*$//g" | sed -e "s/\s/\\ /g"`
59                  YEAR="--date=${YEAR}"
60  
61                  $DECODER -ao pcm -vo null "$file"
62                  echo "Will use the following command line to encode ogg:"
63                  echo \"${ENCODER} ${DUMPFILE} --quality=${QUALITY} --output="${file%.${SOURCE}}.${TARGET}" "${ARTIST}" "${TITLE}" "${ALBUM}" "${TRACK}" "${YEAR}"\"
64                  #$CMDLINE
65                  ${ENCODER} ${DUMPFILE} --quality=${QUALITY} --output="${file%.${SOURCE}}.${TARGET}" "${ARTIST}" "${TITLE}" "${ALBUM}" "${TRACK}" "${YEAR}"
66                  rm -fr "$DUMPFILE"
67          done
68  else
69          echo "You need [mplayer] and [oggenc] to proceed!"
70          echo "Quitting ...!"
71          exit 1
72  fi

00:49:50 by fishy - Permanent Link

May the Force be with you. RAmen