You are not logged in.

#1 2015-07-08 23:25:08

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Script encode music library

Hello all,

I am after a specific technical script and need some help because I do not want to loose data.

I have a large music folder which contained files of mp3/m4a/wma/flac and some jpg or other picture file for the album cover.
I need to have those files encode to mp3, and would love a script to find all those file, encode them to mp3 with the same name and delete the other files - obviously keeping the same compression rate than the original.

Obviously with more than 20,000 songs I am quite careful with the folder and don't want to mess around.

Your help would be greatly appreciated.

Many thanks

Offline

#2 2015-07-09 01:06:03

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Script encode music library

Just copy some of these files to another folder and try things out.

Offline

#3 2015-07-09 01:10:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,522
Website

Re: Script encode music library

You have a backup, right?  If you don't have a backup, then you don't have data - valuable or otherwise.

Given that you have your backup, you'll likely just want to use a find command with an -exec flag to re-encode.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2015-07-09 01:25:38

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,222
Website

Re: Script encode music library

What have you tried so far?

Offline

#5 2015-07-09 08:51:42

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Re: Script encode music library

Yes, I have a backup and yes I can work on a copy so if it turn sour I can use the back up.

Honestly I have been looking around but cannot find a solution, hence why I am looking for assistance - this is way above my bash script competence.

thanks

Offline

#6 2015-07-09 10:03:33

satanselbow
Member
Registered: 2011-06-15
Posts: 538

Re: Script encode music library

With the greatest respect... are you looking to program/script this as a (homework) project for your own learning - or do you want someone to give you the answer?

Whilst re-encoding audio formats seems a trivial task on the face of it - there are a few gotchas... preventing the appearance of "transcodes" (converting low bitrate audio source to a higher bitrate) for example...

Offline

#7 2015-07-09 10:07:42

sweetthdevil
Member
Registered: 2009-10-20
Posts: 415

Re: Script encode music library

Thank for the interest and for the reply.

The reason why I need to do it, is because I am getting a new car and it read only mp3 from usb-key.

Honestly if a great soul and script guru could write it I will be over the moon - that wouldn't stop me to understand it and learn from it. But as you rightly pointed it out it is extremely technical.

thanks,

Offline

#8 2015-07-09 10:19:37

Awebb
Member
Registered: 2010-05-06
Posts: 6,285

Re: Script encode music library

How about you reply to Trilby's proposal and go on from there? He basically gave you everything but the actual encode command. The rest is one google query ala "linux cli convert mp3" away.

Offline

#9 2015-07-09 10:35:34

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,522
Website

Re: Script encode music library

find /path/to/music -regex '.*\.\(m4a\|wma\|flac\)$' -exec bash -c "fname='{}'; sox \"\$fname\" \"\${fname%.*}.mp3\"; rm \"$fname\"" \;

Then go get coffee.


Or, alternatively, for something more readable, you could just use the following:

find /path/to/music -regex '.*\.\(m4a\|wma\|flac\)$' -exec myconvert "'{}'" \;

Where "myconvert" is an executable script that does whatever you want in the conversion:

#!/bin/bash

mp3="${1%.*}"
sox "$1" "$mp3"
# optionally also delete original in the same step:
#rm $1

That's about all it takes.  But as this is untested, you better take AWebb's advice too and use this as a guide to what to look for in the google results and what you need to understand and check for bugs before you run it on your valuable data.  You could also use ffmpeg instead of sox.  ffmpeg is more likely to already be installed on your system.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2015-07-09 16:18:06

karkhaz
Member
Registered: 2014-01-25
Posts: 79

Re: Script encode music library

I use a makefile for this. The advantage is that the makefile only converts things that need to be converted, and you can also pass in -j for parallelism.

Scenario: I have mp3, flac, and m4a files somewhere under directory 'music'. For the flac files, I want to convert them to mp3 and put them in directory 'ipod'. For the mp3 and m4a files, I want a symlink from directory 'ipod' to the original mp3/m4a files in 'music' (to save space).

Here's the makefile:

MP3_SRC=$(shell find music -name "*.mp3")
M4A_SRC=$(shell find music -name "*.m4a")
FLAC_SRC=$(shell find music -name "*.flac")

MP3_DST=$(patsubst  music/%,ipod/%.mp3,$(MP3_SRC))
FLAC_DST=$(patsubst music/%,ipod/%.mp3,$(FLAC_SRC))
M4A_DST=$(patsubst  music/%,ipod/%,$(M4A_SRC))

FFMPEG_FLAGS=-y -loglevel warning

default: $(MP3_DST) $(FLAC_DST) $(M4A_DST)

ipod/%.mp3.mp3: music/%.mp3
        @mkdir -p $(patsubst music,ipod,$(@D))
        @echo "Symlinking" $<
        @ln -s $< $@

ipod/%.m4a: music/%.m4a
        @mkdir -p $(patsubst music,ipod,$(@D))
        @echo "Symlinking" $<
        @ln -s $< $@

ipod/%.flac.mp3: artists/%.flac
        @mkdir -p $(patsubst music,ipod,$(@D))
        @echo "Transcoding" $<
        @ffmpeg $(FFMPEG_FLAGS) -i $< -b:a 320k $@

Then just run make -j20 and

Trilby wrote:

Then go get coffee.

but hurry, this completes surprisingly fast on a multicore machine smile And if you add new music to 'music' and run make again, it will ignore everything that it's already converted.

Last edited by karkhaz (2015-07-09 16:21:18)

Offline

#11 2015-07-09 16:26:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,522
Website

Re: Script encode music library

The makefile is a good idea in general - but the OP wants the original files deleted afterwards, so the skipping of anything that is already converted applies to all approaches: my find command will only convert files that are not yet in mp3 format.  Once they are converted, they are also deleted, so running the same command again when new music is added will only convert what is new.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB