public
Description: A collection of audio handling programs which work from the command line.
Homepage: http://audiotools.sourceforge.net
Clone URL: git://github.com/tuffy/python-audio-tools.git
python-audio-tools / cd2xmcd
100755 159 lines (123 sloc) 5.485 kb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
 
#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007-2009 Brian Langenberger
 
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
 
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
 
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 
import sys
import audiotools
import audiotools.cdio as cdio
import gettext
 
gettext.install("audiotools",unicode=True)
 
if (__name__ == '__main__'):
    parser = audiotools.OptionParser(
        usage=_(u"%prog [-x filename] [--cdrom CDROM device]"),
        version="Python Audio Tools %s" % (audiotools.VERSION))
 
    parser.add_option('-V','--verbose',
                      action='store',
                      dest='verbosity',
                      choices=["quiet","normal","debug"],
                      default="normal",
                      help=_(u'the verbosity level to execute at'))
 
    parser.add_option('-x','--xmcd',action='store',
                      type='string',dest='xmcd',default='-',
                      metavar='FILENAME',
                      help=_(u'FreeDB XMCD file or MusicBrainz XML output file'))
 
    parser.add_option('-c','--cdrom',action='store',
                      type='string',dest='cdrom',
                      default=audiotools.DEFAULT_CDROM)
 
    server = audiotools.OptionGroup(parser,_(u"Server Options"))
 
    server.add_option('--musicbrainz-server',action='store',
                      type='string',dest='musicbrainz_server',
                      default=audiotools.MUSICBRAINZ_SERVER,
                      metavar='HOSTNAME')
    server.add_option('--musicbrainz-port',action='store',
                      type='int',dest='musicbrainz_port',
                      default=audiotools.MUSICBRAINZ_PORT,
                      metavar='PORT')
 
    server.add_option('--freedb-server',action='store',
                      type='string',dest='freedb_server',
                      default=audiotools.FREEDB_SERVER,
                      metavar='HOSTNAME')
    server.add_option('--freedb-port',action='store',
                      type='int',dest='freedb_port',
                      default=audiotools.FREEDB_PORT,
                      metavar='PORT')
 
    server.add_option('--no-musicbrainz',action='store_false',
                      dest='use_musicbrainz',default=True,
                      help='do not query MusicBrainz for metadata')
    server.add_option('--no-freedb',action='store_false',
                      dest='use_freedb',default=True,
                      help='do not query FreeDB for metadata')
 
    server.add_option('-D','--default',
                      action='store_const',const=1,default=None,
                      help=_(u'when multiple choices are available, select the first one automatically'))
 
    parser.add_option_group(server)
 
    (options,args) = parser.parse_args()
    msg = audiotools.Messenger("cd2xmcd",options)
 
    try:
      if (options.xmcd == '-'):
         outfile = sys.stdout
      else:
         outfile = open(options.xmcd,"w")
    except IOError,err:
      msg.error(_(u"Unable to write \"%s\"") % (msg.filename(options.xmcd)))
      sys.exit(1)
 
    cdda = audiotools.CDDA(options.cdrom)
 
    if (len(cdda) == 255):
         msg.error(_(u"No CD in drive"))
         sys.exit(1)
 
    tracks = list(cdda)
 
    musicbrainz_discid = audiotools.MBDiscID.from_cdda(cdda)
 
    freedb_discid = audiotools.DiscID.from_cdda(cdda)
 
    msg.info(_(u"Found CD information"))
 
    returnval = 0
    musicbrainz_matches = 0
    freedb_matches = 0
    if (options.use_musicbrainz):
        try:
            musicbrainz_matches = audiotools.get_mbxml(
                musicbrainz_discid,
                outfile,
                options.musicbrainz_server,
                options.musicbrainz_port,
                msg,
                options.default)
            returnval = 0
        except IOError,e:
            msg.error(unicode(e))
            returnval = 1
 
    if ((musicbrainz_matches == 0) and options.use_freedb):
        try:
            freedb_matches = audiotools.get_xmcd(
                freedb_discid,
                outfile,
                options.freedb_server,
                options.freedb_port,
                msg,
                options.default)
            returnval = 0
        except IOError,e:
            msg.error(unicode(e))
            returnval = 1
 
    #if musicbrainz_matches and freedb_matches are both 0
    #output a MusicBrainzReleaseXML file instead of a FreeDB one
    #since XML is better for editing by end users
    #(none of that " / "-handling nonsense)
    if ((musicbrainz_matches == 0) and (freedb_matches == 0)):
        if (options.use_musicbrainz):
            musicbrainz_discid.toxml(outfile)
        else:
            freedb_discid.toxmcd(outfile)
        msg.info(_(u"%s written") % (msg.filename(options.xmcd)))
        outfile.close()
 
    if (returnval != 0):
        sys.exit(1)