Created
August 19, 2014 01:49
-
-
Save stefanjenkner/6a46d8b41130ffc7799d to your computer and use it in GitHub Desktop.
Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AFCONVERT=afconvert | |
AFINFO=afinfo | |
AP=/usr/local/bin/AtomicParsley | |
FLAC=flac | |
METAFLAC=metaflac | |
MP4CHAPS=mp4chaps | |
SHNTOOL=shntool | |
ZIP=zip | |
COVER?=cover.jpg | |
BITRATE?=128 | |
DOT_FLAC=$(wildcard *.flac) | |
TAG=awk '\ | |
BEGIN { TRACKNUMBER=99; TRACKTOTAL=0; DISCNUMBER=99; DISCTOTAL=0; } \ | |
/^ALBUM=/{ printf "--album \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^ALBUMSORT=/{ printf "--sortOrder album \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^ARTIST=/{ printf "--artist \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^ARTISTSORT=/{ printf "--sortOrder artist \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^ALBUMARTIST=/{ printf "--albumArtist \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^ALBUMARTISTSORT=/{ printf "--sortOrder albumartist \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^COMPILATION=1/{ printf "--compilation true " } \ | |
/^DISCNUMBER=[0-9]+/{ DISCNUMBER=substr($$0,index($$0,"=")+1); } \ | |
/^DISCTOTAL=[0-9]+/{ DISCTOTAL=substr($$0,index($$0,"=")+1); } \ | |
/^TRACKNUMBER=[0-9]+/{ TRACKNUMBER=substr($$0,index($$0,"=")+1); } \ | |
/^TRACKTOTAL=[0-9]+/{ TRACKTOTAL=substr($$0,index($$0,"=")+1); } \ | |
/^TITLE=/{ printf "--title \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^GENRE=/{ printf "--genre \"%s\" ", substr($$0,index($$0,"=")+1) } \ | |
/^DATE=[0-9][0-9][0-9][0-9]/{ printf "--year \"%s\" ", substr($$0,index($$0,"=")+1,4) } \ | |
END { \ | |
if (int(TRACKNUMBER) <= int(TRACKTOTAL)) printf "--tracknum %s/%s ", TRACKNUMBER, TRACKTOTAL ; \ | |
if (int(DISCNUMBER) <= int(DISCTOTAL)) printf "--disk %s/%s ", DISCNUMBER, DISCTOTAL ; \ | |
} \ | |
' $(1) | |
default: clean | |
wav: $(DOT_FLAC:%.flac=%.wav) | |
m4a: $(DOT_FLAC:%.flac=%.m4a) | |
joined.chapters.txt: $(DOT_FLAC:%.flac=%.chapter) $(COVER) | |
awk '\ | |
BEGIN { OFFSET=0; } \ | |
/^[0-9]+\ /{ \ | |
R=OFFSET; HH=R/3600000 ; R=R%3600000; MM=R/60000; R=R%60000; SS=R/1000; MS=R%1000 ; \ | |
printf "%02d:%02d:%02d.%03d %s\n", HH, MM, SS, 0, substr($$0,index($$0," ")+1); \ | |
OFFSET=OFFSET+$$1; \ | |
}' $(DOT_FLAC:%.flac=%.chapter) > $@ | |
joined.txt: $(DOT_FLAC:%.flac=%.txt) | |
awk '\ | |
/^ALBUM=/{ printf "TITLE=%s\n", substr($$0,index($$0,"=")+1) } \ | |
/^(ALBUM|ARTIST|ALBUMARTIST)(SORT)?=/{ print } \ | |
/^DATE=/{ print } \ | |
' $^ |sort -u -o $@ | |
joined.wav: $(DOT_FLAC:%.flac=%.wav) | |
$(SHNTOOL) join -o wav $^ | |
joined.m4b: joined.chapters.txt joined.txt joined.wav $(COVER) | |
$(AFCONVERT) -v -f m4af -d 'aac ' -s 3 -u vbrq 32 joined.wav $@ | |
$(AP) $@ -W $(shell $(call TAG,$(@:%.m4b=%.txt))) --stik value=2 | |
$(AP) $@ -W --encodingTool "" --encodedBy "" | |
PIC_OPTIONS="removeTempPix:MaxDimensions=600" $(AP) joined.m4b -W --artwork $(COVER) | |
$(MP4CHAPS) --import $@ | |
%.chapter: %.wav %.txt | |
$(AFINFO) $(@:%.chapter=%.wav) | awk 'BEGIN { OFFSET=0; } /^estimated\ duration:\ [0-9]+\.[0-9]+\ sec/ { OFFSET=OFFSET+(substr($$3,0,index($$3,".")-1)*1000); OFFSET=OFFSET+substr($$3,index($$3,".")+1,3); } END { printf OFFSET >> "$@"}' | |
awk '/TITLE/{ printf " %s\n" ,substr($$0,index($$0,"=")+1) >> "$@" }' $(@:%.chapter=%.txt) | |
%.wav: %.flac | |
$(FLAC) -f -o $@ -d $< | |
%.m4a: %.wav %.txt %.lyrics $(COVER) | |
$(AFCONVERT) -v -f m4af -d 'aac ' -s 3 -u vbrq 64 $(@:%.m4a=%.wav) $@ | |
$(AP) $@ -W $(shell $(call TAG,$(@:%.m4a=%.txt))) | |
$(AP) $@ -W --encodingTool "" --encodedBy "" | |
$(AP) $@ -W --lyricsFile $(@:%.m4a=%.lyrics) | |
PIC_OPTIONS="removeTempPix:MaxDimensions=600:SquareUp" $(AP) $@ -W --artwork $(COVER) | |
%.m4a: %.wav %.txt $(COVER) | |
$(AFCONVERT) -v -f m4af -d 'aac ' -s 3 -u vbrq 64 $(@:%.m4a=%.wav) $@ | |
$(AP) $@ -W $(shell $(call TAG,$(@:%.m4a=%.txt))) | |
$(AP) $@ -W --encodingTool "" --encodedBy "" | |
PIC_OPTIONS="removeTempPix:MaxDimensions=600:SquareUp" $(AP) $@ -W --artwork $(COVER) | |
album_m4a.zip: $(DOT_FLAC:%.flac=%.m4a) | |
$(ZIP) $@ $^ | |
clean: | |
rm -f $(DOT_FLAC:%.flac=%.wav) $(DOT_FLAC:%.flac=%.m4a) $(DOT_FLAC:%.flac=%.chapter) joined.chapter joined.chapters.txt joined.wav cover-resized* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment