Saturday, May 11, 2013

Converting FLAC files for your iPad


Introduction

The Free Lossless Audio Codec (FLAC) is a widely supported audio file format on many devices, but unfortunately is not supported on Apple's various iDevices such as iPads.

The key advantage of FLAC is that it is a lossless compression format, which means that you don't lose any information when converting to this format from the original recording. Many other audio formats are lossy, such as MP3, which means that smaller file sizes are achieved by discarding some less important information. 

Given that Apple devices can't play FLAC files, how to play your FLAC files on iDevices?

There are essentially two choices:
  1. Convert the FLAC files to an inferior lossy format such as MP3 or AAC
  2. Convert the FLAC files to another lossless format.
Option 1 isn't an alternative if you want to retain all of the information in the FLAC file.

Option 2 can be achieved by converting to the Apple Lossless Audio Codec (ALAC) which is supported by Apple's iDevices.

ALAC files have the extension .m4a

A free conversion tool from FLAC to ALAC is FFmpeg or its fork avconv
Here we use  avconv since that is what Ubuntu / Debian supports.

avconv -i input.flac -c:a alac output.m4a

ffmpeg -i input.flac -acodec alac -map_meta_data 0:0,s0 output.m4a

Note the ffmpeg command should all be on one line.

How do .m4a ALAC files sound?

Well to my untrained ear I prefer the ALAC file to the original FLAC file. This may simply be due to the players I have tried. The experts seem to conclude that there shouldn't be any real difference between ALAC and FLAC since they are both merely compressed, i.e. no data lost. Still I prefer the sound of ALAC files to FLAC files, but I guess this is what separates humans from computers. What makes my conclusion all the more interesting is that I have a natural anti-Apple bias.

As a general rule I keep all of my original music files in FLAC format and convert them as needed to ALAC and other formats.

The big problem with ALAC is that support for the file format centers around iDevices. My Playon!HD Mini2 Full HD Network Media Player for example, refuses to play ALAC files despite being less than a year old.


Conclusion


Despite not being a supporter of Apple or its approach to business and consumers, I have to admit I have been quietly impressed by ALAC. There is no doubt that ALAC is comparable to FLAC and performs a similar role.

In summary, ALAC is highly recommended as a lossless format for Apple's iDevices. Just remember though that ALAC support is at best patchy away from Apple devices and so doesn't have the widespread support of  FLAC.


Basic Bash Shell Script


A basic Bash Shell script would look something like:

#!/bin/bash
# This script converts FLAC files to Apple Lossless 
# Audio Codec for use on iPad etc.
# Note the main issue seems to be retaining any meta 
# data.
# Note I use avconv in place of FFmpeg since avconv is 
# a fork of FFmpeg and is supported by Ubuntu / Debian.

mkdir -pv ALAC
for f in *.flac
do 
avconv -y  -i "$f" -c:a alac "./ALAC/${f%.*}.m4a"
echo "$f finished"
done

A full Bash Shell Script can be found towards the bottom of the page.


Further Reading


How can I convert my FLAC music collection to Apple Lossless?

Audio conversion between FLAC and ALAC in Ubuntu


Bash Shell Script


#!/bin/bash
# This script converts FLAC files to Apple Lossless Audio Codec for use on iPad etc.
# Note the main issue seems to be retaining any meta data.
# Note I use avconv in place of FFmpeg since avconv is a fork of FFmpeg and is supported by Ubuntu / Debian.

mkdir -pv ALAC

for f in *.flac
do 

avconv -y  -i "$f" -c:a alac "./ALAC/${f%.*}.m4a"

echo "$f finished"
echo
echo
echo
sleep 3

done


# Further Information
# Use Heredoc example from http://tldp.org/LDP/abs/html/here-docs.html
echo <<END
For more information see:

http://en.wikipedia.org/wiki/Apple_Lossless
Wikipedia: Apple Lossless Audio Codec (ALAC)

Ubuntu FFmpeg man page
http://manpages.ubuntu.com/manpages/lucid/man1/ffmpeg.1.html
http://linux.die.net/man/1/ffmpeg

How can I convert my FLAC music collection to Apple Lossless?
http://askubuntu.com/questions/108043/how-can-i-convert-my-flac-music-collection-to-apple-lossless

See also
https://lists.ffmpeg.org/pipermail/ffmpeg-user/2011-March/000010.html
which recommends

for i in *.flac
do 
ffmpeg -i "$i"  -acodec alac -map_meta_data 0:0,s0 "`basename "$i" .flac`.m4a" 
done;

in order to keep meta data.

Free batch conversion from FLAC to Apple Lossless (ALAC)?
http://avp.stackexchange.com/questions/1139/free-batch-conversion-from-flac-to-apple-lossless-alac
recommends:

for f in *.flac
do 
ffmpeg -i "$f"  -vf "crop=((in_w/2)*2):((in_h/2)*2)" -c:a alac "${f%.flac}.m4a"
done


For informaton on HERE DOCUMENT see

Shell script put multiple line comment
http://www.cyberciti.biz/faq/bash-comment-out-multiple-line-code/


bash/shell script comment multiple lines of code 
http://viewsby.wordpress.com/2011/12/17/bashshell-script-comment-multiple-lines-of-code/

Advanced Bash-Scripting Guide: 
Chapter 19. Here Documents
http://tldp.org/LDP/abs/html/here-docs.html
A here document is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.


Block Comment in Shell script
http://www.unix.com/shell-programming-scripting/39478-block-comment-shell-script.html


END


echo "Conversions Completed!"















1 comment:

  1. I use Avdshare Audio Converter to convert FLAC to Apple Lossless M4A, MP3, AAC, AIFF for iPod, iPad, iPhone.
    Here is the easy guide https://www.avdshare.com/flac-to-apple-lossless-alac-m4a-converter

    ReplyDelete