Join 3,418 readers in helping fund MetaFilter (Hide)


Joining MP3 Files Together Based on Date in Filename?
March 25, 2009 4:08 PM   Subscribe

I have a podcast that downloads as multiple segments over multiple days. Its format arrives as YYYYMMDD_showcode_segment.mp3; so, for example, one download might result in files as 20090323_goofa_01.mp3 through 20090323_goofa_21.mp3, and also within the same download 20090324_goofa_01.mp3 through 20090324_goofa_21.mp3. What I'm looking for is a Unix script (shell, perl, what have you) that would look at a directory and look at what dates are represented there, and let me join all of the segments together by day. I can use mpgtx -j to do the actual joining; I'm looking for the code that would let the script determine what dates are in the directory and then get each day's segments united into a single MP3 for that day's show.
posted by WCityMike to Computers & Internet (5 answers total)
 
(assuming syntax for the mpgtx command is to just string the files on the end)

for date in `ls -1 | cut -d_ -f1 | sort | uniq `; do echo Working for date $date; mpgtx -j ${date}_*; done
posted by devbrain at 4:28 PM on March 25, 2009


... and if you have multiple showcodes in one day, replace "-f1" with "-f1-2" to make the main for loop iterate over date_showcode pairs ...
posted by devbrain at 4:29 PM on March 25, 2009


... and one more adjustment. If you have non zero padded part numbers (instead of 01 through 21 you have 1 through 21) they'll not sort correctly by default. Here's how to force a numeric sort on the third field when combining ...

for date in `ls -1 | cut -d_ -f1-2 | sort | uniq `; do echo Working for $date; echo COMMAND `ls -1 ${date}_* | sort -t _ -k3 -n`; done
posted by devbrain at 4:32 PM on March 25, 2009


Thanks! Iterations #2 and #3 won't be necessary at the moment, but it'll be great to refer back to if needed later.

I did modify it very slightly -- mpgtx needs an output file name. (And, also, as normally a tcsh user, I realized I had to head over to bash for this.)

So:

for date in `ls -1 | cut -d_ -f1 | sort | uniq `; do echo Working for date $date; mpgtx -j ${date}_*.mp3 -o "[Show Title] ${date}.mp3"; done

Thanks! :)
posted by WCityMike at 4:34 PM on March 25, 2009


The output of ls -1 is already sorted, so there's no need for another sort before the uniq. Also, wrapping the ls | cut | uniq pipe in backticks and processing it in a for loop loses the distinction between newlines that separate filenames and whitespace within the filenames, so if any filenames contain spaces it does the wrong thing.

Better is

ls -1 | cut -d_ -f1-2 | uniq | while read -r prefix; do echo Merging "$prefix"; mpgtx -j -o "$prefix.mp3" "$prefix"*; done

which won't break unless your filenames contain newlines.
posted by flabdablet's sock puppet at 5:59 AM on March 26, 2009


« Older Is it time to sell my vintage ...   |  I recently purchased a new HDT... Newer »
This thread is closed to new comments.