LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2007, 03:39 AM   #1
True`Colors
LQ Newbie
 
Registered: Mar 2007
Location: Urziceni - Romania
Distribution: Fedora 16
Posts: 12

Rep: Reputation: 0
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/*
 
Old 03-08-2007, 08:18 PM   #2
Sepero
Member
 
Registered: Jul 2004
Location: Tampa, Florida, USA
Distribution: Ubuntu
Posts: 734
Blog Entries: 1

Rep: Reputation: 33
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.
 
Old 03-08-2007, 09:23 PM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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/*
 
Old 03-09-2007, 02:45 AM   #4
Sepero
Member
 
Registered: Jul 2004
Location: Tampa, Florida, USA
Distribution: Ubuntu
Posts: 734
Blog Entries: 1

Rep: Reputation: 33
True Colors, thanks for the original script.
 
Old 03-09-2007, 04:51 AM   #5
True`Colors
LQ Newbie
 
Registered: Mar 2007
Location: Urziceni - Romania
Distribution: Fedora 16
Posts: 12

Original Poster
Rep: Reputation: 0
==========
#!/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".
 
Old 06-06-2007, 12:06 AM   #6
True`Colors
LQ Newbie
 
Registered: Mar 2007
Location: Urziceni - Romania
Distribution: Fedora 16
Posts: 12

Original Poster
Rep: Reputation: 0
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/*

==========
 
Old 07-20-2007, 09:54 AM   #7
True`Colors
LQ Newbie
 
Registered: Mar 2007
Location: Urziceni - Romania
Distribution: Fedora 16
Posts: 12

Original Poster
Rep: Reputation: 0
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

==========
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
quick bash script to convert flac to mp3 zuralin Programming 8 08-19-2011 03:50 AM
Speaking of .ogg vs .mp3 - .mp3 format now the center of legal battles Shadoglare Linux - News 1 04-16-2007 11:02 AM
MP3 or OGG in C exvor Programming 2 05-20-2006 02:22 PM
mp3 to ogg help bunnyknight13 Linux - General 5 09-25-2005 02:21 AM
mp3 name 'squeezer' bash script ?? purpleburple Linux - General 7 12-12-2002 04:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration