home :: hacks :: flac

RSS 0.91 feeds Subscribe and save over 75% off newsstand price!

car - 9
games - 2
hacks - 21
  flac - 4
  photo - 5
  slim - 3
journal - 153
misc - 5
news - 9
pomona - 2
rants - 6

For the biographers:

2005 - 2006 - 2007 - 2008 - 2009

Now playing

Décollage (Cristobal Paz & Leo di Giusto remix)
from Remixed by Bajofondo
at Monday, December 14, 2009 12:51 AM

How this works

Hot Live Webcam

Yes, really.

Recently from Google

from (null), searching for 56 foot ketch

from Mountain View CA, searching for mikey dickerson

How this works

Contact me

mikey@singingtree.com
4171622
mdickers47
mike.dickerson@pomona.edu
pomonamikey
wii number7808 7239 7724 0213
blackberry pin204db8e4

On Notice

  • The sun
  • Librarians
  • Blue
  • Purple
  • David Gray
  • Whirlpool
  • Taco Bell

As seen on The Colbert Report.

This site does not represent my employer.

May look horrible in Internet Explorer.

blosxom logo   Creative Commons License

gotta
knock
a
little
harder

obstrepero.us banner
obstrepero.us mistakes you can learn from

How to turn a crazy FLAC back into a CD

filed under: /hacks/flac

From the beginning when I started ripping CDs into FLAC files, I knew that notionally one could recreate an exact copy of the original CD from the FLAC if one wanted to, modulo hardware limitations, because the audio data is stored as a single untouched rip, and the original TOC is stored in the form of the embedded cuesheet. I never got around to figuring out how to do this, because I didn't have much reason: I have Squeezeboxes everywhere I listen to music, and they play the FLAC file.

Now, however, I have got a new car with a CD changer, plus I wanted to burn a copy of something for Beth. So despite not being able to find much of anything on Google, I found that this is actually dead simple:

#!/bin/sh
#
# flac2cd.sh - Recreate a CD from FLAC file $1.
# Written 4 Apr 2007 by M. Dickerson (too trivial for a license,
# don't you think?)

set -e

ME=`basename $0`
AUDIO=`tempfile -p $ME`
CUE=`tempfile -p $ME`
FLAC=$1

metaflac --export-cuesheet-to=- "$FLAC" | sed s!dummy.wav!$AUDIO! > $CUE
flac -f -o $AUDIO -d "$FLAC"
cdrecord -dao --cuefile=$CUE
rm $CUE $AUDIO

Obviously you need your cdrecord defaults set up right if you're going to use that literally, but that's left as an exercise to the reader.

So, the reason I didn't figure that out three years ago was that it was too easy. I was expecting to have to do all kinds of Godawful cuesheet arithmetic like I did for my assemble-random-tracks-into-CD script. It's kind of like the time when I couldn't figure out for half an hour how the hell you burn a CD on OS X, because it turns out, all you do is put in the blank CD and wait for magical things to happen.

04 Apr 2007 01:37 PT - persistent link - trackback - 2 comments

Debian packages for flac stuff!

filed under: /hacks/flac

see also:flacenstein

see also:flac-image

Moving away this month has given me some motivation to get my FLAC stuff into shape, so that the one or two people that are interested in using it have a fighting chance of getting it to do something without me to set it up for them. In the process I learned that the dh_make way of creating .deb packages, which I was using, is needlessly miserable. Without getting too far offtopic, if you are trying to make debian packages, look at this thing called CDBS.

I also found out how to set up a full-blown deb repository with a thing called reprepro. What this means is that if you add the following to your sources.list:

deb http://debs.singingtree.com sarge main
deb-src http://debs.singingtree.com sarge main

then you should be able to apt-get install packages named flac-image and flacenstein, and (maybe) even have them work. The big advantage is that having a .deb package lets apt do the work of tracking down and installing all the many dependencies.

This repository works for either the i386 or amd64 architectures, since these are the ones I can build on. Next I might try to import my daapd packages, which were once in mainstream Debian but were booted out (see #295774) because of some typical Debian license wanking.

06 Jan 2006 01:26 PT - persistent link - trackback - 0 comments

Flacenstein: FLAC librarian and transcoder

filed under: /hacks/flac

This page is a work in progress. Don't believe anything you read here.

What it does

Flacenstein is meant to do two things: rip CDs and store them in single FLAC files, and convert subsets of those FLAC files to any destination format you want.

Future proof your music.

When it rips a CD, it stores the cuesheet in the FLAC CUESHEET block, it stores the tags (artist, album, track titles, etc) in the VORBIS_COMMENT block in the current fashion, and it stores the CD cover image in an APPLICATION block, using my flac-image program. The point is that there is only one file to keep track of, and that file contains all of the information that it is reasonably possible to extract from the CD. No matter what new compression formats might be invented, there should be no need to ever take the CD off the shelf (or out of storage) again.

Easily manage incompatible player devices

Some non-portable player devices directly support FLAC (such as the excellent Squeezebox), and you can use your new FLAC library as is. But devices with relatively limited storage, like the iPod, work much better with whatever compressed format is native to them. Flacenstein displays a list of all your FLAC albums, from which you can select any subset and have them automatically converted. Output formats are handled through plugins; the ones I have supplied are for Ogg Vorbis, mp3 (via lame), and MPEG-4/AAC (via faac). The MPEG-4/AAC (.m4a) files that Flacenstein creates are compatible with the iPod, and even contain the cover art.

Flacenstein can run an arbitrary number of encoding jobs in parallel, so if you have access to multiple CPUs (e.g. an SMP machine, or through MOSIX), you can realize an almost linear speedup. Using 12 threads on an OpenMOSIX cluster of Pentium 4 machines at work, I am easily able to dump 500 albums into about 30G of m4a files in a few hours.

Screenshots

I have to convince you to try this out before you see that list of dependencies, so here is some more bait:

Flacenstein main window
Flacenstein ripper window

Download

OSI Certified

There are quite a lot of programs and libraries that flacenstein needs, but if you are hacking on digital music, you probably already have most of them lying around. If you know how to use subversion, check out the up-to-the-minute program files that I am using from:

https://svn.cs.pomona.edu/sys/src/flacenstein

Otherwise, here is a snapshot .tar.gz that I just made. It may be out of date when you read this, but it has the advantage that I happen to know that flacenstein is working today:

flacenstein-20051023.tar.gz (53k)

You may consider any files that you check out from the above subversion URL, or any files in the above tar ball, to be licensed to you under the GNU GPL version 2, unless otherwise noted.

Generally you will start flacenstein by saying python flacapp.py. If it doesn't work, read on about the many dependencies.

Dependencies

Boy howdy. From the beginning:

Python

Flacenstein is written in Python, so before anything else you will need the Python interpreter. I used Python 2.3, so Python 2.4 should also be fine if that's what you have. This, at least, is easy to get.

Python homepage - Debian package: python

wxPython

wxWidgets is a cross platform GUI library, and wxPython is the set of bindings that let you use that library inside Python. Either wxPython 2.5.3 or 2.6 should be OK. Note that wxPython is going to depend in turn on the binary wxwidgets library for GTK.

wxPython page - Debian package: python-wxgtk2.6

pyflac

pyflac is an interface that lets Python programs manipulate FLAC files, so obviously it's pretty important to Flacenstein. Unfortunately parts of it have to be natively compiled on your system, or I would just include it.

Fortunately for me, the guy who wrote pyflac has packaged it for Debian, but I don't know of anywhere else to get it.

Debian package: python-flac

Sanity check: You should now be able to start Flacenstein.

If you have made it this far, and your Python interpreter is able to locate the wxpython and pyflac modules, you should now be able to start Flacenstein with

python flacapp.py

and at least get a window to appear. It won't be able to do much yet, since there are still about a thousand more dependencies to install.

FLAC

Although Flacenstein uses pyflac to monkey with FLAC tags in some cases, it just calls the FLAC encoder directly when ripping (lame, I know). So, you will need the FLAC package and the header files in order to compile the next thing, flac-image.

FLAC home page - Debian packages: flac libflac6 libflac-dev

flac-image

I wanted to be able to store the cover art images as metadata blocks directly in the FLAC file, which Josh's metaflac program doesn't directly support, so I had to write my own, which is right here.

Required for ripping: cdparanoia

If you want to rip CDs, you need the cdparanoia binary in your $PATH. The paranoia library tries much harder than most rippers to correct jitter, skips, and all the other things that tend to go wrong. So far it is able to handle all of the lame-ass copy protection and crippleware schemes that I have run into.

The price is that cdparanoia takes much longer than some rippers, but if you want to make imperfect copies in exchange for speed, it's probably silly to be fooling around with FLAC at all.

libparanoia home page - Debian package: cdparanoia

Required for ripping: python-musicbrainz

The ripper connects to the musicbrainz database to automatically identify CDs when it can, so you will need the python-musicbrainz module. This is already a Debian package.

musicbrainz page - Debian package: python2.3-musicbrainz

Optional: lame

If you want to export FLAC albums to mp3s, you need the lame binary somewhere in your $PATH. If you don't have it, flacenstein will handle it gracefully, the mp3 export option will just be disabled.

Getting lame is a pain because of the US patent encumbrances on the mp3 format. You might have to compile it yourself, or get Debian packages from http://www.rarewares.org.

LAME home page - Debian package: lame (see above)

Optional: oggenc

If you want to export FLAC albums to Ogg Vorbis .ogg files, you need the oggenc binary somewhere in your $PATH. This is easy to find, since Ogg was designed specifically to avoid all the patent problems with mp3.

Ogg Vorbis home - Debian package: vorbis-tools

Optional: faac

If you want to export FLAC albums to MPEG4/AAC files (.m4a), you need the faac binary somewhere in your $PATH. You will have to compile it yourself or go to rarewares.org for binary packages, as for MP3.

The .m4a files that flacenstein produces use the AAC audio codec with the "low complexity" profile, which is the type that is compatible with iTunes and the iPod. A thumbnail of the cover art image will be stored in the .m4a if the image is available in the FLAC file, which is neat.

Note that flacenstein can't actually put the m4a files onto your iPod. For that you will want gtkpod or yamipod or something else.

FAAC information - Debian package: faac (see above)

25 Oct 2005 00:05 PT - persistent link - trackback - 2 comments

Storing cover art images in FLAC metadata blocks

filed under: /hacks/flac

Several people have looked for a way to store images, such as cover art, inside FLAC metadata blocks. (Mostly because several players such as the slimserver and the iPod can now display the album cover.) The standard FLAC distribution and its metaflac tool don't have this capability yet, so I have rolled my own:

flac-image.tar.gz (13k)
in subversion: https://svn.cs.pomona.edu/sys/src/flac-image/

The tar ball contains a binary compiled on Debian, but if that doesn't work, it is trivial to compile your own. It is also pretty trivial to use:

mikey@ember:~$ flac-image
flac-image 1.00, Copyright (c) 2005 Michael A. Dickerson

Usage:
flac-image (-x | -i <imagefile> | -d | -n | -l) [-t <mimetype>] flacfile ...

   -x: eXtract images in flacfile to current directory
   -i: Insert image <imagefile> in flacfile
   -d: Delete image blocks in flacfile
   -n: extract only the smallest available image block (thumbNail)
   -l: List recognized image blocks

   -t: with -i, use <mimetype> instead of guessing based on filename
       with any other operation, process only the blocks that match
       <mimetype>

For FLAC hackers: the APPLICATION block that I use is registered as 0x696d6167 (ASCII "imag") and has the following structure (from flac-image.h).

/* This struct is the "specification" for the flac-image APPLICATION data
   block.  The binary image data is prepended with this 72-byte header:
   
   vers - header structure version number.  The only defined value is
          0x0001.

   mimetype - 32 byte char buffer meant to describe the block content type
          (although this program could care less, and just treats the
          mimetype field as a kind of label.)

   filename - 32 byte char buffer meant to record the name of the original
          file.  Programs are free to ignore this when extracting files.
          This program stores the last component of the original filename
          when importing, and uses the same name when exporting.

   datasize - number of bytes of binary data that follow the header.
*/

typedef struct {
  unsigned int vers;
  char mimetype[IMAGE_HEADER_MIMETYPE_MAXLEN];
  char filename[IMAGE_HEADER_FILENAME_MAXLEN];
  unsigned int datasize;
} flac_image_header;

Josh has indicated that he intends to incorporate something like this in the next release of FLAC. When that happens, it should be simple to convert my image blocks to the standard scheme.

19 Jan 2005 01:47 PT - persistent link - trackback - 0 comments

Copyright © 2005-06 Michael A. Dickerson