LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash mp3 to ogg script (https://www.linuxquestions.org/questions/programming-9/bash-mp3-to-ogg-script-535632/)

True`Colors 03-08-2007 03:39 AM

Bash mp3 to ogg script
 
I took some ideas from others scripts on the net, and made a script to suit my needs. Please give some help and tips for improvemets, tanks. Recursive search and preserving tags are the conditions.

#!/bin/bash

mkdir ${HOME}/ogg
mkdir ${HOME}/mp3
rm -rfv ${HOME}/.Trash/*

cd ${HOME}
find . -iname "*.mp3" | while read file
do cp -ruvx --parents "$file" ${HOME}/mp3
done

cd ${HOME}/mp3
find . -iname "*.mp3" | while read file

ARTIST=`mp3info -p "%a" "$file"`
LABEL=`mp3info -p "%l" "$file"`
TRACK=`mp3info -p "%n" "$file"`
TITLE=`mp3info -p "%t" "$file"`
GENRE=`mp3info -p "%g" "$file"`
YEAR=`mp3info -p "%y" "$file"`

do exec lame --decode "${file}" - | oggenc --resample 22050 -a "$ARTIST" -t "$TITLE" -l "$LABEL" -G "$GENRE" -d "$YEAR" -N "$TRACK" -o "${file/%mp3/ogg}" -
done

cd ${HOME}/mp3
find . -iname "*.ogg" | while read file
do cp -ruvx --parents "$file" ${HOME}/ogg
done

cd ${HOME}
rm -rv ${HOME}/mp3/
rm -rfv ${HOME}/.Trash/*

Sepero 03-08-2007 08:18 PM

ogg is great and all, but I hope you realize mp3 and ogg are both lossy compression formats. It is better to rip from the originals.

cfaj 03-08-2007 09:23 PM

Quote:

Originally Posted by True`Colors
I took some ideas from others scripts on the net, and made a script to suit my needs. Please give some help and tips for improvemets, tanks. Recursive search and preserving tags are the conditions.

#!/bin/bash

mkdir ${HOME}/ogg
mkdir ${HOME}/mp3

You only need one call to mkdir:

Code:

mkdir "${HOME}/ogg" "${HOME}/mp3"
Quote:

rm -rfv ${HOME}/.Trash/*

cd ${HOME}
find . -iname "*.mp3" | while read file
do cp -ruvx --parents "$file" ${HOME}/mp3
done
You don't need the while loop. If you are using a recent version of find:

Code:

find . -iname "*.mp3" -exec cp -ruvx --parents {} ${HOME}/mp3" +
If your version of find doesn't accept '+', replace it with '\;'; the plus sign will be faster.

Quote:

cd ${HOME}/mp3
find . -iname "*.mp3" | while read file

ARTIST=`mp3info -p "%a" "$file"`
LABEL=`mp3info -p "%l" "$file"`
TRACK=`mp3info -p "%n" "$file"`
TITLE=`mp3info -p "%t" "$file"`
GENRE=`mp3info -p "%g" "$file"`
YEAR=`mp3info -p "%y" "$file"`
You only need to call mp3info once, and it should be after 'do':

Code:

do
  eval "`mp3info -p "ARTIST='%a' LABEL='%l' TRACK='%n'
                  TITLE='%t' GENRE='%g' YEAR='%y'" "$file"`"

Quote:

do exec lame --decode "${file}" - | oggenc --resample 22050 -a "$ARTIST" -t "$TITLE" -l "$LABEL" -G "$GENRE" -d "$YEAR" -N "$TRACK" -o "${file/%mp3/ogg}" -
done

cd ${HOME}/mp3
find . -iname "*.ogg" | while read file
do cp -ruvx --parents "$file" ${HOME}/ogg
done
Why don't you build the output filename to include the $HOME/ogg directory:

Code:

do
  eval "`mp3info -p "ARTIST='%a' LABEL='%l' TRACK='%n'
                  TITLE='%t' GENRE='%g' YEAR='%y'" "$file"`"
  ofile=${file#./}
  lame --decode "${file}" - |
    oggenc --resample 22050 -a "$ARTIST" -t "$TITLE" \
    -l "$LABEL" -G "$GENRE" -d "$YEAR" -N "$TRACK" \
    -o "$HOME/ogg/${ofile%[Mm][Pp]3}ogg" -
done

Quote:

cd ${HOME}
rm -rv ${HOME}/mp3/
rm -rfv ${HOME}/.Trash/*

Sepero 03-09-2007 02:45 AM

True Colors, thanks for the original script.

True`Colors 03-09-2007 04:51 AM

==========
#!/bin/bash

mkdir "${HOME}/ogg" "${HOME}/mp3"
rm -rfv ${HOME}/.Trash/*

cd ${HOME}
find . -iname "*.mp3" -exec cp -ruvx --parents {} ${HOME}/mp3 \;

cd ${HOME}/mp3
find . -iname "*.mp3" | while read file

do
eval "`mp3info -p "ARTIST='%a' LABEL='%l' TRACK='%n'
TITLE='%t' GENRE='%g' YEAR='%y'" "$file"`"
ofile=${file#./}
lame --decode "${file}" - |
oggenc --resample 22050 -a "$ARTIST" -t "$TITLE" \
-l "$LABEL" -G "$GENRE" -d "$YEAR" -N "$TRACK" \
-o "$HOME/ogg/${ofile%[Mm][Pp]3}ogg" -
done

cd ${HOME}
rm -rv ${HOME}/mp3/
rm -rfv ${HOME}/.Trash/*
==========

Thanks cfaj, wellcome Sepero.
How can i skip a bad mp3 file in the transcoding process? Some strange message that i have encountered: "Error: sample frequency has changed in MP3 file - not supported".

True`Colors 06-06-2007 12:06 AM

mp3toogg.sh
 
Just a little modification to do that i need at least for Fedora 7:

==========

#!/bin/bash

mkdir "${HOME}/ogg" "${HOME}/mp3"
rm -rfv ${HOME}/.Trash/*

cd ${HOME}
find . -iname "*.mp3" -exec cp -ruvx --parents {} ${HOME}/mp3 \;

cd ${HOME}/mp3
find . -iname "*.mp3" | while read file

do
eval "`mp3info -p "ARTIST='%a' LABEL='%l' TRACK='%n'
TITLE='%t' GENRE='%g' YEAR='%y'" "$file"`"
ofile=${file}
lame --decode "${file}" - |
oggenc --resample 22050 -a "$ARTIST" -t "$TITLE" \
-l "$LABEL" -G "$GENRE" -d "$YEAR" -N "$TRACK" \
-o "$HOME/ogg/${ofile%[Mm][Pp]3}ogg" -
done

cd ${HOME}
rm -rv ${HOME}/mp3/
rm -rfv ${HOME}/.Trash/*

==========

True`Colors 07-20-2007 09:54 AM

mp3toogg.sh
 
==========

#!/bin/bash

rm -rfv ${HOME}/.Trash/*
mkdir "${HOME}/ogg"

cd ${HOME}
find . -iname "*.mp3" | while read file

do
eval "`mp3info -p "ARTIST='%a' LABEL='%l' TRACK='%n'
TITLE='%t' GENRE='%g' YEAR='%y'" "$file"`"

ofile=${file}

lame --decode "${file}" - |
oggenc --resample 22050 -a "$ARTIST" -t "$TITLE" \
-l "$LABEL" -G "$GENRE" -d "$YEAR" -N "$TRACK" \
-o "$HOME/ogg/${ofile%[Mm][Pp]3}ogg" -
done

==========


All times are GMT -5. The time now is 06:03 AM.