You are not logged in.

#1 2015-02-11 17:47:48

theking2
Banned
From: Romanshorn Switzerland
Registered: 2009-03-04
Posts: 372

convert flac to mp3 with make

Hi! https://wiki.archlinux.org/index.php/Co … lac_to_Mp3 has really descriptions, but I'm stuck at one point.

The makefile shown there is.

SOURCE_DIR := flacdir
XCODE_MP3_DIR := mp3dir
# NOTE: see lame -v option for quality meaning
XCODE_MP3_QUALITY := 0

# Find .flac sources and determine corresponding targets
flac_srcs := $(shell find $(SOURCE_DIR) -type f -name '*.flac')
flac_2_mp3_tgts := $(patsubst $(SOURCE_DIR)/%.flac, $(XCODE_MP3_DIR)/%.mp3, \
    $(flac_srcs))

.PHONY: all mp3 flac_2_mp3

all: mp3 

mp3: flac_2_mp3

flac_2_mp3: $(flac_2_mp3_tgts)

$(XCODE_MP3_DIR)/%.mp3: $(SOURCE_DIR)/%.flac
        @echo "converting -> $@"
        @mkdir -p "$(@D)"
        @ffmpeg -v error -i "$<" -codec:a libmp3lame \
            -q:a $(XCODE_MP3_QUALITY) "$(@)"

but as track file names and artists commonly have spaces in their names I wanted to change this in a script that could deal with just that and I replace the flac_srcs line with

flac_srcs := $(shell find $(SOURCE_DIR) -type f -name '*.flac' | sed 's/ /\\\\\\ /g')

which would yield a list like:

/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/04.\\\ Love\\\ Came\\\ Down.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/01.\\\ Happiness.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/06.\\\ Holy\\\ Love.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/03.\\\ Sentimental\\\ Man.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/05.\\\ Body\\\ and\\\ Soul.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/08.\\\ War\\\ Is\\\ Love.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/02.\\\ Tomorrow\\\ Morning.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/07.\\\ Family\\\ Life.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/10.\\\ Soon.flac
/srv/media/audio/music/The\\\ Blue\\\ Nile/Peace\\\ at\\\ Last/09.\\\ God\\\ Bless\\\ You\\\ Kid.flac

which almost works as I get:

make: *** No rule to make target '/srv/media/audio/music/Cecilia\ Bartoli,\ Charles\ Spencer/Rossini\ Recital/05.\ La\ Regata\ Veneziana-\ II\ Anzoleta\ co\ Passa\ la\ Regata.flac', needed by 'flac_2_mp3'.  Stop.

which seems to mean that the targets are defined correctly. I think. But it seems not to work really.
Is it at all possible to define targets with spaces in makefiles? and what would be the proper way to do so?


archlinux on a Gigabyte C1037UN-EU, 16GiB
a Promise  PDC40718 based ZFS set
root on a Samsung SSD PB22-J
running LogitechMediaServer(-git), Samba, MiniDLNA, TOR

Offline

#2 2015-02-25 19:58:45

DrZaius
Member
Registered: 2008-01-02
Posts: 188

Re: convert flac to mp3 with make

Not really an answer to address your specific issue, but why not just use ffmpeg?

mkdir outputdir
for f in *.flac; do ffmpeg -i "$f" -c:v copy -c:a libmp3lame -q:a 4 outputdir/"${f%.flac}.mp3"; done

Or mp3fs:

mp3fs is a read-only FUSE filesystem which transcodes between audio formats (currently FLAC to MP3) on the fly when files are opened and read.

It can let you use a FLAC collection with software and/or hardware which only understands the MP3 format, or transcode files through simple drag-and-drop in a file browser.

Offline

#3 2015-02-25 20:17:00

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 9,868
Website

Re: convert flac to mp3 with make

use flac2all from the aur


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2015-02-26 03:50:30

Buddlespit
Member
From: Chesapeake, Va.
Registered: 2014-02-07
Posts: 452

Re: convert flac to mp3 with make

graysky, that wasn't his question. He wants to know how to rename (with spaces) during the conversion via make. He's obviously figured out how to convert For all we know, he's trying to learn something or sees a challenge and can't get around that one problem he has.

I, btw, don't have an answer. The title just caught my attention.


AMD Ryzen 7 2700X | Asus TUF X570-Plus | GeForce GTX 2070S Super | 16G Ram | KDE Plasma | Stock Kernel
Until linuxcounter.com closed, I was registered linux user #560427

Offline

#5 2015-02-26 14:01:43

EscapedNull
Member
Registered: 2013-12-04
Posts: 129

Re: convert flac to mp3 with make

flac_srcs := $(shell find $(SOURCE_DIR) -type f -name '*.flac' | sed 's/ /\\\\\\ /g')

As a side note, this is pretty fragile. If you later edit your Makefile in a way that passes the filenames into or out of the shell one more or one fewer times, you'll need to add or remove two backslashes. That being said, you can use the wildcard function.

flac_srcs: := $(wildcard $SOURCE_DIR/*.flac) $(wildcard $SOURCE_DIR/**/*.flac)

This correctly assigns flac_srcs with a list of space-embedded filenames. The problem is, no other functions or syntax are able to parse the list in a way that preserves these embedded spaces. (This says it all.) This includes your line:

$(XCODE_MP3_DIR)/%.mp3: $(SOURCE_DIR)/%.flac

Which effectively expands to (for "with spaces.flac")

mp3dir/spaces.mp3: flacdir/spaces.flac

because the percent syntax doesn't know anything about embedded spaces. It also sees "with" as a file, but since it doesn't end in ".flac", the pattern does not match. Even maintaining your sed-style escape command doesn't help, because the percent syntax doesn't interpret escaped spaces either. Instead it sees "with\ spaces.flac" as two files, "with\" (ending with a literal backslash), and "spaces.flac"

There is a trick to store an embedded space into a variable $(space). This is useful for getting a literal space, when Make would normally see the space as just a regular part of the syntax (and extra whitespace is ignored in Make). This sounds like we could use the foreach function with $(space) in order to avoid the percent substitution syntax all together. Unfortunately, using $(space) inside a foreach function (or seemingly any other function) causes it to be interpreted by the function as a name separator, *not* an embedded space. To illustrate:

flac_2_mp3_tgts := $(subst flac,mp3,$(foreach flac, $(wildcard $(SOURCE_DIR)/*.flac), a$(flac)$(space)a))
make: *** No rule to make target 'amp3dir/a.mp3', needed by 'flac_2_mp3'.  Stop. #notice there's no 'a' after '.mp3'

Note that we could, in theory, use something like

flac_2_mp3_tgts := $(shell cd $(SOURCE_DIR) ; find . -type f -name '*.flac' | sed -E 's/^\./$(XCODE_MP3_DIR)/' | sed 's/ /\\\\\\ /g')

to insert escaped spaces into the recipe target while still using the wildcard function to correctly assign space-embedded filenames into flac_srcs, but once again, the percent substitution syntax used in the recipe target does not understand them. Not to mention, if you're going to use the shell function in a Makefile, you might as well write a shell script from the start.

This tells us that it escaped spaces *are* honored in recipe targets when they are specified explicitly in the Makefile, that is without the percent substitution syntax. Using this method, however, you would have to hard-code each and every file into your Makefile as a separate recipe.

From this one can conclude that Makefiles and filenames with spaces are mortal enemies. I hope this post helps, even though I couldn't find a solution. You're an adult and you have the right to choose how you want to solve this problem, but personally I can't justify spending any more time to help you solve it with Makefiles when there are (aforementioned) far superior solutions available. Best of luck with whatever you choose!

Offline

#6 2015-02-28 03:54:19

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,157
Website

Re: convert flac to mp3 with make

EscapedNull wrote:

I hope this post helps, even though I couldn't find a solution.

This post is pure, distilled awesomeness. smile


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#7 2015-02-28 09:22:54

HiImTye
Member
From: Nanaimo, BC, Canada
Registered: 2012-05-09
Posts: 1,061

Re: convert flac to mp3 with make

you could use a for loop on your source directory, and just use a bashism to rename the files such as

for f in $srcdir/*.flac; do ffmpeg -i $f ${f%.*}.mp3; done

Last edited by HiImTye (2015-02-28 09:23:28)

Offline

#8 2015-02-28 17:31:52

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 16,861

Re: convert flac to mp3 with make

I would probably reach into my toolbox and pull out awk.  I have much better luck with awk than I do with sed.   sed does not like me sad


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#9 2018-02-19 05:52:41

mipi
Member
From: Germany
Registered: 2018-01-03
Posts: 41

Re: convert flac to mp3 with make

Hi,
I've just publish a nice tool for synching and converting music files: https://github.com/mipimipi/smsync
First, I also had a look at make, but couldn't make it work as I needed it.
Feedback is welcome.
mipi

Offline

#10 2018-02-19 06:13:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 28,157
Website

Re: convert flac to mp3 with make

Don't necrobump: https://wiki.archlinux.org/index.php/Co … bumping.22

If you want to let people know about your tool, open a thread in Community Contributions.



Closing


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB