Convert various Music files to .mp3

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Convert various Music files to .mp3

Post by rossdv8 »

This was written mostly for people who are NEW to Linux, so it is not all 'techy'.
Thanks to Rene and Flemur, who fixed my mistakes and resulted in this EDIT !!
Rene - all processes on one line
Flemur - Possible unintended loss of some files.

Perhaps, run it in COPIED folders a few times to see if anything breaks?

Over time we collect Music in various formats, either ripping our CDs or DVDs to play the music we have bought on our phones, tablets or computers and players.

.mp3 is one format that while it might not be the 'best' format (it is lossy) is convenient and easily played on almost anything.

Mint, and other Linux systems can rip and convert most things. There are several GUI programs to do this task. Most might find a GUI easier than using a script, but doing stuff in the terminal and using shell scripts is part of the fun of getting your hands dirty in Linux. (sometimes)
There is a handy program called ffmpeg in most repos, and it is usually installed by default in Mint.
It can change formats easily and bash scripts have been around for years to achieve this, but often they only convert from .mp4 to .mp3, leaving all the .wav files (the format you usually get when you simply 'copy' the tracks on a CD rather than 'ripping' them) unconverted in the folder/directory. We also sometimes accumulate other files that can often be converted.

I rewrote the mp42mp3.sh script to 0vid4mp3.sh
the 0 in front simply puts it early in the file list.

A discussion here corrected a few glaring errors in my messy script and it now works better. Thanks Rene and Flemur.

This is nothing special, just a simple script for beginners who may have music in various other formats who want it changed to .mp3 format.

Here's Rene's simplified (corrected) code. If it throws an ERROR message on a particular file type, just remove that extension form the line.

If you save it as 0vid4mp3 it will appear early in a list of files, and it converts more filetypes than the old mp42mp3
Run this script in Terminal in the folder you want to convert folder
Run it with ./0vid4mp3.sh or bash 0vid4mp3

Code: Select all

#!/bin/bash
# This script needs ffmpeg
# sudo apt install ffmpeg
# If ffmpeg is not in your suppository, you may need to dig around a bit for it
# Copy this script to the directory you want to convert
# If you have other file types like .ogg or .MPG in capitals
# that are not in the list, add them to the line with filetypes
# don't forget to save this file if you edit it or changes won't work
# rename the resulting output directory (music-mp3) and move it wherever you want it.
# Have Fun - Rene, Flemur, RossDv8 (Mint Forums)

shopt -s nullglob

mkdir -p music-mp3
for i in *.mp4 *.MP4 *.mkv *.mpg *.webm *.wav *.avi *.flv *.m4a *.wma *.wmv
do
	ffmpeg -i "$i" -ab 128k -y "music-mp3/${i%.*}.mp3"
done
mv *.mp3 -t music-mp3
Last edited by rossdv8 on Sat Feb 12, 2022 7:32 am, edited 9 times in total.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
User avatar
wallyUSA
Level 6
Level 6
Posts: 1433
Joined: Thu Jun 08, 2017 2:31 pm
Location: Top of Georgia

Re: Convert various Music files to .mp3

Post by wallyUSA »

@rossdv8,

Very cool! I would like to mention I used the 'asunder' app to rip an extensive CD collection. A very user friendly app in System Manager.

Thanks for sharing the script :!:
> If your query has been resolved, edit your original post and add <SOLVED> to the beginning of the subject line. This may help others find solutions. <

Dell Latitude 7490 Mint 21.3 Ker 5.15.0-101 Cinn 6.0.4
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Convert various Music files to .mp3

Post by Flemur »

rossdv8 wrote: Thu May 06, 2021 3:18 am

Code: Select all

 mkdir music-mp3
for i in *.mp4
do
ffmpeg -i "$i" -ab 128k "${i%mp4}mp3"
mv *.mp3 music-mp3
done
You might want to change that to

Code: Select all

mv *.mp3 music-mp3/.
so you get an error if music-mp3 doesn't exit, e.g. someone takes a chunk of that script without the mkdir, or renames the output directory; it prevents the .mp3 files being renamed to music-mp3 (there was a recent forum post from a guy who'd lost a bunch of files because of exactly that issue).
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

May also want to just have it be just one loop, i.e., something like

Code: Select all

shopt -s nullglob

mkdir -p music-mp3
for i in *.mp4 *.MP4 *.mkv *.mpg *.webm *.wav *.avi
do
	ffmpeg -i "$i" -ab 128k "music-mp3/${i%.*}.mp3"
done
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

rene wrote: Thu May 06, 2021 11:26 am May also want to just have it be just one loop, i.e., something like

Code: Select all

shopt -s nullglob

mkdir -p music-mp3
for i in *.mp4 *.MP4 *.mkv *.mpg *.webm *.wav *.avi
do
	ffmpeg -i "$i" -ab 128k "music-mp3/${i%.*}.mp3"
done
Thanks Rene and Flemur,
I hate getting old. I just looked at some stuff I did a few years ago to automate graphics - sure enough it's all one line :oops:
I just don't think properly any more. I'll delete part of the original post and make this one 'the post'.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

I notice you added back the mv *.mp3 music-mp3/; I did leave that out on purpose, chosing to simply generate the mp3 into music-mp3/ directly. If the point is that any existing mp3's in the current directory should also be moved, doing that once outside the loop would be more appropriate.
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

I added that after reading Flemur's comment about someone losing a heap of files apparently because I messed up the end bit:
ffmpeg -i "$i" -ab 128k "${i%mp4}mp3"
mv *.mp3 music-mp3
done
You might want to change that to
CODE: SELECT ALL

mv *.mp3 music-mp3/.
so you get an error if music-mp3 doesn't exit, e.g. someone takes a chunk of that script without the mkdir, or renames the output directory; it prevents the .mp3 files being renamed to music-mp3 (there was a recent forum post from a guy who'd lost a bunch of files because of exactly that issue).
This was meant to be a simple quick way to organise a collection.
Any suggestions that improve it from people not as tired as I am are welcome and I'll implement them in a modified post if they make sense.

I suppose I should take it a step further and test whether the music-mp3 directory exists before starting the loop.
something like [ ! -d /tmp/mydir ] && mkdir -p /tmp/mydir should work
Last edited by rossdv8 on Thu May 06, 2021 9:20 pm, edited 1 time in total.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Convert various Music files to .mp3

Post by Flemur »

rossdv8 wrote: Thu May 06, 2021 8:02 pmI hate getting old. I just looked at some stuff I did a few years ago to automate graphics - sure enough it's all one line
Hey, I'm old too.

I also don't trust scripts, I'm afraid they'll "bulk unicornize" the files, so for something like this conversion I'd probably do

Code: Select all

ls *.mp4 [etc] > a
vi a 
:1,$s/^/lameV2 /
:wq
sh a
mv *.mp3 music/.
...so 'a' would be a file full of one-line scripts: lameV2 filename.mp4 creates filename.mp3

lameV2 = lame -V 2 -q 0 -m s $1

Edit: FWIW, my "workflow" always consists of file.mp4[etc] -> ffmpeg -> .wav -> edit it -> .wav -> lameV2 -> .mp3, but I'm pretty sure that lame can't convert from all those formats, rather than just .wav files. (.wav, PCM, mp1,2,3 input only)?
Last edited by Flemur on Thu May 06, 2021 9:50 pm, edited 1 time in total.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

rossdv8 wrote: Thu May 06, 2021 9:15 pm I added [the mv] after reading Flemur's comment about someone losing a heap of files
The way it is now, with the mp3 being generated as music-mp3/<name>.mp3 directly you could leave out the entire mv.
rossdv8 wrote: Thu May 06, 2021 9:15 pm I suppose I should take it a step further and test whether the music-mp3 directory exists before starting the loop.
That's basically what the parameter -p to mkdir already takes care of. I.e., either the directory music-mp3 exists already or it is created right before the loop.

Hope I'm not too severely spoiling the fun... ;(
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

Yes, I'm used to just writing quick and dirty stuff for myself these days and modifying it as I go. I was collating a lot of ripped stuff yesterday and though 'I should share this'.
But I 'should have' waited until I knew what I wanted instead of copying and pasting each time it threw an ERROR on a new media type.

I've added [ ! -d /tmp/mydir ] && mkdir -p /tmp/mydir at the beginning. If I remember correctly that should look for the existence of the directory and make it if it is not there.
I'm running it now, then I'll run it again with the directory in place and see if it breaks...
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

Hope I'm not too severely spoiling the fun... ;(
I gave up serious programming years ago, and it looks like even bash scripts are beyond me now :D
But this is still fun, and perhaps something useful will come out of it.

We should be able to fine tune this, then get a moderator to move the post to the general discussion area - and move the RESULT to Tutorials.

Maybe this thread can end up in 'How a forum can combine to make something useful' ?

Ok,
I ran it without the directory - and it created it, and moved all the .mp3 files into it.
I ran it with the directory created and got the ' filename.mp3 exists, do you want to overwrite y/N.
Now I need to break out of pressing Return for each file that exists, while adding any that don't!

Obviously, the simple way is to just do Ctrl + c at that point.
I can see a small problem here, but I don;t want this to become something scary.
Last edited by rossdv8 on Thu May 06, 2021 9:41 pm, edited 1 time in total.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

Sure. And in that sense then: the [ ! -d <dir> ] && mkdir -p <dir> is unneeded. The -p parameter ostensibly means "create parents" but with as in the original a single path component anyway that's not what it's here used for: the other part of -p says "no error if existing". So no need to check either: mkdir does so itself.

[EDIT] Saw your edit later. Well, yes, if the file music-mp3/<name>.mp3 already exists then my variant which generates it directly under that name would ask if you wanted to overwrite it. Supposedly the normal situation is that it does not yet exist --- because why generate it if you already have it --- but if you want that old behaviour back, just add -y to the ffmpeg command. I.e.,

Code: Select all

shopt -s nullglob

mkdir -p music-mp3
for i in *.mp4 *.MP4 *.mkv *.mpg *.webm *.wav *.avi
do
	ffmpeg -i "$i" -ab 128k -y "music-mp3/${i%.*}.mp3"
done
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

Ok, this works (at my end), but does it make sense, and are there glaring errors still?

Remembering that this is supposed to be for newbies, so I tried to put easy to follow instructions at the start as comments. It would be nice if it is simple, and if my instructions inside the file make sense, and especially if it does not look like it will break stuff.

Thanks for the help. Here we go..

Code: Select all

#!/bin/bash
# This script needs ffmpeg
# sudo apt install ffmpeg
# If ffmpeg is not in your suppository, you may need to dig around a bit for it
# Copy this script to the directory containing the files you want to convert
# If you have other file types like .ogg or .MPG etc in capitals
# that are not in the list, add them to the line beginning with  'for i in *.etc (the file extensions)
# Don't forget to save this file if you edit it or changes won't work
# Rename the resulting output directory (music-mp3) after the script finishes and move it wherever you want it.
# If the directory music-mp3 already exists, you will get an error and a y/N choice to overwrite each file
# typing Ctrl + c  [Enter]  will break you out and you can rename and move the music-mp3 directory
# If you had added more files to the original directory, just run the script after renaming and moving music-mp3
# and copy the contents of the new music-mp3 into the old one and replace or skip the existing mp3 files
# Have Fun - RossDv8

shopt -s nullglob
[ ! -d /music-mp3 ]
mkdir -p music-mp3
for i in *.mp4 *.MP4 *.mkv *.mpg *.webm *.wav *.avi
do
	ffmpeg -i "$i" -ab 128k "music-mp3/${i%.*}.mp3"
	mv *.mp3 music-mp3/.

done
Last edited by rossdv8 on Fri Feb 11, 2022 5:23 pm, edited 2 times in total.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

Off to bed. I'd personally keep things at my edited in one just above, but what works, works! :)
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

Testing that now - it is so much simpler...
That's one of the GREAT things about a discussion forum. The idea was good, but the execution, although it worked, was horribly flawed.
If this version plays nice I'll move it to the original post and add credit the people who made it happen.

E D I T:
It played nice - The First post has been appropriately (I hope) modified and credited :D
Had to go back and add the mv command that I'd lost somewhere along the way, to shift the .mp3 files into the music-mp3 directory duh !

I re-learned some stuff AND learned other stuff in the discussion !

Thanks again for the running repairs..
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

Looks fine to me. And am sure someone will find the script useful; thanks as well.
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

Thanks.
In some ways it is a stupid idea because there are GUI programs around that probably do it easier.
This however was fun, entertaining and educational not to mention collaborative :D

I didn;t realise how much I had forgotten. I ended up adding the mv command at the end with the -t option so the whole thing doesn't try to move all the files into one file called music-mp3 instead of the folder.
I've lost files doing that before when I forgot to create the target.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

I tend to use the -t parameter to (GNU-) mv only in the xargs context, i.e.,

Code: Select all

find ~/ -name "*.txt" -print0 | xargs -0 mv -t ~/txt/
which then turns into e.g. mv -t ~/txt ~/a.txt ~/b.txt ~/c.txt [ ... ]. Without -t you need to run a per-file -exec.

In your case the mv *.mp3 -t music-mp3 is equivalent to the by Flemur advised mv *.mp3 music-mp3/ with the trailing slash disallowing interpretation of "music-mp3/" as anything other than a directory as well, but sure, -t also works for that.
User avatar
rossdv8
Level 7
Level 7
Posts: 1737
Joined: Wed Apr 23, 2014 4:48 am
Location: Within 2,000 kilometres of Alice Springs, Australia
Contact:

Re: Convert various Music files to .mp3

Post by rossdv8 »

In your case the mv *.mp3 -t music-mp3 is equivalent to the by Flemur advised mv *.mp3 music-mp3/ with the trailing slash disallowing interpretation of "music-mp3/" as anything other than a directory as well, but sure, -t also works for that.
I got an error using / so I went back to plain old mv. Then I remembered -t might make sure the files only went to a directory, rather than poor old bash trying to fit all the .mp3s into one file called music-mp3, and failing miserably.

There is good stuff about not needing extensions, and there is bad stuff. But if something as simple as a hyphen and a character solves the problem, I can live with the downside..

There's always a chance that I had a space before the trailing /. You never know what surprises some of us old fellers can create!

In the mean time, I wonder if we can make this thing search recursively through a drive for music that is not in .mp3, and process all of them - Without destroying too much stuff.

I was thinking about what I started out doing last night, and remembered one of my Linux systems many years ago had drag and drop conversion of various formats built into the file manager.

Originally I discovered that all files dragged out of a CD into a folder offered to convert them to .ogg or .mp3
Then I think I remember discovering that there were dragon droppings for .mpeg to .mp3, but I might be wrong.

I've seen some weird and wonderful things introduced into Linux distros for short periods of time.
Current main OS: MInt 21.3 with KDE Plasma 5.27 (using Compiz as WM) - Kernel: 6.5.0-15 on Lenovo m900 Tiny, i5-6400T (intel HD 530 graphics) 16GB RAM.
Sharks usually only attack you if you are wet
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Convert various Music files to .mp3

Post by rene »

rossdv8 wrote: Fri May 07, 2021 6:49 pm In the mean time, I wonder if we can make this thing search recursively through a drive for music that is not in .mp3, and process all of them - Without destroying too much stuff.
Sure, and not so much necessarily recursively but from a find -exec structure. When you make it more general you also though immediately increase potential failure conditions manifold so then need to put in better error handling, and before you know it it all gets away from you. Let's not, that is :)
Post Reply

Return to “Tutorials”