Saturday, March 22, 2014

Converting FLAC to AAC for Apple iPhone .m4a

Converting FLAC to AAC for Apple iPhone

Overview
This page describes how to convert from FLAC to AAC via avconv. The conversion tool avconv is a fork of FFmpeg.

The operating system used is a version of Linux Ubuntu (Mint KDE Nadia). For multiple FLAC to AAC conversions a simple Bash shell script is provided.

During tests savings of almost 70% were achieved when converting 166 files from FLAC to AAC, equivalent to a saving of 3.4 GB (from 4.9 GB to 1.5 GB).

The key avconv FLAC to AAC conversion syntax is 

avconv -i  inputfile.flac -acodec aac -strict experimental -ab 320k -ar 48000 -y outputfile.m4a


Introduction
Having finally purchased an Apple iPhone 5s, it quickly became clear that my favoured lossless audio format of FLAC (or Apple's lossless ALAC) was going to have much larger files sizes than desired.

Although the Apple iPhone 5s 16 GB storage (of which about 12.9 GB is available for user storage) would easily accommodate my 166 favourite songs in either lossless format, it seemed a waste of space given the limited amount of storage, and besides I wanted to store some videos on the Apple 5s as well!

A quick look at the Apple 5s technical specifications confirmed that the Advanced Audio Coding (AAC) codec was the correct lossy audio format to use. 

AAC is the successor to the popular but outdated MP3 format. AAC generally achieves better sound quality than MP3 at similar bit rates. 


Put simply forget MP3, it has already been widely replaced by AAC.

Against the general advice on the internet I went for the highest possible bit rate of 320 Kbps, fully knowing that I could have achieved smaller file sizes with lower bit rates with minimal or modest sound quality loss. I wanted the best sound that AAC would permit if I couldn't use either FLAC or ALAC.

I did consider using a Variable Bit Rate (VBR) but given the experimental nature of the AAC codec natively used in avconv I decided after several tests to play safe and use a Constant Bit Rate (CBR). In doing so I noted that the file sizes with CBR were larger than in VBR (around 50% larger in my simple tests). 

The Bash shell script shown later on this page contains a commented out (#) VBR conversion line that the user can use by uncommenting it (i.e. removing the starting #) and commenting out (i.e. placing # at the start) the CBR conversion line.

If you must use VBR try 

avconv -i  inputfile.flac -acodec aac -strict experimental -aq 31 -ar 48000 -y outputfile.m4a

and adjust the constant quality -aq 31 from the value 31 shown here until you obtain an output that satisfies your personal sound quality / file size tradeoff.


Solution
The key avconv FLAC to AAC conversion syntax is

avconv -i  inputfile.flac -acodec aac -strict experimental -ab 320k -ar 48000 -y outputfile.m4a

replacing inputfile.flac and outputfile.m4a as needed.

The key points to note are:

-acodec aac tells avconv to use the aac audio codec.
Note that multiple alternative codecs exist for converting to AAC, however most of them can't by default be released with avconv. The aac codec used by avconv doesn't have a very good reputation and is still experimental. Previously the libfaac AAC codec was available but it is no longer available by default for licensing reasons.

-strict experimental is needed since the native avconv AAC codec aac is still experimental.

-ab 320k sets a constant bit rate of 320 Kbps, the maximum value permitted under the Apple 5s technical specifications. This can equivalently be written as -b:a 320k

-ar 48000 sets a sample rate of 48,000 Hz and is recommended since some FLAC files can have higher sampling rates not supported by aac.

-y is used to automatically overwrite an existing file with the same name.


Bash Shell Script

Feel free to use this Bash shell script and distribute it to all your friends. I warmly welcome receiving improvements to the script. It is designed to 'get the job done' not be anywhere near perfect.

At the very least you will need to replace the bold red text as it refers to directories that will not exist on your computer.
#!/bin/bash
clear
clear
echo  "Start of $0"
echo
## This is to stop Bash breaking on Spaces and force it to break at the End of each Line.
IFS=$'\n'

count=0

cd  "/WinE/FLAC Master - 166/"

echo "Files processed = $count"

for i in `ls *.flac`
do
echo "Starting $i"
echo
j=$(basename $i .flac)

#Alternatively....
#j="${i%.flac}"
# or to also add the.m4a container extension at the same time...but don't forget to remove .m4a from end of avconv line around line 41.
# j="${i%.flac}.m4a"

echo -e "$i WITHOUT flac EXTENSION is \n$j"
echo

# See http://stackoverflow.com/questions/8467424/echo-new-line-in-bash-prints-literal-n for advice on adding newlines with echo -e

echo -e "New filename with FLAC extension removed is :   \n$j"
echo

echo "Files processed = $count"

let count=$count+1

# Here we output the files into my home directory (~) that has sub-directories of iPhone and AAC obviously change these to suit you.
avconv -i  $i -acodec aac -strict experimental -b:a 320k -ar 48000 -y ~/iPhone/AAC/$j.m4a

# Alternative VBR syntax
#avconv -i  inputfile.flac -acodec aac -strict experimental -aq 31 -ar 48000 -y outputfile.m4a
#avconv -i  $i  -acodec aac -strict experimental -aq 31 -ar 48000 -y ~/iPhone/AAC/$j.m4a

echo "$i done"
echo
echo "====================================================================================="
echo
done


Further Reading

To follow.

3 comments:

  1. To play FLAC on iPhone, iPod, iPad, it is better to convert FLAC to AAC, M4A, Apple Lossless, MP3, AIFF.
    iDealshare VideoGo can convert FLAC to any audio format. Here is the step by step guide at http://www.idealshare.net/flac-to-m4a-converter.html

    ReplyDelete
  2. This article will introduce the best FLAC to M4A converter - Avdshare Audio Converter and the step by step guide on how to lossless convert FLAC to M4A or convert M4A back to FLAC at http://www.avdshare.com/flac-to-m4a-converter-for-mac-pc

    ReplyDelete
  3. I convert FLAC to AAC or convert AAC to FLAC with this method https://www.idealshare.net/audio-converter/flac-to-aac-mac-windows.html

    ReplyDelete