Welcome, guest | Sign In | My Account | Store | Cart

This script would do convert tracks from an audio cd into Apple lossless music using iTunes.

When we have a bad cd rom or a bad disk, conversion would usually fail and iTunes would not complain about it. I usually have to check every converted track manually. This script would save the pain: it would check every converted track, if the duration of the converted track is less than the original track, the script would retry the conversion.

This script is inspired by Fabien C. 's Recipe 498241.

Python, 64 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
#coding=utf-8
import os
import sys

from time import sleep
from win32com.client import Dispatch


def exit(message):
    confirm = raw_input(message)
    sys.exit(1)

def main():
    itunes = Dispatch("iTunes.Application")
    for source in itunes.Sources:
        if source.Kind == 3:
            break
    else:
        exit("audio CD not found. Enter to exit.")
    if itunes.CurrentEncoder.Name.lower() != "lossless encoder":
        exit("Bad encoder, switch to ALAC please. Enter to exit.")
    playlist = source.Playlists[0]
    # convert tracks in audio cd.
    status = itunes.ConvertTracks(playlist.Tracks)
    while status.InProgress:
        sleep(1)

    all_tracks = itunes.LibraryPlaylist.Tracks
    album_name = playlist.Name
    for cd_track in playlist.Tracks:
        for track in reversed(all_tracks):
            if track.Name == cd_track.Name and track.Album == album_name:
                print track.Duration == cd_track.Duration, track.Name, track.Duration, cd_track.Duration
                if track.Duration != cd_track.Duration:
                    # Convert again. 
                    retry = 0
                    while track.Duration != cd_track.Duration and retry < 10:
                        # delete bad converted track.
                        print "retrying %s, count=%s" % (track.Name, retry)
                        retry += 1
                        os.remove(track.Location)
                        track.Delete()
                        # Convert again.
                        status = itunes.ConvertTrack(cd_track)
                        while status.InProgress:
                            sleep(0.5)
                        for t in reversed(all_tracks):
                            if t.Name == cd_track.Name and t.Album == album_name:
                                track = t
                                break
                        else:
                            print "track '%s' not found." % track.Name
                    if retry >= 10:
                        confirm = raw_input("'%s' not converted." % track.Name)
                break
        else:
            print "track '%s' not found." % track.Name

    raw_input("all done.")


if __name__ == "__main__":
    main()
Created by Kai Xia on Wed, 12 Oct 2011 (MIT)
Python recipes (4591)
Kai Xia's recipes (1)

Required Modules

Other Information and Tasks