annotate amakode.py @ 1:07e3d8655a29 AMAKODE_1_0

Initial import of Amakode a transcoding script for Amarok written entirely in Python.
author darius
date Sat, 31 Mar 2007 02:09:51 +0000
parents
children de86a9e19151
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
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
5 # (c) 2007 Daniel O'Connor <darius@dons.net.au>
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
6 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
7 # Depends on: Python 2.2
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
8 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
9 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
10 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
11 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
12 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
13 # 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
14 # modification, are permitted provided that the following conditions
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
15 # are met:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
16 # 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
17 # 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
18 # 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
19 # 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
20 # 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
21 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
22 # 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
23 # 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
24 # 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
25 # 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
26 # 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
27 # 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
28 # 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
29 # 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
30 # 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
31 # 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
32 # SUCH DAMAGE.
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
33 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
34 ############################################################################
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 import ConfigParser
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
37 import os
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
38 import sys
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
39 import string
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
40 import signal
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
41 import logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
42 import select
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
43 import subprocess
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
44 import tempfile
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
45 from logging.handlers import RotatingFileHandler
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
46 import urllib
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
47 import urlparse
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
48 import re
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
49
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
50 class QueueMgr:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
51 queuedjobs = []
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
52 activejobs = []
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
53 maxjobs = 1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
54
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
55 def __init__(self, _callback):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
56 self.callback = _callback
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
57 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
58
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
59 def add(self, job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
60 log.debug("Job added")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
61 self.queuedjobs.append(job)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
62
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
63 def poll(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
64 """ 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
65 if (len(self.activejobs) == 0):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
66 needajob = True
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
67 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
68 needajob = False
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
69
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
70 for j in self.activejobs:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
71 if j.isfinished():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
72 log.debug("job is done")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
73 needajob = True
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
74 self.activejobs.remove(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
75 if (self.callback != None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
76 self.callback(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
77
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
78 if needajob:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
79 #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
80 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
81 newjob = self.queuedjobs.pop(0)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
82 newjob.start()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
83 self.activejobs.append(newjob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
84
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
85 def isidle(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
86 """ Returns true if both queues are empty """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
87 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
88
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
89 class TranscodeJob:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
90 # Programs used to decode (to a wav stream)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
91 decode = {}
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
92 decode["mp3"] = ["mpg123", "-w", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
93 decode["ogg"] = ["ogg123", "-d", "wav", "-f", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
94 # 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
95 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
96 decode["m4a"] = decode["mp4"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
97 decode["flac"] = ["flac", "-d", "-c", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
98
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
99 # Programs used to encode (from a wav stream)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
100 encode = {}
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
101 encode["mp3"] = ["lame", "--abr", "128", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
102 encode["ogg"] = ["oggenc", "-q", "2", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
103 encode["mp4"] = ["faac", "-o", "/dev/stdout", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
104 encode["m4a"] = encode["mp4"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
105 encode["flac"] = ["flac", "-c", "-"]
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 __init__(self, _inurl, _tofmt):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
108 self.outfname = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
109 self.errfname = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
110 self.errormsg = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
111 log.debug("Creating job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
112 self.inurl = _inurl
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
113 self.tofmt = string.lower(_tofmt)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
114 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
115 if (self.inext in self.decode):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
116 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
117 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
118 log.debug("unable to decode " + self.inext)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
119 raise KeyError("no available decoder")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
120
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
121 if (self.tofmt in self.encode):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
122 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
123 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
124 log.debug("unable to encode " + self.tofmt)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
125 raise KeyError("no available encoder")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
126
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
127 def start(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
128 log.debug("Starting job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
129 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
130 self.inputfile = urllib.urlopen(self.inurl)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
131 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
132 #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
133
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
134 self.errfd, self.errfname = tempfile.mkstemp(prefix="transcode-", suffix=".log")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
135 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
136 log.debug("Outputting to " + self.outfname + " " + self.outurl + ")")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
137 log.debug("Errors to " + self.errfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
138 self.decoder = subprocess.Popen(self.decode[self.inext], stdin=self.inputfile, stdout=subprocess.PIPE, stderr=self.errfd)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
139 self.encoder = subprocess.Popen(self.encode[self.tofmt], stdin=self.decoder.stdout, stdout=self.outfd, stderr=self.errfd)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
140 except Exception, e:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
141 log.debug("Failed to start - " + str(e))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
142 self.errormsg = str(e)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
143 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
144 os.unlink(self.outfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
145 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
146 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
147
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
148 def isfinished(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
149 if (self.errormsg != None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
150 return(True)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
151
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
152 rtn = self.encoder.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
153 if (rtn == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
154 return(False)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
155
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
156 os.close(self.errfd)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
157 os.close(self.outfd)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
158
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
159 if (rtn == 0):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
160 os.unlink(self.errfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
161 self.errormsg = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
162 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
163 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
164 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
165 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
166 os.unlink(self.outfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
167 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
168 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
169
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
170 return(True)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
171
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
172 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
173 # amaKode
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
174 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
175 class amaKode:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
176 """ The main application"""
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
177
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
178 def __init__(self, args):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
179 """ 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
180 log.debug("Started.")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
181
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
182 self.readSettings()
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 self.queue = QueueMgr(self.notify)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
185
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
186 while True:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
187 # Check for finished jobs, etc
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
188 self.queue.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
189 # Check if there's anything waiting on stdin
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
190 res = select.select([sys.stdin.fileno()], [], [], 0.1)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
191 if (sys.stdin.fileno() in res[0]):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
192 # 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
193 line = sys.stdin.readline()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
194 if line:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
195 self.customEvent(line)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
196 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
197 break
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
198
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
199 def readSettings(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
200 """ Reads settings from configuration file """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
201
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
202 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
203 foovar = config.get("General", "foo")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
204
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
205 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
206 log.debug("No config file found, using defaults.")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
207
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
208 def customEvent(self, string):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
209 """ Handles notifications """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
210
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
211 #log.debug("Received notification: " + str(string))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
212
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
213 if string.find("transcode") != -1:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
214 self.transcode(str(string))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
215
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
216 if string.find("quit") != -1:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
217 self.quit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
218
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
219 def transcode(self, line):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
220 """ Called when requested to transcode a track """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
221 args = string.split(line)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
222 if (len(args) != 3):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
223 log.debug("Invalid transcode command")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
224 return
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
225
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
226 log.debug("transcoding " + args[1] + " to " + args[2])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
227 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
228 newjob = TranscodeJob(args[1], args[2])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
229 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
230 log.debug("Can't create transcoding job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
231 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
232
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
233 self.queue.add(newjob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
234
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
235 def notify(self, job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
236 """ Report to amarok that the job is done """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
237 if (job.errormsg == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
238 log.debug("Job " + job.inurl + " completed successfully")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
239 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
240 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
241 log.debug("Job " + job.inurl + " failed - " + job.errormsg)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
242 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
243
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
244 def quit(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
245 log.debug("quitting")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
246 sys.exit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
247
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
248 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
249
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
250 def debug(message):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
251 """ Prints debug message to stdout """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
252 log.debug(message)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
253
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
254 def onStop(signum, stackframe):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
255 """ Called when script is stopped by user """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
256 log.debug("signalled exit")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
257 sys.exit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
258
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
259 def initLog():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
260 # Init our logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
261 global log
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
262 log = logging.getLogger("amaKode")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
263 # Default to warts and all logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
264 log.setLevel(logging.DEBUG)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
265
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
266 # Log to this file
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
267 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/amakode.log",
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
268 maxBytes = 10000, backupCount = 3)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
269
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
270 # And stderr
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
271 logstderr = logging.StreamHandler()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
272
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
273 # Format it nicely
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
274 formatter = logging.Formatter("[%(name)s] %(message)s")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
275
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
276 # Glue it all together
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
277 logfile.setFormatter(formatter)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
278 logstderr.setFormatter(formatter)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
279 log.addHandler(logfile)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
280 log.addHandler(logstderr)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
281 return(log)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
282
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
283 def reportJob(job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
284 """ Report to amarok that the job is done """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
285 if (job.errormsg == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
286 log.debug("Job " + job.inurl + " completed successfully")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
287 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
288 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
289 log.debug("Job " + job.inurl + " failed - " + job.errormsg)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
290 log.debug("dcop amarok mediabrowser transcodingFinished " + job.inurl + "\"\"")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
291
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
292 if __name__ == "__main__":
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
293 initLog()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
294 signal.signal(signal.SIGINT, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
295 signal.signal(signal.SIGHUP, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
296 signal.signal(signal.SIGTERM, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
297 if 1:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
298 app = amaKode(sys.argv)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
299 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
300 q = QueueMgr(reportJob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
301 j = TranscodeJob("file:///tmp/test.mp3", "ogg")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
302 q.add(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
303 j2 = TranscodeJob("file:///tmp/test2.mp3", "m4a")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
304 q.add(j2)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
305 while not q.isidle():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
306 q.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
307 res = select.select([], [], [], 1)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
308
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
309 log.debug("jobs all done")