Convert audio files to mp3 using ffmpeg [closed]
Asked Answered
B

15

334

I need to convert audio files to mp3 using ffmpeg.

When I write the command as ffmpeg -i audio.ogg -acodec mp3 newfile.mp3, I get the error:

FFmpeg version 0.5.2, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: 
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  built on Jun 24 2010 14:56:20, gcc: 4.4.1
Input #0, mp3, from 'ZHRE.mp3':
  Duration: 00:04:12.52, start: 0.000000, bitrate: 208 kb/s
    Stream #0.0: Audio: mp3, 44100 Hz, stereo, s16, 256 kb/s
Output #0, mp3, to 'audio.mp3':
    Stream #0.0: Audio: 0x0000, 44100 Hz, stereo, s16, 64 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
Unsupported codec for output stream #0.0

I also ran this command:

 ffmpeg -formats | grep mp3

and got this in response:

FFmpeg version 0.5.2, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: 
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  built on Jun 24 2010 14:56:20, gcc: 4.4.1
 DE mp3             MPEG audio layer 3
 D A    mp3             MP3 (MPEG audio layer 3)
 D A    mp3adu          ADU (Application Data Unit) MP3 (MPEG audio layer 3)
 D A    mp3on4          MP3onMP4
 text2movsub remove_extra noise mov2textsub mp3decomp mp3comp mjpegadump imxdump h264_mp4toannexb dump_extra

I guess that the mp3 codec isn't installed. Am I on the right track here?

Breadstuff answered 15/7, 2010 at 12:55 Comment(0)
B
18

Never mind,

I am converting my audio files to mp2 by using the command:

ffmpeg -i input.wav -f mp2 output.mp3

This command works perfectly.

I know that this actually converts the files to mp2 format, but then the resulting file sizes are the same..

Breadstuff answered 15/7, 2010 at 12:55 Comment(6)
Generates a wave fileBusterbustle
This does not result in a mp3 fileIsauraisbel
While the sizes are the same, these mp2 files are not playable in a web browser, as they are not mp3. Not even with a flash player. The only benefit they have is that they require less cpu to encode, but the drawbacks are not worth it.Casie
This should not be the accepted answer, as it does not answer the question correctly.Skat
To reduce the size use this: superuser.com/a/553049/1623401Zendah
It is a bad idea to suffix mp2 audios as .mp3 because it could be badly detected as mp3.Haws
S
494

You could use this command:

ffmpeg -i input.wav -vn -ar 44100 -ac 2 -b:a 192k output.mp3

Explanation of the used arguments in this example:

  • -i - input file

  • -vn - Disable video, to make sure no video (including album cover image) is included if the source would be a video file

  • -ar - Set the audio sampling frequency. For output streams it is set by default to the frequency of the corresponding input stream. For input streams this option only makes sense for audio grabbing devices and raw demuxers and is mapped to the corresponding demuxer options.

  • -ac - Set the number of audio channels. For output streams it is set by default to the number of input audio channels. For input streams this option only makes sense for audio grabbing devices and raw demuxers and is mapped to the corresponding demuxer options. So used here to make sure it is stereo (2 channels)

  • -b:a 192k - Converts the audio bit-rate to be exact 192 KB/s (192 kibibit per second).

    But maybe use -q:a 2 instead, which allows the encoder to pick from 170 to 210 KB/s quality-range (average 192 KB/s). But -q format may not be compatible with some old player-hardware.

Note to see docs about bit-rate argument's differences. Because maybe that option is the most important one, as it decides the "quality" versus "output size" versus "old mp3-player compatibility".

Where:

  • -b:a is for CBR (constant-bit-rate), which should be compatible with most old players, but may take more file-size.
  • -q:a or -qscale:a alias, is for VBR (variable-bit-rate).
  • --abr is for ABR (adaptive-bit-rate), which is a combo of CBR and VBR modes, but --abr argument needs -b to be passed as well (because ffmpeg does not take any parameters after --abr, unlike lame --abr executable).
Serendipity answered 18/10, 2012 at 10:9 Comment(3)
@apanloco for me, changing -b to -q absolutely butchers the sound. Using no options at all, or using the options presented in the answer, sound virtually the same as the source .wav.Labrum
Cool to batch for f in *.wma; do ffmpeg -i "$f" -vn -ar 44100 -ac 2 -b:a 192k "${f%.*}.mp3"; doneRoundhead
@Roundhead It's important to note that your solution will only work on non-Windows installs. To do this from the Windows command line, you can use this: for %f in (*.wma) do ffmpeg.exe -i "%f" -vn -ar 44100 -ac 2 -b:a 192k "%f.mp3" This will create files for every wma file in the current folder, with the original name and ".mp3" appended to it after the ".wma". E.g. input.wma -> input.wma.mp3Canterbury
R
192
  1. wav to mp3

    ffmpeg -i audio.wav -acodec libmp3lame audio.mp3
    
  2. ogg to mp3

    ffmpeg -i audio.ogg -acodec libmp3lame audio.mp3
    
  3. ac3 to mp3

    ffmpeg -i audio.ac3 -acodec libmp3lame audio.mp3
    
  4. aac to mp3

    ffmpeg -i audio.aac -acodec libmp3lame audio.mp3
    
Rendarender answered 18/5, 2016 at 11:39 Comment(7)
How do you specify the mp3 bitrate?Chamaeleon
Add -b:a 128k for 128 kbps.Cookie
Or even shorter without acodec: ffmpeg -i audio.aac audio.mp3Spermatic
how to convert from .mp3 to .ogg ?Francinafrancine
I use libmp3lame but why its codec on VLC says mp2 (mpeg audio layer 1/2)???Requirement
@Spermatic The -acodec libmp3lame is necessary for me, when transferring from aac to mp3.Colloquial
Is there any solution for audio/mp4 to mp3?Senior
R
37

For batch processing files in folder:

for i in *.wav; do ffmpeg -i "$i" -f mp3 "${i%}.mp3"; done

This script converts all "wav" files in folder to mp3 files and adds mp3 extension

ffmpeg have to be installed. (See other answers)

Recurved answered 18/12, 2016 at 10:28 Comment(2)
With -f mp2 MP2 is generated, not MP3. Change it to -f mp3Cookie
The above command creates files that are named wav.mp3. To get files with the correct file extension, change the command to: for i in *.wav; do ffmpeg -i "$i" -f mp3 "${i%.*}.mp3"; done, i.e. add .* after i%.Aman
C
37

For batch processing with files in folder aiming for 190 VBR and file extension = .mp3 instead of .ac3.mp3 you can use the following code

Change .ac3 to whatever the source audio format is.

ffmpeg mp3 settings

for f in *.ac3 ; do ffmpeg -i "$f" -acodec libmp3lame -q:a 2 "${f%.*}.mp3"; done
Coachwork answered 4/1, 2017 at 11:58 Comment(0)
C
27

As described here input and output extension will detected by ffmpeg so there is no need to worry about the formats, simply run this command:

ffmpeg -i inputFile.ogg outputFile.mp3

Caprice answered 18/10, 2019 at 1:33 Comment(0)
B
18

Never mind,

I am converting my audio files to mp2 by using the command:

ffmpeg -i input.wav -f mp2 output.mp3

This command works perfectly.

I know that this actually converts the files to mp2 format, but then the resulting file sizes are the same..

Breadstuff answered 15/7, 2010 at 12:55 Comment(6)
Generates a wave fileBusterbustle
This does not result in a mp3 fileIsauraisbel
While the sizes are the same, these mp2 files are not playable in a web browser, as they are not mp3. Not even with a flash player. The only benefit they have is that they require less cpu to encode, but the drawbacks are not worth it.Casie
This should not be the accepted answer, as it does not answer the question correctly.Skat
To reduce the size use this: superuser.com/a/553049/1623401Zendah
It is a bad idea to suffix mp2 audios as .mp3 because it could be badly detected as mp3.Haws
J
12

I had to purge my ffmpeg and then install another one from a ppa:

sudo apt-get purge ffmpeg
sudo apt-add-repository -y ppa:jon-severinsson/ffmpeg 
sudo apt-get update 
sudo apt-get install ffmpeg

Then convert:

 ffmpeg -i audio.ogg -f mp3 newfile.mp3
Janey answered 9/1, 2015 at 11:54 Comment(1)
this is the short way, but if you want to specify the quality kb per seconds, use -ab 192kFecit
A
5

https://trac.ffmpeg.org/wiki/Encode/MP3

VBR Encoding:

ffmpeg -i input.ogg -vn -ar 44100 -ac 2 -q:a 1 -codec:a libmp3lame output.mp3
Acarology answered 28/6, 2018 at 13:20 Comment(1)
You forgot the input.Weig
F
5

No one seems to use find, which let you do everything on one line. Based on this answer and this post:

find . -type f -iname "*.webm" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -vn -ab 128k "${FILE%.webm}.mp3";' _ '{}' \;

For e.g. podcasts, 128k is enough for me. You can adjust that argument beside some others:

  • -i - input file.
  • -vn - Disable video, to make sure no video (including album cover image) is included if the source would be a video file.
  • -ar - Set the audio sampling frequency. For output streams it is set by default to the frequency of the corresponding input stream. For input streams this option only makes sense for audio grabbing devices and raw demuxers and is mapped to the corresponding demuxer options.
  • -ac - Set the number of audio channels. For output streams it is set by default to the number of input audio channels. For input streams this option only makes sense for audio grabbing devices and raw demuxers and is mapped to the corresponding demuxer options. So used here to make sure it is stereo (2 channels).
  • -b:a - Converts the audio bitrate to be exact 192kbit per second.
Friede answered 13/12, 2021 at 18:52 Comment(0)
S
3

High quality for Mac OS works perfectly!

ffmpeg -i input.wma -q:a 0 output.mp3

Savory answered 22/12, 2018 at 9:0 Comment(0)
N
3

I will explain how to convert webm to mp3 for macs, I guess for linux it also works.

  1. Install ffmpeg - brew install ffmpeg (mac) or sudo apt install ffmpeg (linux)
  2. Create shell script - Open text editor put the following code inside:
#!/bin/bash

echo webm to mp3 converter! Work begins! 
for FILE in *.webm; do     
    echo -e "Processing file '$FILE'";
    ffmpeg -i "${FILE}" -vn -ab 128k -ar 44100 -y "${FILE%.webm}.mp3";
done;

this code will look all files with .webm extension in current directory.

  1. Save this file without extension (for example "my-converter")
  2. Navigate to created file via terminal
  3. Make the file executabe by typing command: chmod 700 my-converter, now in the same directory the unix executable file (.sh) will be created.
  4. Execute the file from terminal by typing: ./my-converter and the process begins, you will see the progress in the terminal window.

Done.

Nims answered 13/4, 2021 at 6:22 Comment(0)
R
1

Try FFmpeg Static Build Link

Documentation: https://www.johnvansickle.com/ffmpeg/

Host the static build on your server in same directory

$ffmpeg = dirname(__FILE__).'/ffmpeg';

$command = $ffmpeg.'ffmpeg -i audio.ogg -acodec libmp3lame audio.mp3';

shell_exec($command);
Revanche answered 26/1, 2018 at 14:17 Comment(0)
V
1

If you have a folder and sub-folder full of wav's you want to convert, put below command in a file, save it in a .bat file in the root of the folder where you wan to convert, and then run the bat file

for /R %%g in (*.wav) do start /b /wait "" "C:\ffmpeg-4.0.1-win64-static\bin\ffmpeg" -threads 16 -i "%%g" -acodec libmp3lame "%%~dpng.mp3" && del "%%g"
Veal answered 11/7, 2018 at 19:52 Comment(0)
P
0

Using the previous answers, here is an alias for this by adding the following into .bashrc/.zshrc:

alias convert-aac="cd ~/Downloads && aac-to-mp3"

# Convert all .aac files into .mp3 files in the current folder, don't convert if a mp3 file already exists
aac-to-mp3(){
    find . -type f -iname "*.aac" -exec \
        bash -c 'file="$1"; ffmpeg -n -i "$file" -acodec libmp3lame "${file%.aac}.mp3";' _ '{}' \;
}

Usage: convert-aac (in shell)

Thanks to https://mcmap.net/q/98287/-convert-audio-files-to-mp3-using-ffmpeg-closed and https://mcmap.net/q/98287/-convert-audio-files-to-mp3-using-ffmpeg-closed and https://unix.stackexchange.com/a/683488/60329

Prosthetics answered 22/12, 2021 at 13:27 Comment(0)
V
0
for file in *.wma; do ffmpeg -i "${file}"  -acodec libmp3lame -ab 192k "${file/.wma/.mp3}"; done
Veterinary answered 16/4, 2023 at 3:18 Comment(2)
Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?Crossstaff
This is the only answer that mentions a specific combination of -acodec and -ab arguments that worked for me.Angelikaangelina

© 2022 - 2025 — McMap. All rights reserved.