tags:

views:

348

answers:

1

It's been a while since I've used make, so bear with me...

I've got a directory, flac, containing .FLAC files. I've got a corresponding directory, mp3 containing MP3 files. If a FLAC file is newer than the corresponding MP3 file (or the corresponding MP3 file doesn't exist), then I want to run a bunch of commands to convert the FLAC file to an MP3 file, and copy the tags across.

The kicker: I need to search the flac directory recursively, and create corresponding subdirectories in the mp3 directory. The directories and files can have spaces in the names, and are named in UTF-8.

And I want to use make to drive this.

+4  A: 

I would try something along these lines

FLAC_FILES = $(shell find flac/ -type f -name '*.flac')
MP3_FILES = $(patsubst flac/%.flac, mp3/%.mp3, $(FLAC_FILES))

.PHONY: all
all: $(MP3_FILES)

mp3/%.mp3: flac/%.flac
    @mkdir -p "$(@D)"
    @echo convert "$<" to "$@"

Two quick notes for make beginners:

  • The @ in front of the commands prevents make from printing the command before actually running it.
  • Make sure that the lines with shell commands in them start with a tab, not with spaces.

Even if this should handle all UTF-8 characters and stuff, it will fail at spaces in file or directory names, as make uses spaces to separate stuff in the makefiles and I am not aware of a way to work around that. So that leaves you with just a shell script, I am afraid :-/

ndim
This is where I was going...fast fingers for the win. Though it looks like you may be able to do something clever with `vpath`. Must study that one of these days.
dmckee
Doesn't appear to work when the directories have spaces in the names.
Roger Lipscombe
Didn't realize that I'd have to shell out to `find` to get the names recursively...
Roger Lipscombe
Oh. Spaces. Well, make will not work with spaces. Make syntax uses spaces for its own purposes.
ndim
@Roger: No it doesn't. There is a Grouch Marx skit involving a doctor... But I suppose that the file naming is not in your control.
dmckee
What, even in the filenames? Ick.
Roger Lipscombe
Fair enough; I'll investigate other options. Marking this as the answer anyway.
Roger Lipscombe
See the follow up question about rake, instead: http://stackoverflow.com/questions/2483418/recursive-wildcards-in-rake
Roger Lipscombe