WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Convert Ogg to Mp3 with ffmpeg?  (Read 7485 times)

Offline samweller

  • Newbie
  • *
  • Posts: 16
Convert Ogg to Mp3 with ffmpeg?
« on: November 11, 2009, 09:04:18 PM »
I've downloaded a bunch of stuff that says ffmpeg and mp3 hoping I have the right stuff.  My command is
Code: [Select]
ffmpeg -i soundpollution.ogg soundpollution.mp3
well it appears I can't cut or paste with Aterm.  Anyway it says "unsupported codec for output stream."

Do we have the codec to download to enable this?

Offline perthie

  • Full Member
  • ***
  • Posts: 118
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #1 on: November 11, 2009, 11:04:22 PM »
If you had ogg-vorbis.tczl and lame.tczl, you could use

Code: [Select]
oggdec $1.ogg ; lame $1.wav $1.mp3 ; /bin/rm $1.wav

Offline samweller

  • Newbie
  • *
  • Posts: 16
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #2 on: November 12, 2009, 01:25:12 AM »
Yep that works.  Thanks.  Seems a little long to have to do for about 100 songs.  But that might have to do.  Thankyou for the help.

Offline perthie

  • Full Member
  • ***
  • Posts: 118
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #3 on: November 12, 2009, 02:12:12 AM »
Seems a little long to have to do for about 100 songs.

Then how about:
Code: [Select]
for X in *.ogg ; do
  Y=${X%.*}
  oggdec $Y.ogg ; lame $Y.wav $Y.mp3 ; /bin/rm $Y.wav
done


Offline samweller

  • Newbie
  • *
  • Posts: 16
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #4 on: November 12, 2009, 12:11:36 PM »
perthie-

Thanks for the response.  I've been looking at it and can't quite figure what to plug in to the variables.  All my music is at /mnt/hda4/mu51c.  Then inside the mu51c folder are folders of bands and the songs themselves are in those.  Does Y=./mu51c?

Offline mikshaw

  • Sr. Member
  • ****
  • Posts: 368
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #5 on: November 12, 2009, 03:06:29 PM »
The variables are automatically populated according to whether or not there are *.ogg files in the current directory.

for X in *.ogg <-- this sets X to the name of an ogg file in the current directory.  The loop repeats once for each ogg file.
Y=${X%.*} <-- This sets Y to the value of $X without the filename extension.

This script works only for a single directory, so you'd need to cd to the artist's folder, run the script, cd to the next artist directory and run the script, etc.

If you want to do this automatically from mu51c you'll need to nest this loop in another one.

I haven't tested, but it would be something like this....

Code: [Select]
for D in /mnt/hda4/mu51c/*; do
if [ -d "$D"]; then # if it's a subdirectory in mu51c
cd "$D"
for X in *.ogg ; do
  Y=${X%.*}
  oggdec $Y.ogg ; lame $Y.wav $Y.mp3 ; /bin/rm $Y.wav
done
fi
done
If this works, it should also work from any directory, so you wouldn't need to cd to /mnt/hda4/mu51c first.
« Last Edit: November 12, 2009, 03:14:31 PM by mikshaw »

Offline samweller

  • Newbie
  • *
  • Posts: 16
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #6 on: November 12, 2009, 04:20:32 PM »
mikshaw the results were

Code: [Select]
sh: [d-: not found
By the way, can you cut and paste in Aterm?  I have no middle button.

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #7 on: November 12, 2009, 06:20:17 PM »
@samweller:
looks like you've entered something like
Code: [Select]
[d- which is not the same as
Code: [Select]
[ -d
Please also note the space between the [ and the -d, that is another thing to be mindful of.

Furthermore I'd like to share a general piece of advice since I see an awful lot of for loops showing up in this forum here. Sample code:
Code: [Select]
for VAR in *.ext ; do ..... done When one uses an expression like *.ogg in a shell command the shell is going to expand this expression and is going to textually replace the *.ogg with all the file names that match this term. That process is called globbing and it's fine as long as you can be sure that there are only a limited number of files that match. But if there are a lot of file names the result can exceed the maximum length of a command that the shell can handle. I'm talking of experience here when I say that the outcome can be quite nasty. At best the command will just fail, but depending on how the loop is constructed I've seen pretty poor outcomes (e.g. files lost etc.)

So what's the solution? Simple: use a while loop instead. Sample code:
Code: [Select]
ls | grep '\.ext$' | while read VAR ; do ..... done The pipe ensures that no matter how many iterations it won't cause the aforementioned problems.

Furthermore, in your case assuming you want to convert all files "below" a certain direrectory, why not use find instead? I would probably do:
Code: [Select]
find /mnt/hda4/mu51c -type f -name *.ogg | while read FILE ; do
        BASE=${FILE%.*}
        oggdec ${BASE}.ogg ; lame ${BASE}.wav ${BASE}.mp3 ; /bin/rm -f ${BASE}.wav
done

If you do any of these loops and you are not sure if it will work, I'd suggest to test it out on a smaller subset. To achieve this you could use
Code: [Select]
find /mnt/hda4/mu51c -type f -name *.ogg | head -5 | while read FILE ; do to test the loop but limited to the first 5 files.

I can't give you the exact hint on how to solve you copy&paste problem, but the middle button can be emulated by pressing both buttons at the same time. I'd therefore suggest to use xsetup.sh and try out the different 2 button mouse types. This changes the Xvesa parameter in your .xsession file. You will have to restart X (by performing an 'Exit to prompt' and using the startx command). Also depending on your setup you'll have to ensure that the change "survives" you next reboot.

Offline combo3

  • Full Member
  • ***
  • Posts: 148
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #8 on: November 12, 2009, 08:07:25 PM »
mikshaw the results were
By the way, can you cut and paste in Aterm?  I have no middle button.

Highlight the text you want to copy and then press Shift+Insert to paste in the terminal.

Offline mikshaw

  • Sr. Member
  • ****
  • Posts: 368
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #9 on: November 12, 2009, 09:25:25 PM »
@maro

I've heard you can also use the -exec action with the find command to prevent problems rather than using a pipe to while

To be honest, though, I've never seen the problems encountered by yourself and others in using a for loop.  It might be an issue of having filenames with spaces, which can easily break a for loop.  Personally I don't allow spaces in filenames on my system if I can help it, and as I said I've never run into problems with for loops even on directories with thousands of files.

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #10 on: November 13, 2009, 11:38:26 PM »
@mikshaw

I agree that the -exec option for find can also be used, but in my experience is less flexible than a pipe into a 'while read' loop. In some cases I found a pipe into xargs a good solution, but that is fairly close to the -exec option anyway.

Regarding the spaces in file names: that would be an issue for anly kind of loop or xargs construct, but the remedy is to enclose the entire term with the variable in double-quotes (e.g. "${BASE}.ogg").

The point I've tried to make might be seen as a matter of taste and it could well be the use of full length path where each filename ended up being > 80 characters that caused the issue. I just recall that I had nasty cleanup job on my hands when this occured on a production system.
« Last Edit: November 14, 2009, 12:00:59 AM by maro »

Offline samweller

  • Newbie
  • *
  • Posts: 16
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #11 on: November 14, 2009, 12:19:28 AM »
Thanks a million combo3.

Offline ^thehatsrule^

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 1726
Re: Convert Ogg to Mp3 with ffmpeg?
« Reply #12 on: November 18, 2009, 01:34:03 AM »
IIRC `find -exec` is generally faster than using pipe+xargs, and that pipe+xargs is generally faster than using shell loops, ie 'for'.

There shouldn't be any problems with spaces if `while read` is used, but for loops will.  This is controlled by $IFS, i.e.
Code: [Select]
IFS="
"
Hm, this is a bit OT.