[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post tutorials, HOWTO's and other useful resources here.
User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#1 by Rava » 13 Sep 2022, 22:51

Maybe you also have the need for occasionally convert sound files from mp4/mpv/WebM fortats into mp3.

TL;DR version
use

Code: Select all

ffmpeg -v error -i INFILE -b:a 128K -vn OUTFILE.mp3
(adjust the bit rate accordingly to the one in your source file. mp3con.sh always uses 320 kb/s regardless of what the source file has - often resulting in unneeded large target file while gaining nothing from that (what is lost due to smaller bitrate cannot be regained by converting into another file using larger bitrate)
Use

Code: Select all

ffprobe -hide_banner INFILE
to get the info on your multimedia file - in this case, the bitrate.

Know that "-v error" suppresses all output that are not errors (like the banner) - when you want to see all that omit "-v error" e.g. by using simply

Code: Select all

ffmpeg -i INFILE -b:a 128K -vn OUTFILE.mp3
____________________________________________________

Long version with the weirdness that is mp3con.sh:
I found a script that does that and it needs only ffmpeg and lame - both are already installed on your Port 4.* or 5.0 system.

Get the script via

Code: Select all

wget https://raw.githubusercontent.com/jmutai/dotfiles/master/scripts/mp3con.sh
Maybe you want to tweak the script or use parts of it for your own one-liner or your own conversion script.
The website describing it is this: https://computingforgeeks.com/how-to-co ... -on-linux/

What I dislike with mp3con.sh: it asks for the folder to convert, and then converts all mp4/mpv/WebM in that folder, but maybe you only want to convert a certain file, or you already have mp3 versions of some mp4/mpv/WebM files in that directory, mp3con.sh would again convert all the mp4/mpv/WebM files from that folder into mp3.

So, realizing that I created a "mp3con.sh.CONVERT/" folder and only copied one file - a webm - into that I wanted converted - I wanted to see how mp3con.sh does its work prior examining its code and possibly using some snippet for a conversion script - to create one that can be run e.g. as

Code: Select all

mp3convert 1.webm 2.mpv
and thus only convert the named files - or also executed like

Code: Select all

mp3convert .
or

Code: Select all

mp3convert /path/
to convert all files mp4/mpv/WebM in these paths (like mp3con.sh does now, but unlike an envisioned mp3convert mp3con.sh doesn't use or understand any parameters it is given)

EDIT
While mp3con.sh definitely did convert something, the target file is not to be found. Look for yourself:

Code: Select all

guest@porteus:/mysound/WhiteNoise/mp3con.sh.CONVERT$ ls -l
total 56976
-rw-r--r-- 1 guest users 58278950 Feb 15  2022 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.webm
guest@porteus:/mysound/WhiteNoise/mp3con.sh.CONVERT$ mp3con.sh 

1: Current directory
2: Provide directory

Selection : 1
Okay..

Current dir is /mysound/WhiteNoise/mp3con.sh.CONVERT 



ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 11.2.0 (GCC)
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/-4.4.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-libaom --enable-version3 --enable-avresample --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libbluray --enable-libcdio --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-pthreads --enable-postproc --enable-swscale --enable-avfilter --enable-libopus --enable-libspeex --enable-openal --enable-libass --enable-libx264 --enable-libx265 --enable-libgsm --enable-libxvid --enable-libtwolame --enable-libopencore-amrnb --'mp3con.sh.CONVERT-mp3/./Vintage 70s Hoover Convertible ASMR _ Retro White Noise for Sleeping, Focus _ 1 HOUR-gWWCInm-SDw.c1.webm.mp3'enable-libopencore-amrwb --enable-libgsm --enable-nvdec --enable-librtmp
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, matroska,webm, from './Vintage 70s Hoover Convertible ASMR _ Retro White Noise for Sleeping, Focus _ 1 HOUR-gWWCInm-SDw.c1.webm':
  Metadata:
    ENCODER         : Lavf58.76.100
  Duration: 01:03:39.42, start: -0.006000, bitrate: 122 kb/s
  Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 01:03:39.421000000
Stream mapping:
  Stream #0:0 -> #0:0 (opus (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to 'mp3con.sh.CONVERT-mp3/./Vintage 70s Hoover Convertible ASMR _ Retro White Noise for Sleeping, Focus _ 1 HOUR-gWWCInm-SDw.c1.webm.mp3':
  Metadata:
    TSSE            : Lavf58.76.100
  Stream #0:0(eng): Audio: mp3, 48000 Hz, stereo, fltp, 320 kb/s (default)
    Metadata:
      DURATION        : 01:03:39.421000000 'mp3con.sh.CONVERT-mp3/./Vintage 70s Hoover Convertible ASMR _ Retro White Noise for Sleeping, Focus _ 1 HOUR-gWWCInm-SDw.c1.webm.mp3'
      encoder         : Lavc58.134.100 libmp3lame'mp3con.sh.CONVERT-mp3/./Vintage 70s Hoover Convertible ASMR _ Retro White Noise for Sleeping, Focus _ 1 HOUR-gWWCInm-SDw.c1.webm.mp3'
size=  149198kB time=01:03:39.41 bitrate= 320.0kbits/s speed=  25x    
video:0kB audio:149198kB subtitle:0kB other streams:0kB glob'mp3con.sh.CONVERT-mp3/./Vintage 70s Hoover Convertible ASMR _ Retro White Noise for Sleeping, Focus _ 1 HOUR-gWWCInm-SDw.c1.webm.mp3'al headers:0kB muxing overhead: 0.000658%

 All files converted successfully
guest@porteus:/mysound/WhiteNoise/mp3con.sh.CONVERT$ ls -oa 
total 56984
drwxr-xr-x 2 guest     4096 2022-09-14 00:32 .
drwxr-xr-x 3 guest     4096 2022-09-14 00:29 ..
-rw-r--r-- 1 guest 58278950 2022-02-15 11:34 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.webm
That is quite the disappointment, for now I have no clue where the named by ffmpeg target file 'mp3con.sh.CONVERT-mp3/./Vintage 70s Hoover Convertible ASMR _ Retro White Noise for Sleeping, Focus _ 1 HOUR-gWWCInm-SDw.c1.webm.mp3' is… :wall:
Seems lazy butt me cannot simple run mp3con.sh and have the file I wanted converted, well, converted, when the target file is not to be found. :crazy:

Added in 17 minutes 29 seconds:
Update 2

Looking into its code, I found where it put the file - it put it into ~/Music/mp3con.sh.CONVERT-mp3/ without telling the user about that.

Code: Select all

# Move audio directory to ~/Music

if [[ ! -d ~/Music ]]; then
    mkdir ~/Music
fi
cd ..

mv  "$base_name"-mp3 ~/Music/
Weird undocumented behaviour, but at least the file conversation was finally successful.
Having to read a script's source code to find the resulting converted files is in my book a no-no.

What do you folks think?

Added in 8 minutes :
And another thing I dislike about how mp3con.sh handles things - it converts all files using 320K bitrates - which creates a quite large target file, but in my case the source file only has

Code: Select all

Duration: 01:03:39.42, start: -0.006000, bitrate: 122 kb/s
  Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
and what is lost cannot be regained, so, using 320K on a 122K file is not making the resulting mp3 amazingly better in quality than the source, the only thing it does is creating an unneeded large target file while gaining nothing by using 320K bitrate - unless your source file also had 320K bitrate.

For now, I first use my own "Multimedia File Info" Thunar add-on to give me the bitrate details of the source file and adjust the conversion line manually, e.g. from what mp3con.sh uses

Code: Select all

ffmpeg -i INFILE -b:a 320K -vn OUTFILE.mp3
I use this instead

Code: Select all

ffmpeg -i INFILE -b:a 128K -vn OUTFILE.mp3
Last edited by Rava on 14 Sep 2022, 11:18, edited 1 time in total.
Reason: typo - its ffprobe (not fprobe) thanks donald!
Cheers!
Yours Rava

beny
Full of knowledge
Full of knowledge
Posts: 2092
Joined: 02 Jan 2011, 11:33
Location: italy

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#2 by beny » 13 Sep 2022, 23:22

guest@porteus:~/Music$ sh mp3con.sh

1: Current directory
2: Provide directory

Selection : 1
Okay..

Current dir is /home/guest/Music



ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11.2.0 (GCC)
configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/doc/-4.4.1/html --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-gpl --enable-libaom --enable-version3 --enable-avresample --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gnutls --enable-libbluray --enable-libcdio --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libmp3lame --enable-opencl --enable-opengl --enable-libopenjpeg --enable-libpulse --enable-libsmbclient --enable-pthreads --enable-postproc --enable-swscale --enable-avfilter --enable-libopus --enable-libspeex --enable-openal --enable-libass --enable-libx264 --enable-libx265 --enable-libgsm --enable-libxvid --enable-libtwolame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libgsm --enable-nvdec --enable-librtmp
libavutil 56. 70.100 / 56. 70.100
libavcodec 58.134.100 / 58.134.100
libavformat 58. 76.100 / 58. 76.100
libavdevice 58. 13.100 / 58. 13.100
libavfilter 7.110.100 / 7.110.100
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 9.100 / 5. 9.100
libswresample 3. 9.100 / 3. 9.100
libpostproc 55. 9.100 / 55. 9.100
Input #0, matroska,webm, from './Beyonce - Ave Maria [rgxfthPHTwc].webm':
Metadata:
ENCODER : Lavf58.76.100
Duration: 00:03:41.74, start: -0.007000, bitrate: 235 kb/s
Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv), 640x480, SAR 1:1 DAR 4:3, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
Metadata:
DURATION : 00:03:41.633000000
Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Metadata:
DURATION : 00:03:41.741000000
Stream mapping:
Stream #0:1 -> #0:0 (opus (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to 'Music-mp3/./Beyonce - Ave Maria [rgxfthPHTwc].webm.mp3':
Metadata:
TSSE : Lavf58.76.100
Stream #0:0(eng): Audio: mp3, 48000 Hz, stereo, fltp, 320 kb/s (default)
Metadata:
DURATION : 00:03:41.741000000
encoder : Lavc58.134.100 libmp3lame
size= 8663kB time=00:03:41.71 bitrate= 320.1kbits/s speed=22.4x
video:0kB audio:8662kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.011330%
mv: 'Music-mp3' and '/home/guest/Music/Music-mp3' are the same file

All files converted successfully
hi rava if you choose the first option you have a directory inside that is where the converted file is but i have to see if play work with audacity that i use for this task.

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#3 by Rava » 13 Sep 2022, 23:26

The difference in filesizes speaks for themselves. I moved the mp3con.sh converted mp3 into the original folder for comparison and renamed it:

Code: Select all

guest@porteus:/mysound/WhiteNoise$ mv Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping\,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.mp3 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping\,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.mp3con.sh.mp3 
guest@porteus:/7/sound/WhiteNoise$ ls -o Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping\,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.*
-rw-r--r-- 1 guest 152779245 2022-09-14 00:32 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.mp3con.sh.mp3
-rw-r--r-- 1 guest  58278950 2022-02-15 11:34 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.webm
guest@porteus:/mysound/WhiteNoise$ ffmpeg -v error -i Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.webm  -b:a 128K -vn Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.mp3
guest@porteus:/mysound/WhiteNoise$ ls -o Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping\,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.*
-rw-r--r-- 1 guest 152779245 2022-09-14 00:32 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gMBWWCInm-SDw.c1.mp3con.sh.mp3
-rw-r--r-- 1 guest  58278950 2022-02-15 11:34 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.c1.webm
-rw-r--r-- 1 guest  61111725 2022-09-14 01:22 Vintage\ 70s\ Hoover\ Convertible\ ASMR\ _\ Retro\ White\ Noise\ for\ Sleeping,\ Focus\ _\ 1\ HOUR-gWWCInm-SDw.mp3
or in MBs:

Code: Select all

Original webm (122K) -  55.58 MB
mp3con.sh.mp3 (320K) - 145.70 MB
my mp3 (128K)        -  58.28 MB
Added in 2 minutes 17 seconds:
Beny, by coincidence by using "/home/guest/Music" as your source folder, you used the very folder mp3con.sh uses to move its converted files into.
When you have some folder somewhere on one of your harddisks - e.g. /mnt/sda3/sound/mp4/ or /mnt/sdb1/sound/webm/ you would not find it so easily.
mp3con.sh even says so itself:
beny wrote:
13 Sep 2022, 23:22
mv: 'Music-mp3' and '/home/guest/Music/Music-mp3' are the same file
ImageImage

Also, when you convert many files via mp3con.sh and your free space on /home/guest/Music/ is limited (usually that is on your RAM disc) it would fill up your RAM disc to 100% by moving files from your hard disk (e.g. from /mnt/sda3/sound/mp4/ ) …
In my book, a script should either ask the user first if he wants the converted files moved somewhere, or just leave the files where the user put the original file in.
Cheers!
Yours Rava

beny
Full of knowledge
Full of knowledge
Posts: 2092
Joined: 02 Jan 2011, 11:33
Location: italy

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#4 by beny » 13 Sep 2022, 23:47

hi Rava i can confirm that with audacity the file mp3 is ok, the script make a folder inside the directory named mp3-music so you have all the files in and work locally i have run the script on anothe hard disk but no way:
1: Current directory
2: Provide directory

Selection : 2

Give diretory name: /mnt/sdg1/mio
/home/guest/mp3con.sh: line 35: ${$dir_name}: bad substitution

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#5 by Rava » 13 Sep 2022, 23:51

beny, the script is weird on more than one level. I recommend using the one-liner I put into my initial post at top in the TL;DR section.

In your case, your source file is 235 kb/s

You get that info e.g. via

Code: Select all

ffprobe -hide_banner FILENAME
Usually, these are valid bit rates for lame:

Code: Select all

32,40,48,56,64,80,96,112,128,160,192,224,256,320
Everything below 128 kb/s is NOT recommended unless it is a telephone voice recording or other kinds of low-level voice recording (like from a low-quality surveillance camera with crappy microphone) or such.

So, for your file I would recommend

Code: Select all

ffmpeg -i INFILE -b:a 256K -vn OUTFILE.mp3
Try that and compare the file sizes of your original, of the mp3con.sh mp3 version and your manually created mp3 version using 256K as shown above.
Last edited by Rava on 14 Sep 2022, 11:18, edited 1 time in total.
Reason: typo - its ffprobe (not fprobe) thanks donald!
Cheers!
Yours Rava

donald
Full of knowledge
Full of knowledge
Posts: 2067
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#6 by donald » 14 Sep 2022, 01:02

Rava wrote:
13 Sep 2022, 23:16
adjust the bit rate accordingly to the one in your source file.mp3con.sh always uses 320 kb/s regardless of what the source file has - ......gaining nothing from that (what is lost due to smaller bitrate cannot be regained by converting into another file using larger bitrate)
It is in fact useful to use a higher bitrate than the source file.
[not so much for "white noise", but for "real" music].

Why:
The source file has already been compressed with a lossy codec, and part
of the original is already gone.

If you now convert this file to another format using (again) another, non-lossless codec,
even more of the music will be cut off.
The more, the lower the choosen bitrate is.

so if you don't want to lose much, choose a higher bitrate for recompression than
the source file has.

However, 320K seems to be a bit overkill.

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#7 by Rava » 14 Sep 2022, 01:52

I agree, and as you can see I always choose a bitrate higher than the source.
donald wrote:
14 Sep 2022, 01:02
However, 320K seems to be a bit overkill.
Especially considering that 128K is seen as the standard.

As seen by valid bitrats for lame (leaving out the lower ones)

Code: Select all

128,160,192,224,256,320
320 is 5 levels above the average (when we surmise the average is the most used one)
Cheers!
Yours Rava

User avatar
M. Eerie
Moderator
Moderator
Posts: 622
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#8 by M. Eerie » 14 Sep 2022, 08:57

Rava wrote:
14 Sep 2022, 01:52
I always choose a bitrate higher than the source.
Would you select a maximum quality jpeg when saving a pixelated photograph? ;)

I think the best best option would be the one that best matches the original sound source.
If the original audio is sampled @128kbps (typical value for CD's) you won't get better sound sampling it @320 kbps.

Then the AAC codec (Advanced Audio Coder) has a more efficient algorithm and allows you to almost halve the sampling bitrate to obtain a sound that perceptually matches an MP3 source.
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] convert mp4/mpv/WebM sound files into mp3

Post#9 by Rava » 14 Sep 2022, 09:55

M. Eerie wrote:
14 Sep 2022, 08:57
Would you select a maximum quality jpeg when saving a pixelated photograph? ;)
I would even use bmp (lossless) when doing several instances of editing the image, because else each and every saving instance would add new loss. And in the end I use webp since it ismore efficient than jpeg.
Or when it's a screenshot usually lossless png is the more efficient than 93% quality of webp or jpeg.
M. Eerie wrote:
14 Sep 2022, 08:57
Then the AAC codec (Advanced Audio Coder) has a more efficient algorithm and allows you to almost halve the sampling bitrate to obtain a sound that perceptually matches an MP3 source.
Therefore I looked for a solution that used the free cddb database and includes aac among its file formats - see the other thread for the package and setup I provided.
( look for abcde-2.7.1-noarch+cd-discid-1.4-x86_64-2jsc-fixed-abcde.conf_rava.xzm )
Cheers!
Yours Rava

Post Reply