Dmitry Leskov
 

iTunes Automation: Convert FLAC Audio Files to Apple Lossless

Update 07-Jul-2013: My iTunes for Windows automation scripts have got a home page at github.io: http://dmitryleskov.github.io/iTunesScripts/, and the latest release can now always be found at https://github.com/dmitryleskov/iTunesScripts/releases.

CD Cabinet

If this is not your first visit to my blog, you may recall that I had ripped all my audio CDs and put them away in boxes last year. My music collection now occupies just a tiny corner of a 1.5 TB hard drive. (I do not own THAT many CDs as you may guess.) Not a big deal itself these days, but the reason for blogging about that was the small utility I had then written to help EAC deal with UTF-8 encoded freedb entries. Now time has come to share with you another FLAC tool that I have created for myself. Hope you will find it handy.

Basically, I wanted to use iTunes Remote to control the playback on the PC and occasionally copy some tracks to my iPad. Problem is, iTunes does not support FLAC, so it looked as if I had to use a third-party plugin or a converter. But then I have discovered the iTunes COM for Windows SDK and put together a script that has imported my entire FLAC collection into iTunes in Apple Lossless format in just a few hours. (That’s on an i5 desktop and, mind you, I do not have THAT many CDs, so your mileage will vary.)

The script needs two binaries from the official FLAC command-line tools, flac.exe and metaflac.exe, and a small helper utility to convert metadata from UTF-8 to UTF-16.

For your convenience, dear reader, I have put together a package containing all those dependencies, but I must first warn you that my solution currently has a few limitations:

  • It is not capable of processing albums ripped of downloaded as a single FLAC file. You must split them into individual tracks before conversion.
  • The FLAC files that have any of the metatags ARTIST, ALBUM, and TITLE missing are skipped.
  • Tracks are imported in Apple Lossless (ALAC) format.

If you need any of these fixed, drop me a line in the comments or fork my script on GitHub – it is available under the MIT/X11 license.

Otherwise, grab the latest release from GitHub, watch the screencast, and enjoy!

Update 19-Feb-2012: Recorded a screencast for the most straightforward usage scenario.

Update 02-Feb-2013: Fixed a bug with invalid track numbers causing crashes.

Update 15-Jun-2013: Fixed a bug that caused the script to abort after converting a FLAC file with the readonly attribute set. Credit for finding the root cause and suggesting a fix goes to Dave Granic – thank you, Dave!.

Update 14-Jul-2013: Fixed a bug: metadata-less FLAC files caused the script to crash.

Tags: ,

« | »

Talkback

  1. Happy_Reader
    26-Dec-2011
    1:24 am
    1

    Thank you so much. Worked fine, easy, awesome.. much love!

  2. Happy_Reader
    29-Dec-2011
    10:44 pm
    2

    hey, i have a question. only a few other people on the internet have had the same problem but there is no answer currently…

    is there a maximum bitrate that the iPod can handle?`because songs with higher bitrates than the usual won’t go to the iPod…

    usually starts at 1700 I think. something like that…

    others had the same problem but no answer. people answered like “320kbps is enough for on the go”…. made me cry.

  3. Dmitry Leskov
    13-Jan-2012
    11:12 am
    3

    Well, I have copied an AAC encoded album totaling 427 MB on my hard drive to the iPad and it ate 427MB off iPad storage. So I guess the files were not changed. Their bitrates range from 835 to 1037 Kbps.

    So I guess you get the whatever bitrate your .m4a files have de facto. Question is, can you actually hear the difference?

  4. BraiNEateR
    17-Feb-2012
    3:10 am
    4

    for some reason my .bat file stops after just 1 file. I have to launch it for each file

  5. Dmitry Leskov
    19-Feb-2012
    6:28 pm
    5

    How do you run it and what does it output to the console?

    I have just created a screencast for the simplest usage scenario:

    http://screencast.com/t/peQrVyuYZf

  6. Cutiaia
    05-Mar-2012
    5:32 am
    6

    Great tool, but same problem here as BraiNEater. Stops after each file and I have to launch the batch fail again and again. Windows 7 / iTunes 10.5.3.3

  7. Dmitry Leskov
    10-Mar-2012
    12:54 pm
    7

    Same question: how do you run it and does it output any error messages?

  8. Sila
    22-May-2012
    8:01 pm
    8

    I cannot thank you enough for this conversion pack.
    Music is food for the soul :D :D :D

    Kindest regards,
    Sila

  9. Dmitry Leskov
    27-May-2012
    10:29 am
    9

    You are welcome, Sila.

  10. Furgelnod
    11-Jun-2012
    5:18 pm
    10

    Ran flawlessly for me here on Windows 7 (64 bit) with iTunes 10.6.1.7. Its an excellent utility that saved me a lot of time that would have been spent redoing all the metadata. Thanks a lot.

  11. dbM
    05-Aug-2012
    10:18 pm
    11

    Perfect! Many thanks.

  12. Ali
    19-Aug-2012
    5:47 pm
    12

    Thank you very much, this was very helpful. And I appreciate your altruism :-)

  13. Corel
    08-Sep-2012
    10:09 pm
    13

    Worked perfectly! Much appreciated

  14. John
    10-Nov-2012
    4:44 am
    14

    Hi, I tried my first flac file today and wanted to see if I could convert it to the Apple lossless format with your tool. I unpacked the zip file, then added my flac file to the same directory. I then ran the AddFLACs.bat file but get this error -

    Connecting to iTunes COM server…
    Importing FLAC files from iTunesScripts-master
    Scanning folder D:\downloads\_inbox\iTunesScripts\iTunesScripts-master
    Found FLAC file D:\downloads\_inbox\iTunesScripts\iTunesScripts-master4 – Ave Maria (Shubert, Orch. Weingartner).flac
    Extracting metadata from D:\downloads\_inbox\iTunesScripts\iTunesScripts-master4 – Ave Maria (Shubert, Orch. Weingartn
    er).flac
    D:\downloads\_inbox\iTunesScripts\iTunesScripts-master\AddFLACs.js(165, 13) Microsoft JScript runtime error: Input past
    end of file

    Have you seen that before. I might have an odd case your sofware tool doesn’t cover. I prefer to do all of my mp3 manipulation in working area first. Once I have the music files tagged properly, I manually copy them to my network hard-drive where my iTunes library is. Is it possible to run your software to just convert the flac file to Apple lossless – putting the result file in the same directory as the flac file?

    Thanks,
    John

  15. Dmitry Leskov
    11-Nov-2012
    10:52 am
    15

    John,

    It looks as if your FLAC file had no metadata. Please have a look in your temporary folder (pointed to by the TEMP environment variable.) The easiest way to get to it is to press Win-R, type “%TEMP%” (without quotes) and click OK. Let me know if there is a file called temp.meta and if yes, what is its size.

  16. Pietro Giannini
    03-Mar-2013
    11:12 pm
    16

    Many thanks for this great tool, it is very easy to use and the instructions are very clear .

  17. Brendan
    17-Mar-2013
    6:48 pm
    17

    Love your script, real time saver.
    But since reinstalling Windows (from Windows 8 to Windows 8, so nothing new there, just a reinstall) it’s converting 1 file each time I run it.
    The temp.meta files that are created contain the correct Artist, Album, track nr. etc.
    The files I’m trying to add of course also contain this information.
    Do you have any idea what causes this?

    I’m running Windows 8 and I use your script as per the instructions on this blog.

  18. Dmitry Leskov
    27-Mar-2013
    10:36 pm
    18

    Brendan, I had several reports like this, but I cannot reproduce the problem on my system. Need someone to help me with debugging. Can I email you at the address you specified when posting your comment?

  19. E
    03-May-2013
    12:55 pm
    19

    Really fast and easy to use process. Thanks for creating the batch/js and sharing!

  20. dave granic
    19-May-2013
    11:13 am
    20

    The problem Brendan is having is the same one I am having with Permission Denied when the script tries to execute

    fso.DeleteFile(tempWAVPath)

    I modified the script to show the TempWAVPath and I have set permissions on the folder to allow EVERYONE full control, could the file be open?

    Deleting intermediate track
    Deleting temporary WAV file C:\temp\AddFLACs-2013-05-19-00-08-11\temp.wav
    C:\downloads\install\AddFLACs-0.91\AddFLACs.js(275, 13) Microsoft JScript runtime error: Permission denied

  21. dave granic
    19-May-2013
    11:55 am
    21

    fixed the issue… changed line 276 of js file to

    fso.DeleteFile(tempWAVPath, true)

  22. dave granic
    20-May-2013
    1:27 am
    22

    a recommendation to deal with files not containing meta data

    if (!(‘Title’ in tags && ‘Album’ in tags && ‘Artist’ in tags)) {
    WScript.Echo(“No track title, album, or artist in metadata, skipping”)
    tags['TrackNumber'] = xxTrack
    xxTrack = xxTrack + 1
    tags['Album'] = “unknown”
    tags['Artist'] = “unknown”
    tags['Title'] = file.Name
    // continue folderScanLoop;
    }

  23. dave granic
    20-May-2013
    1:47 am
    23

    another recommended script change to handle empty metadata file

    var metadata = ”
    log (“Reading metadata ” + tempMetadataPath)
    var metadataFile = fso.OpenTextFile(tempMetadataPath,1,false,-1)
    if (metadataFile.size > 0) {
    metadata = metadataFile.ReadAll()
    }
    metadataFile.Close()

  24. ThunderPeel2001
    30-May-2013
    11:34 pm
    24

    Another solution to the PERMISSION DENIED error:

    Add the following lines after line 274:

    file = fso.GetFile(tempWAVPath)
    file.attributes[1] = 0
    if(file.attributes & 1) {
    file.attributes = file.attributes – 1
    }

    The problem with the existing script is that TEMP.WAV is READ ONLY. You don’t need to do change the permissions of your TEMP folder or anything like that, you just need to remove the READ ONLY attribute from the file.

  25. fence
    08-Jun-2013
    5:05 pm
    25

    Outstanding man. much appreciation bro.

  26. Dmitry Leskov
    15-Jun-2013
    11:19 am
    26

    Incorporated your fix. Thanks Dave.

    There is an issue tracker on GitHub, just in case.

  27. Gorilla
    07-Jul-2013
    12:21 pm
    27

    FYI, I just got my hands on some FLACs and found your tool! Works great but it seems the version on GitHub is not the same as the version on your blog (0.92) as I ran into a JS runtime error: “Input past end of File” with the GitHub Version.

  28. Dmitry Leskov
    07-Jul-2013
    2:06 pm
    28

    Gorilla, this is pretty strange. I have just cloned the GitHub repo and it is identical to the version on my blog. Can you find out what exactly was different on your system?

  29. Gorilla
    08-Jul-2013
    1:24 am
    29

    Looking at ur comments it seems I am having the same exact error message John (Nov 11) is having. I downloaded the ZIP of ur repo (i did not clone it). At first it says:

    C:\iTunesScripts-master>cscript /Nologo ”
    C:\iTunesScripts-master\AddFLACs.js”

    C:\Downloads\Gorillaz\
    1
    0:C:\Downloads\Gorillaz\
    Creating folder C:\AppData\Local\Temp\AddFLACs-2013-07-07-14-07-50 f
    or storing temporary files
    Connecting to iTunes COM server…
    Importing FLAC files from Gorillaz
    Scanning folder C:\Downloads\Gorillaz
    Found FLAC file C:\Downloads\Gorillaz1 Tomorrow Comes Today.flac
    Extracting metadata from C:\Downloads\Gorillaz1 Tomorrow Comes Today.flac
    C:\Downloads\iTunesScripts-master\AddFLACs.js(164, 13) Microsoft JScript runtime error: File not found

    Reading your post I put the flac.exe and metaflac.exe files into the same directory as AddFLACs.js. Then I get the same error message as John. To make sure I also copied over the build.bat from the src/ folder into the same directory as AddFLACs.js

    Following up on your question to John I did go to %TEMP% and found your folder with a file “temp.meta” of size 0KB. I also noticed in the ZIP that the “build” batch file in the main directory (size 445 bytes) is of a different size than the one in src folder (size 548 bytes)

    Sorry I would have no idea of how to debug but am giving you all the steps I did so you could recreate the bug.
    Running Windows 7

  30. Chris
    09-Jul-2013
    12:28 am
    30

    You are a gentleman and a scholar, Sir. Your brilliant tool has turned what used to be a time-wasting, exercise in drudgery into a simple mouse-click. The video showing how to install and use the tool is incredibly helpful and it was nice of you to make and include it. Thank you very much.

  31. Dmitry Leskov
    14-Jul-2013
    2:21 pm
    31

    Apparently that FLAC file has no metadata, but the script should not have crashed. I have now fixed that problem, please download the new version and try again.

    Note that the script skips tracks that do not have any of the three required metatags (Artist, Album, and Title) present.

  32. Conor Howard
    25-Jul-2013
    12:43 am
    32

    Thank you for this tool. Easy to use and worked perfect here Windows 7 and latest iTunes. Major time saver!

  33. Impressedperson
    25-Jul-2013
    6:46 am
    33

    This blew my mind. I am speechless. seriously brilliantly done

  34. HAPPY PERSON
    04-Aug-2013
    1:56 pm
    34

    I AM A HAPPY PERSON.
    THIS MADE ME HAPPY.

    THANKYOU!!!

  35. Renato Br
    03-Nov-2013
    8:39 pm
    35

    Realy Great Dude!!!

    Thx alot… just put .FLAC at the folder with

    1- DL this =>dmitryleskov-iTunesScripts-v0.93-0-gdacd6ba

    2- DL this =>AddFLACs-0.93

    Extrac 2 into folder 1 and relace all.

    Copy FLAC files at folder 1… run the AddFLACs.bat

    Job Done ! ! !

    All files automatically get add at Itunes… Good to Go!

    Thx alot ! ! !

    Best Regards from Brazil

  36. Alex
    21-Nov-2013
    2:21 am
    36

    Fanastic program – thanks!

    I’ve run into an odd one today, and I thought you might want to take a look at it. The flac I downloaded from http://psymbionic.bandcamp.com/releases contains metadata for Artist, Title, release date, and artwork, but addscript claims otherwise. Any ideas?

    Creating folder C:\Users\Alex\AppData\Local\Temp\AddFLACs-2013-11-20-13-06-09 for storing temporary files
    Connecting to iTunes COM server…
    Importing FLAC files from FLAC
    Scanning folder C:\FLAC
    Found FLAC file C:\FLAC\Psymbionic – Peter Frampton – Do You Feel Like We Do (Psymbionic Remix).flac
    Extracting metadata from C:\FLAC\Psymbionic – Peter Frampton – Do You Feel Like We Do (Psymbionic Remix).flac
    No track title, album, or artist in metadata, skipping
    Removing temporary folder C:\Users\Alex\AppData\Local\Temp\AddFLACs-2013-11-20-13-06-09

  37. Dmitry Leskov
    21-Nov-2013
    10:01 pm
    37

    Thank you for the nice words, Alex. Please check whether the track in question has all three of Title, Album, and Artist tags. They must all be present.

  38. Tyler
    08-Dec-2013
    1:23 am
    38

    I have 571 flac files (about 13 gb) that i’m trying to add, It adds most of them perfectly, but after a while I get the no title, album, artist error, even though, I have verified that with the files I looked at(~35 or so, 1 or 2 from each album from each artist), they all have all of those tags, although I noticed some had Band instead of Artist which I made an allowance for on line 175 by adding” || ‘BAND’ “. But, that was before I started getting the mentioned error, after giving one or more unrecognized metatag error, where the tag(s) generally varies depending on the source of the files.

  39. Michael
    29-Dec-2013
    1:35 pm
    39

    Thanks! Worked fantastically. Do you have a Paypal link where I could throw you a few dollars?

  40. Ash
    03-Jan-2014
    10:40 pm
    40

    Dmitry,

    Thank you for the excellent work on this program! You have saved me a lot of time searching for and installing programs for this task. Great product!

  41. Timothy Bunston
    25-Mar-2014
    6:22 pm
    41

    I found this script very very useful for my purpose.

    I would like to be able to convert directly to AAC, but this definitely allows me to convert my FLAC collection to iTunes.

  42. Dmitry Leskov
    03-May-2014
    3:58 pm
    42

    Tyler, can you export the metadata from problematic tracks?

    metaflac.exe --export-tags-to=meta.txt --no-utf8-convert problematic-track.flac

  43. Dmitry Leskov
    03-May-2014
    4:22 pm
    43

    Glad it helped you, Michael. No Paypal, sorry, but here is my GitTip: https://www.gittip.com/dmitryleskov/

    You can also send those few dollars to a secular charity of your liking.

  44. Dmitry Leskov
    03-May-2014
    4:42 pm
    44

    Timothy, there is a handful of nice GUI tools that convert FLAC direct to AAC. Most, if not all of them, are commercial, though.

  45. Cosmo Barker
    09-May-2014
    9:56 pm
    45

    Does it work with 24bit 96kHz FLAC? and does it still skip over FLAC with no meteadata. (None of my FLAC have metadata, i always go by filename).

  46. Dmitry Leskov
    15-May-2014
    7:35 pm
    46

    Cosmo, I do not have such FLAC files, sorry. I would expect it to work, but cannot be 100% sure. Also the questions are whether WAV files can hold such audio data, whether iTunes can import such WAV files, and whether ALAC supports such resolution. If you tell me where to get a sample 24-bit 96KHz file, I can check that.

    As far as metadata goes, I actually have a half-baked piece of code that extracts metadata from filenames. Does anyone else need it?

  47. Chad Balkwill
    02-Jul-2014
    2:02 am
    47

    I have put all my FLAC folders into a new folder along with your packet. When I run it, it opens iTunes than the script closes. If iTunes is already open, the script closes instantly; merely flashing on the screen.

  48. Dmitry Leskov
    16-Jul-2014
    11:12 am
    48

    Chad, if you run AddFLACs.bat from the command prompt, what is the error message?

  49. User
    02-Aug-2014
    11:54 pm
    49

    Currently (V1.0) at line 62, “dryRun” variable is declared as “dryrun” (with lower r), causing the script to fail.

  50. Nick Donaldson
    26-Aug-2014
    5:29 am
    50

    You have a typo in the 1.0 version; I tried running your script and got the following error:

    C:\Program Files (x86)\FLAC\AddFLACs.js(360, 13) Microsoft JScript runtime error: ‘dryRun’ is undefined

    Changing the line…
    var dryrun = false

    to…
    var dryRun = false

    … fixed the issue and the script ran perfectly. Thanks!

  51. Dmitry Leskov
    26-Aug-2014
    9:02 am
    51

    Fixed, bumped version to 1.01. Thanks for the reports!

  52. Jeremy
    27-Nov-2014
    1:42 am
    52

    Saved me from buying some ugly software loaded with adware. Thank you very much. Simple and fast!

* Copy This Password *

* Type Or Paste Password Here *