LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   converting m4a to mp3 (https://www.linuxquestions.org/questions/linux-general-1/converting-m4a-to-mp3-170553/)

scotth20 12-29-2006 05:29 PM

script to convert m4a, wma or ogg to mp3
 
This script will convert all files with the given extension to mp3 files:

usage tomp3.sh wma (converts all wma files to mp3)
This script needs mplayer, lame and transcode to work, so you will
need the appropriate packages.

Just put this in your executable path somewhere....

-------------------------------------------------------------
remove_spaces=1

tmp_file="/tmp/pcm_audio"

function rm_spaces() {
mv "$1" `echo $1 | tr ' ' '_'`
}

function tomp3() {
ext=$1

for i in *.$ext
do
if [ -f "$i" ]; then
dest=`basename "$i" .$ext`

if [ "$ext" = "ogg" ]; then
echo "Encoding $i to "$dest".mp3"
transcode -q 0 -i "$i" -o "$dest" -y null,lame 2>/dev/null
else
rm -f "$tmp_file"
mkfifo "$tmp_file"

#echo "Ripping $i"
mplayer -quiet -vo null -vc dummy -af volume=0,resample=44100:0:1 -ao pcm:waveheader:file="$tmp_file" "$i" &>/dev/null &

echo "Encoding $i to "$dest".mp3"
lame --quiet -m s -h -b 192 -V0 --vbr-new "$tmp_file" "$dest".mp3

# remove temp file
rm -f "$tmp_file"

fi
# remove spaces
if [ $remove_spaces -eq 1 ]
then
rm_spaces "$dest".mp3
fi
fi
done

echo "done."
}

case "$1" in
ogg)
tomp3 ogg
;;
wma)
tomp3 wma
;;
m4a)
tomp3 m4a
;;
*)
echo "Usage: tomp3.sh m4a|wma|ogg"
exit 1
esac

exit 0

josephmc 01-24-2007 01:39 PM

great script! you don't really need to move the files at the end. i just let the filename stay the same. if you do change it you need to make sure trakes < 10 have a 0 in front of it for sorting. has ANYBODY found a real configurable script for the written in perl or something of the like?

ungua 01-25-2007 07:46 AM

i haven't tried the script yet, but would like to do that soon. still, i'd prefer a program with interface. can anyone provide that!? :)

regards
ungua

scm86 02-19-2007 11:14 AM

If you haven't used one of the scripts yet, you should try the first one. There is no need for a GUI, all you have to do is fire up the script and let it crawl through your files, making them into mp3's.

I had to modify the script to get it to transcode my m4a's, but that was a 30 second find/replace with kwrite, otherwise is the exact same as was posted above.

http://mlinks.net/~scottm/m4a_to_mp3.sh

To use it:
0) Save the file above.
1) Place it in the same folder as the audio files you wish to transcode, or copy them into the folder containing the script.
2) From a terminal as normal user: chmod +x m4a_to_mp3.sh
3) Still in the terminal: ./m4a_to_mp3.sh
4) Sit back and watch, then enjoy your mp3's.

Note that this does replace the m4a files, so if you want to keep them as m4s'a as well, backup them up somewhere.

Hope this helps someone, the script was really handy to me!

Scott McNeely

sblob 07-07-2007 03:58 AM

SteelJ, thank you very much for all of the time that you spent on the script. I have used it for the basis for another script which handles recursively walking a directory and alternative directory names. If anyone is interested I have posted it:
(sorry I would have posted it here but it got very long)

sblob 07-07-2007 03:58 AM

(Sorry about the double post, limitation of the site prevented me from posting the URL)

SteelJ, thank you very much for all of the time that you spent on the script. I have used it for the basis for another script which handles recursively walking a directory and alternative directory names. If anyone is interested I have posted it:
http://www.minigeek.org/index.php/20...ript/#more-116

(sorry I would have posted it here but it got very long)

Steel_J 07-07-2007 07:18 PM

Thanks man,

I dowloaded it and will use this one instead of the old one.

XavierP 07-08-2007 08:58 AM

Quote:

Originally Posted by sblob
(Sorry about the double post, limitation of the site prevented me from posting the URL)

Feature of the site. It is by design rather than by accident.

love mp3 10-22-2007 09:41 PM

I have used a similar tool called M4A to MP3 Converter whihc work on windows, but not on linux.

hoodooman 11-27-2007 04:45 AM

You could just install libmp4 and ecasound.If you want to convert to .mp3 just run in the folder containing the files ecaconvert .mp3 *.mp4.Simple.Change the .mp3 to .wav .flac .ogg depending on what output you need.

mmatt 11-27-2007 09:56 AM

Lengthy discussion!
 
Wow, I didn't realise this thread was still active. Back when it started I'm not sure there were any tools to do this easily, hence the scripts.

Out of interest, do those tools transfer the tags as well as the data? And will they cope with badly formed tag fields?

hoodooman 11-28-2007 03:22 AM

As far as I know ecasound doesnt copy the tags over.It may well do if you looked at the man for it but as I only use it sometimes this isnt a problem for me.It would be a perfect app if it did that too.

mmatt 11-28-2007 09:42 AM

For large collections...
 
I see. Handy tool for the occasional file or two then, I'll remember that.

For the benefit of future readers then, If you want to convert you're entire (for example iTunes) music collection from m4a to mp3, keeping all the tags, you may still need to do it the hard way. Or should I say less convenient way; it's not really hard, especially as there are now two or three different scripts to choose from in this thread.

Enjoy your music.

nobodysbusiness 01-16-2008 09:29 PM

I just wrote a program to convert M4As to MP3s, transferring the artist, album, song name and track number tags. It's available here. Drop a comment if it breaks or there are some tags you want that aren't transferred.

akamikeym 05-15-2008 07:24 AM

Usage preference
 
Hi,

I've taken the scripts from this post and changed the user interface a little to work in what I would consider a more acceptable manner.

It takes a list of m4a files (including wildcards) and converts them into mp3's and dumps them in the working directory.

Code:

#!/bin/bash
#
# Dump m4a to mp3

if [ "$#" -eq "0" ] ; then
        echo "Usage: '$0 filenames'"
        exit 1
fi
for i in "$@"
do
        echo Editing file - $i

        if [ -f "$i" ]; then
                faad "$i"
                # Get the source wav file produced by faad
                x=`echo "$i"|sed -e 's/.m4a/.wav/'`
                # Get the destination filename including path
                y=`echo "$i"|sed -e 's/.m4a/.mp3/'`
                # Strip pathso it's put in the current directory
                y=`echo ${y##*/}`
                # Get the track info
                faad -i "$i" 2>.trackinfo.txt
                title=`grep 'title: ' .trackinfo.txt|sed -e 's/title: //'`
                artist=`grep 'artist: ' .trackinfo.txt|sed -e 's/artist: //'`
                album=`grep 'album: ' .trackinfo.txt|sed -e 's/album: //'`
                genre=`grep 'genre:*/ ' .trackinfo.txt|sed -e 's/genre: //'`
                track=`grep 'track: ' .trackinfo.txt|sed -e 's/track: //'`
                year=`grep 'year: ' .trackinfo.txt|sed -e 's/year: //'`
                # Convert the file using lame
                lame --alt-preset 160 --tt "$title" --ta "$artist" --tl "$album" --tg "$genre" --tn "$track" --ty "$year" "$x" "$y"
                # Remove temporary files
                rm .trackinfo.txt
                rm "$x"
        fi
done


Steel_J 05-15-2008 01:52 PM

Thank will try it

kah00na 08-07-2008 03:29 PM

One more suggestion for all the scripts
 
Instead of removing the m4a files or just leaving them, they could just as easily be moved to the trash. If the file does get hosed up, you could still retrieve it from the trash. Here's the command - just remove the "rm $x" or whatever variable name you end up using.

Code:

mv "$i" ~/.local/share/Trash/files
I modified my script to do this and it sure is nice to not to actually lose the m4a files. Just remember to empty your trash occasionally! :)

kah00na 08-07-2008 03:31 PM

Heres that last script updated to move the m4a files to the trash:

Code:

#!/bin/bash
#
# Dump m4a to mp3

if [ "$#" -eq "0" ] ; then
        echo "Usage: '$0 filenames'"
        exit 1
fi
for i in "$@"
do
        echo Editing file - $i

        if [ -f "$i" ]; then
                faad "$i"
                # Get the source wav file produced by faad
                x=`echo "$i"|sed -e 's/.m4a/.wav/'`
                # Get the destination filename including path
                y=`echo "$i"|sed -e 's/.m4a/.mp3/'`
                # Strip pathso it's put in the current directory
                y=`echo ${y##*/}`
                # Get the track info
                faad -i "$i" 2>.trackinfo.txt
                title=`grep 'title: ' .trackinfo.txt|sed -e 's/title: //'`
                artist=`grep 'artist: ' .trackinfo.txt|sed -e 's/artist: //'`
                album=`grep 'album: ' .trackinfo.txt|sed -e 's/album: //'`
                genre=`grep 'genre:*/ ' .trackinfo.txt|sed -e 's/genre: //'`
                track=`grep 'track: ' .trackinfo.txt|sed -e 's/track: //'`
                year=`grep 'year: ' .trackinfo.txt|sed -e 's/year: //'`
                # Convert the file using lame
                lame --alt-preset 160 --tt "$title" --ta "$artist" --tl "$album" --tg "$genre" --tn "$track" --ty "$year" "$x" "$y"
                # Remove temporary files
                rm .trackinfo.txt
                rm "$x"
                mv "$i" ~/.local/share/Trash/files
        fi
done


kah00na 08-07-2008 03:35 PM

Add to Right-Click Scripts menu
 
Also, since this script is reading files as arguments, you can add a link to your ~/.gnome2/nautilus-scripts directory then when you right click a file, under "Scripts" will be your script. You can then click on it and it will do the conversion without having to open a terminal window. There won't be any progress information, but you can just watch for your CPU to drop off. Here's the command to make the link:

Code:

ln -s /path/to/your/script ~/.gnome2/nautilus-scripts
This right-click menu has saved me a lot of time.

slopshid 10-19-2008 01:33 PM

If anyone's interested at all - instead of having to explicitly use sed to strip the extraneous extensions from the mp3, you can use ${i%.m4a}.wav, etc.:
Code:

#!/bin/bash
#
# Dump m4a to mp3

if [ "$#" -eq "0" ] ; then
        echo "Usage: '$0 filenames'"
        exit 1
fi
for i in "$@"
do
        echo Editing file - $i

        if [ -f "$i" ]; then
                faad "${i%.m4a}.wav"
                # Get the source wav file produced by faad
                x=`echo "${i%.m4a}.wav"
                # Get the destination filename including path
                y=`echo "${i%.m4a}.mp3"
                # Strip pathso it's put in the current directory
                y=`echo ${y##*/}`
                # Get the track info
                faad -i "$i" 2>.trackinfo.txt
                title=`grep 'title: ' .trackinfo.txt|sed -e 's/title: //'`
                artist=`grep 'artist: ' .trackinfo.txt|sed -e 's/artist: //'`
                album=`grep 'album: ' .trackinfo.txt|sed -e 's/album: //'`
                genre=`grep 'genre:*/ ' .trackinfo.txt|sed -e 's/genre: //'`
                track=`grep 'track: ' .trackinfo.txt|sed -e 's/track: //'`
                year=`grep 'year: ' .trackinfo.txt|sed -e 's/year: //'`
                # Convert the file using lame
                lame --alt-preset 160 --tt "$title" --ta "$artist" --tl "$album" --tg "$genre" --tn "$track" --ty "$year" "$x" "$y"
                # Remove temporary files
                rm .trackinfo.txt
                rm "$x"
                mv "$i" ~/.local/share/Trash/files
        fi
done


rocksniffer 12-12-2008 08:49 PM

p0g0, that script of yours is the only one I could get to work and it works great!

elektronaut 05-10-2009 10:01 AM

Doing it with recursion
 
I found a way to convert ALL m4a's in my music collection recursively. Haven't been looking at this thread for a while, there's another suggestion to do the recursion, but mine gives you the opportunity to check if everything went well. There are two parts, first the script for recursively working in all directories with m4a files inside, then the conversion script which is mostly the same as the one on http://wiki.linuxquestions.org/wiki/.m4a_to_.ogg

For the recursion and logfiles:
Code:

#!/bin/bash
# m4a2ogg_r.sh
# converts m4a audio files to ogg files recursively

INPUTFILES=~/m4a2ogg_in.txt
EXPECTED_OUT=~/m4a2ogg_exp.txt
OUTPUTFILES=~/m4a2ogg_out.txt
DIFFRESULT=~/m4a2ogg_diff.txt
TEMP=~/m4a2ogg.tmp
find . -type f -iname '*.m4a' | sed "s+\.+$PWD+" > "$INPUTFILES"
cat "$INPUTFILES" | sed -e 's/.m4a/.ogg/' > "$EXPECTED_OUT"
if [ -f "$OUTPUTFILES" ]
then
  rm "$OUTPUTFILES"
fi
touch "$OUTPUTFILES"
LASTDIR=""
while read LINE ;
do
  DIRNAME=`dirname "${LINE}"`
  if [ "${DIRNAME}" != "${LASTDIR}" ]
  then
    cd "${DIRNAME}"
    m4a2ogg | grep 'encoded' | sed "s;encoded ;${DIRNAME}/;" >> "$OUTPUTFILES"
  fi
  LASTDIR="${DIRNAME}"
done < $INPUTFILES
cat "$EXPECTED_OUT" | sort > "$TEMP" && mv "$TEMP" "$EXPECTED_OUT"
cat "$OUTPUTFILES" | sort > "$TEMP" && mv "$TEMP" "$OUTPUTFILES"
diff "$EXPECTED_OUT" "$OUTPUTFILES" > "$DIFFRESULT"
exit 0

My adaption of the conversion script, successful output is now echoed to stdout so that it can be used for the logging files.
Code:

#!/bin/bash
#
# m4a2ogg, converts m4a files to ogg files
# based on http://wiki.linuxquestions.org/wiki/.m4a_to_.ogg
# see also http://www.linuxquestions.org/questions/linux-general-1/converting-m4a-to-mp3-170553/

for i in *.m4a; do
  tmp=$(mktemp)
  y=`echo "$i"|sed -e 's/.m4a/.ogg/'`
  faad -i "$i" 1>/dev/null 2>"$tmp"
  if [ $? -ne 0 ] ; then
      rm "$tmp"
      echo "failed to get information from $i"
      continue
  fi
  title=`grep 'title: ' "$tmp"|sed -e 's/title: //'`
  artist=`grep 'artist: ' "$tmp"|sed -e 's/artist: //'`
  album=`grep 'album: ' "$tmp"|sed -e 's/album: //'`
  genre=`grep 'genre: ' "$tmp"|sed -e 's/genre: //'`
  track=`grep 'track: ' "$tmp"|sed -e 's/track: //'`
  year=`grep 'year: ' "$tmp"|sed -e 's/date: //'`
  faad "$i" -o - | oggenc -q 5 -t "$title" -a "$artist" -l "$album" -G "$genre" -N "$track" -d "$year" -o "$y" -
  if [ $? -ne 0 ] ; then
      echo "failed to encode $i"
  else
      echo "encoded $y"
  fi
  rm "$tmp"
done

After checking that everything went well, you can delete the m4a files with a little script (or you choose the other solution of moving them into Trash, given a few posts earlier):
Code:

#!/bin/bash
# this deletes all files whose paths occur
# in the text file given as the parameter

while read LINE ;
do
  rm "$LINE"
done < $1
exit 0

After all this I found out that soundKonverter exists. Well, now I can do this on the command line and have exercised again a bit in shell scripting...

Steel_J 05-11-2009 12:09 PM

Wow, I first posted a script version to this thread 3 years ago, hahaha!

Cool!

Glad to see the scripts evolving. I'll try this last one.

IpV8 10-23-2010 08:13 PM

Quality
 
Did anyone else notice that all these methods GREATLY reduce the sound quality of the files? Maybe I just have picky ears, but I find it rather frustrating that there isn't a way to convert m4a's without loss of quality.

Steel_J 10-23-2010 09:09 PM

True
 
Any conversion reduces quality my friend. It is a price you chose to pay for getting out of proprietary formats.

jalirious 10-27-2010 07:18 AM

soundconverter used to batch convert m4a ->ogg, mp3.

maigeleid 06-24-2013 12:21 AM

It's easy to convert m4a to mp3
 
Hi, jonfa,
Just use this program:NoteBurner Audio Converter, it's able to remove music DRM protection from iTunes M4P, M4B, M4A files, Some simple steps for you to have mp3 files.

onebuck 06-24-2013 09:39 AM

Moderator Response
 
Quote:

Originally Posted by maigeleid (Post 4977380)
Hi, jonfa,
Just use this program:NoteBurner Audio Converter, it's able to remove music DRM protection from iTunes M4P, M4B, M4A files, Some simple steps for you to have mp3 files.

Please do not resurrect old necro threads(3 year is bit old). 'jonfa' has not accessed since: 08-05-08 12:24. Your info is valid for a current install so I will let this pass.

Please look at the post/thread dates and consider the age will most likely not get a response but a user searching may benefit if the suggestions are current or relative to the original query.

BTW, Welcome to LQ!


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