Tag Archives: bash

Alpine x305s vs iTunes

I recently purchased a new headunit for my car audio entertainment. The choice fell on an Alpine x305s model.
It belongs to the “media receiver” category: it can play music files from USB drives and iPods, but not CDs. It has no moving mechanic parts and therefore it’s less likely to break with intense use.

I mostly use it with a 32GB Sandisk USB Cruzer blade drive formatted as FAT32.

It works just fine and browsing the directories is really fast, even when loading thousands of files on the USB drive (the Kendwood headunit that I bought back in 2006 was much slower).

The headunit can read both MP3 and M4A/MP4/AAC-LC files. I copied my whole iTunes Library on the USB drive and tried to play it on the Alpine X305s. Unfortunately for many files I got the infamous “Unsupported file format” error.
I decided to examine these files to find out what they have in common and discover a possible common cause for the problem. It turned out that all these files had an encoding bitrate greater than 320Kbps. I searched the headunit manual and indeed the max supported bitrate for M4A files is 320Kbps.

Apparently at least half of the albums that you can buy on iTunes have a bitrate greater than 320Kbps.

If you are not tech-savvy you might just want to convert these files to MP3 using iTunes and be done with it.

If you are a true audiophile and would like to keep the files in MP4/M4A format then keep on reading.
I put together a simple bash script that you can use on MacOSX/Linux/BSD to find and convert m4a files with a bitrate greater than 320Kbps to 320Kbps.
I assume that you have ffmpeg installed on your machine. It’s available in macports, apt, yum.

TEMPFILE=”$(mktemp /tmp/temp-XXXXXXX).m4a”

if ! [ -d “$BASEDIR” ]; then
echo Syntax: $0 /path
exit 1


for i in $(find $BASEDIR -size +5M -type f -name ‘*.m4a’);do
IFS=” ”
BITRATE=$(ffmpeg -i “$i” 2>&1|grep Duration|grep bitrate|cut -d ‘ ‘ -f 8)
echo $i has bitrate $BITRATE
if [ “$BITRATE” -gt “320” ]; then
ffmpeg -v 5 -y -i “$i” -acodec libfaac -ac 2 -ab 320k \
-map_metadata 0 $TEMPFILE
mv $TEMPFILE “$i”

I recommend working on a copy of your iTunes library and not the original files.
Depending on your installed version of ffmpeg, you might need to adjust the BITRATE= line to extract the value of the bitrate from “ffmpeg -i file.m4a” output.

After running the script against the m4a files on my USB thumb drive, I was able to play all the files on my x305s!


How to sum the sizes of files found by ‘find’

Sometimes you want to find the size of all files that match some criteria, for example: files older than N days, or files greater than N bytes, or files that have a certain extension.

In this case, using du is not practical. Here is a simple awk one-liner that will sum the size of all files under /path that are older than 90 days:

find /path/ -type f -mtime +90 -printf "%s\n"|awk '{sum+=$0}END{print sum}'