annotate amakode.py @ 8:e2ef50b914fd default tip

Actually we need Python 2.4 for subprocess.
author darius@inchoate.localdomain
date Mon, 19 Nov 2007 17:25:28 +1030
parents bdb8597191dc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
1 #!/usr/bin/env python
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
2
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
3 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
4 # Transcoder for Amarok
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
5 # - Add support for tagging (jens.zurheide@gmx.de)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
6 # - Fixed typo in lame encoder (tcuya from kde-apps.org)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
7 # - Made setting maxjobs easier, although Amarok doesn't appear to issue
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
8 # multiple requests :(
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
9 #
8
e2ef50b914fd Actually we need Python 2.4 for subprocess.
darius@inchoate.localdomain
parents: 6
diff changeset
10 # Depends on: Python 2.4
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
11 # tagpy (optional)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
12 #
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
13 # The only user servicable parts are the encode/decode (line 103) and the
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
14 # number of concurrent jobs to run (line 225)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
15 #
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
16 # The optional module tagpy (http://news.tiker.net/software/tagpy) is used
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
17 # for tag information processing. This allows for writing tags into the
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
18 # transcoded files.
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
19 #
6
bdb8597191dc Mention Mercurial repo location.
darius@inchoate.localdomain
parents: 5
diff changeset
20 # Mercurial repo available at http://www.dons.net.au/~darius/hgwebdir.cgi/amakode/
bdb8597191dc Mention Mercurial repo location.
darius@inchoate.localdomain
parents: 5
diff changeset
21 #
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
22 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
23 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
24 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
25 # Copyright (C) 2007 Jens Zurheide. All rights reserved.
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
26 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
27 # Redistribution and use in source and binary forms, with or without
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
28 # modification, are permitted provided that the following conditions
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
29 # are met:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
30 # 1. Redistributions of source code must retain the above copyright
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
31 # notice, this list of conditions and the following disclaimer.
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
32 # 2. Redistributions in binary form must reproduce the above copyright
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
33 # notice, this list of conditions and the following disclaimer in the
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
34 # documentation and/or other materials provided with the distribution.
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
35 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
36 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
37 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
38 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
39 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
40 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
41 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
42 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
43 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
44 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
45 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
46 # SUCH DAMAGE.
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
47 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
48 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
49
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
50 __version__ = "1.3"
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
51
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
52 import ConfigParser
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
53 import os
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
54 import sys
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
55 import string
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
56 import signal
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
57 import logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
58 import select
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
59 import subprocess
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
60 import tempfile
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
61 from logging.handlers import RotatingFileHandler
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
62 import urllib
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
63 import urlparse
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
64 import re
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
65
5
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
66 try:
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
67 import tagpy
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
68 have_tagpy = True
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
69 except ImportError, e:
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
70 have_tagpy = False
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
71
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
72 class tagpywrap(dict):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
73 textfields = ['album', 'artist', 'title', 'comment', 'genre']
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
74 numfields = ['year', 'track']
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
75 allfields = textfields + numfields
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
76
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
77 def __init__(self, url):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
78 f = urllib.urlopen(url)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
79
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
80 self.tagInfo = tagpy.FileRef(f.fp.name).tag()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
81 f.close()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
82
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
83 self['album'] = self.tagInfo.album.strip()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
84 self['artist'] = self.tagInfo.artist.strip()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
85 self['title'] = self.tagInfo.title.strip()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
86 self['comment'] = self.tagInfo.comment.strip()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
87 self['year'] = self.tagInfo.year
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
88 self['genre'] = self.tagInfo.genre.strip()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
89 self['track'] = self.tagInfo.track
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
90 for i in self.textfields:
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
91 if (self[i] == ""):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
92 del self[i]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
93
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
94 for i in self.numfields:
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
95 if (self[i] == 0):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
96 del self[i]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
97
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
98 class QueueMgr(object):
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
99 queuedjobs = []
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
100 activejobs = []
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
101
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
102 def __init__(self, callback = None, maxjobs = 2):
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
103 self.callback = callback
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
104 self.maxjobs = maxjobs
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
105 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
106
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
107 def add(self, job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
108 log.debug("Job added")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
109 self.queuedjobs.append(job)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
110
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
111 def poll(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
112 """ Poll active jobs and check if we should make a new job active """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
113 if (len(self.activejobs) == 0):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
114 needajob = True
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
115 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
116 needajob = False
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
117
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
118 for j in self.activejobs:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
119 if j.isfinished():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
120 log.debug("job is done")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
121 needajob = True
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
122 self.activejobs.remove(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
123 if (self.callback != None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
124 self.callback(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
125
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
126 if needajob:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
127 #log.debug("Number of queued jobs = " + str(len(self.queuedjobs)) + ", number of active jobs = " + str(len(self.activejobs)))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
128 while len(self.queuedjobs) > 0 and len(self.activejobs) < self.maxjobs:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
129 newjob = self.queuedjobs.pop(0)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
130 newjob.start()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
131 self.activejobs.append(newjob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
132
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
133 def isidle(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
134 """ Returns true if both queues are empty """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
135 return(len(self.queuedjobs) == 0 and len(self.activejobs) == 0)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
136
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
137 class TranscodeJob(object):
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
138 # Programs used to decode (to a wav stream)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
139 decode = {}
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
140 decode["mp3"] = ["mpg123", "-w", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
141 decode["ogg"] = ["ogg123", "-d", "wav", "-f", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
142 # XXX: this is really fugly but faad refuses to read from a pipe
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
143 decode["mp4"] = ["env", "MPLAYER_VERBOSE=-100", "mplayer", "-ao", "pcm:file=/dev/stdout", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
144 decode["m4a"] = decode["mp4"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
145 decode["flac"] = ["flac", "-d", "-c", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
146
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
147 # Programs used to encode (from a wav stream)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
148 encode = {}
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
149 encode["mp3"] = ["lame", "--abr", "128", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
150 encode["ogg"] = ["oggenc", "-q", "2", "-"]
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
151 encode["mp4"] = ["faac", "-wo", "/dev/stdout", "-"]
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
152 encode["m4a"] = encode["mp4"]
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
153
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
154 # XXX: can't encode flac - it's wav parser chokes on mpg123's output, it does work
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
155 # OK if passed through sox but we can't do that. If you really want flac modify
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
156 # the code & send me a diff or write a wrapper shell script :)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
157 #encode["flac"] = ["flac", "-c", "-"]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
158
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
159 # Options for output programs to store ID3 tag information
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
160 tagopt = {}
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
161 tagopt["mp3"] = { "album" : "--tl", "artist" : "--ta", "title" : "--tt", "track" : "--tn" }
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
162 tagopt["ogg"] = { "album" : "-l", "artist" : "-a", "title" : "-a", "track" : "-N" }
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
163 tagopt["mp4"] = { "album" : "--album", "artist" : "--artist", "title" : "--title", "track" : "--track" }
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
164 #tagopt["flac"] = { "album" : "-Talbum=%s", "artist" : "-Tartist=%s", "title" : "-Ttitle=%s", "track" : "-Ttracknumber=%s" }
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
165
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
166 def __init__(self, _inurl, _tofmt):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
167 self.errormsg = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
168 log.debug("Creating job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
169 self.inurl = _inurl
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
170 self.tofmt = string.lower(_tofmt)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
171 self.inext = string.lower(string.rsplit(self.inurl, ".", 1)[1])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
172 if (self.inext in self.decode):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
173 log.debug("can decode with " + str(self.decode[self.inext]))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
174 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
175 log.debug("unable to decode " + self.inext)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
176 raise KeyError("no available decoder")
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
177
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
178 if (self.tofmt in self.encode):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
179 log.debug("can encode with " + str(self.encode[self.tofmt]))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
180 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
181 log.debug("unable to encode " + self.tofmt)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
182 raise KeyError("no available encoder")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
183
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
184 def start(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
185 log.debug("Starting job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
186 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
187 self.inputfile = urllib.urlopen(self.inurl)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
188 self.outfd, self.outfname = tempfile.mkstemp(prefix="transcode-", suffix="." + self.tofmt)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
189 #self.outfname = string.join(string.rsplit(self.inurl, ".")[:-1] + [self.tofmt], ".")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
190
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
191 self.errfh, self.errfname = tempfile.mkstemp(prefix="transcode-", suffix=".log")
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
192 self.outurl = urlparse.urlunsplit(["file", None, self.outfname, None, None])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
193 log.debug("Outputting to " + self.outfname + " " + self.outurl + ")")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
194 log.debug("Errors to " + self.errfname)
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
195
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
196 # assemble command line for encoder
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
197 encoder = []
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
198 encoder += self.encode[self.tofmt]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
199
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
200 try:
5
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
201 if (have_tagpy and self.tofmt in self.tagopt):
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
202 taginfo = tagpywrap(self.inurl)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
203 for f in taginfo.allfields:
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
204 if (f in taginfo and f in self.tagopt[self.tofmt]):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
205 inf = taginfo[f]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
206 opt = self.tagopt[self.tofmt][f]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
207 log.debug(" %s = %s %s" % (f, opt, inf))
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
208 # If we have a substitution, make it. If
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
209 # not append the info as a separate
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
210 # arg. Note that the tag options are
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
211 # passed in as the second option because a
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
212 # lot of programs don't parse options
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
213 # after their file list.
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
214 if ('%s' in opt):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
215 opt = opt.replace('%s', inf)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
216 encoder.insert(1, opt)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
217 else:
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
218 encoder.insert(1, opt)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
219 encoder.insert(2, inf)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
220 finally:
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
221 pass
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
222
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
223 log.debug("decoder -> " + str(self.decode[self.inext]))
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
224 log.debug("encoder -> " + str(encoder))
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
225 self.decoder = subprocess.Popen(self.decode[self.inext], stdin=self.inputfile, stdout=subprocess.PIPE, stderr=self.errfh)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
226 self.encoder = subprocess.Popen(encoder, stdin=self.decoder.stdout, stdout=self.outfd, stderr=self.errfh)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
227 log.debug("Processes connected")
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
228 except Exception, e:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
229 log.debug("Failed to start - " + str(e))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
230 self.errormsg = str(e)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
231 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
232 os.unlink(self.outfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
233 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
234 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
235
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
236 def isfinished(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
237 if (self.errormsg != None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
238 return(True)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
239
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
240 rtn = self.encoder.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
241 if (rtn == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
242 return(False)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
243
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
244 if (rtn == 0):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
245 os.unlink(self.errfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
246 self.errormsg = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
247 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
248 log.debug("error in transcode, please review " + self.errfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
249 self.errormsg = "Unable to transcode, please review " + self.errfname
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
250 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
251 os.unlink(self.outfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
252 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
253 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
254
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
255 return(True)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
256
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
257 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
258 # amaKode
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
259 ############################################################################
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
260 class amaKode(object):
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
261 """ The main application"""
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
262
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
263 def __init__(self, args):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
264 """ Main loop waits for something to do then does it """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
265 log.debug("Started.")
5
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
266 if (have_tagpy):
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
267 log.debug("Using tagpy")
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
268 else:
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
269 log.debug("Warning: tagpy is unavailable")
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
270
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
271 self.readSettings()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
272
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
273 self.queue = QueueMgr(callback = self.notify, maxjobs = 1)
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
274
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
275 while True:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
276 # Check for finished jobs, etc
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
277 self.queue.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
278 # Check if there's anything waiting on stdin
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
279 res = select.select([sys.stdin.fileno()], [], [], 0.1)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
280 if (sys.stdin.fileno() in res[0]):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
281 # Let's hope we got a whole line or we stall here
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
282 line = sys.stdin.readline()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
283 if line:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
284 self.customEvent(line)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
285 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
286 break
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
287
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
288 def readSettings(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
289 """ Reads settings from configuration file """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
290
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
291 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
292 foovar = config.get("General", "foo")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
293
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
294 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
295 log.debug("No config file found, using defaults.")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
296
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
297 def customEvent(self, string):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
298 """ Handles notifications """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
299
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
300 #log.debug("Received notification: " + str(string))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
301
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
302 if string.find("transcode") != -1:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
303 self.transcode(str(string))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
304
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
305 if string.find("quit") != -1:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
306 self.quit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
307
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
308 def transcode(self, line):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
309 """ Called when requested to transcode a track """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
310 args = string.split(line)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
311 if (len(args) != 3):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
312 log.debug("Invalid transcode command")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
313 return
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
314
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
315 log.debug("transcoding " + args[1] + " to " + args[2])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
316 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
317 newjob = TranscodeJob(args[1], args[2])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
318 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
319 log.debug("Can't create transcoding job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
320 os.system("dcop amarok mediabrowser transcodingFinished " + re.escape(args[1]) + "\"\"")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
321
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
322 self.queue.add(newjob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
323
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
324 def notify(self, job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
325 """ Report to amarok that the job is done """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
326 if (job.errormsg == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
327 log.debug("Job " + job.inurl + " completed successfully")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
328 os.system("dcop amarok mediabrowser transcodingFinished " + re.escape(job.inurl) + " " + re.escape(job.outurl))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
329 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
330 log.debug("Job " + job.inurl + " failed - " + job.errormsg)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
331 os.system("dcop amarok mediabrowser transcodingFinished " + re.escape(job.inurl) + "\"\"")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
332
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
333 def quit(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
334 log.debug("quitting")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
335 sys.exit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
336
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
337 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
338
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
339 def debug(message):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
340 """ Prints debug message to stdout """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
341 log.debug(message)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
342
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
343 def onStop(signum, stackframe):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
344 """ Called when script is stopped by user """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
345 log.debug("signalled exit")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
346 sys.exit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
347
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
348 def initLog():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
349 # Init our logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
350 global log
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
351 log = logging.getLogger("amaKode")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
352 # Default to warts and all logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
353 log.setLevel(logging.DEBUG)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
354
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
355 # Log to this file
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
356 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/amakode.log",
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
357 maxBytes = 10000, backupCount = 3)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
358
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
359 # And stderr
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
360 logstderr = logging.StreamHandler()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
361
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
362 # Format it nicely
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
363 formatter = logging.Formatter("[%(name)s] %(message)s")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
364
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
365 # Glue it all together
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
366 logfile.setFormatter(formatter)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
367 logstderr.setFormatter(formatter)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
368 log.addHandler(logfile)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
369 log.addHandler(logstderr)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
370 return(log)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
371
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
372 def reportJob(job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
373 """ Report to amarok that the job is done """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
374 if (job.errormsg == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
375 log.debug("Job " + job.inurl + " completed successfully")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
376 log.debug("dcop amarok mediabrowser transcodingFinished " + job.inurl + " " + job.outurl)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
377 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
378 log.debug("Job " + job.inurl + " failed - " + job.errormsg)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
379 log.debug("dcop amarok mediabrowser transcodingFinished " + job.inurl + "\"\"")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
380
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
381 if __name__ == "__main__":
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
382 initLog()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
383 signal.signal(signal.SIGINT, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
384 signal.signal(signal.SIGHUP, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
385 signal.signal(signal.SIGTERM, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
386 if 1:
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
387 # Run normal application
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
388 app = amaKode(sys.argv)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
389 else:
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
390 # Quick test case
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
391 q = QueueMgr(reportJob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
392 j = TranscodeJob("file:///tmp/test.mp3", "ogg")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
393 q.add(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
394 j2 = TranscodeJob("file:///tmp/test2.mp3", "m4a")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
395 q.add(j2)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
396 while not q.isidle():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
397 q.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
398 res = select.select([], [], [], 1)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
399
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
400 log.debug("jobs all done")