annotate amakode.py @ 5:f11c5ed0178e

Make using tagpy optional.
author darius@inchoate.localdomain
date Mon, 12 Nov 2007 15:17:20 +1030
parents 65a9f99302cd
children bdb8597191dc
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 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
10 # Depends on: Python 2.2
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 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
20 ############################################################################
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 # 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
23 # 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
24 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
25 # 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
26 # modification, are permitted provided that the following conditions
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
27 # are met:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
28 # 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
29 # 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
30 # 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
31 # 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
32 # 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
33 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
34 # 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
35 # 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
36 # 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
37 # 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
38 # 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
39 # 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
40 # 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
41 # 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
42 # 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
43 # 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
44 # SUCH DAMAGE.
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
45 #
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
46 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
47
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
48 __version__ = "1.3"
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
49
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
50 import ConfigParser
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
51 import os
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
52 import sys
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
53 import string
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
54 import signal
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
55 import logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
56 import select
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
57 import subprocess
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
58 import tempfile
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
59 from logging.handlers import RotatingFileHandler
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
60 import urllib
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
61 import urlparse
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
62 import re
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
63
5
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
64 try:
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
65 import tagpy
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
66 have_tagpy = True
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
67 except ImportError, e:
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
68 have_tagpy = False
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
69
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
70 class tagpywrap(dict):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
71 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
72 numfields = ['year', 'track']
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
73 allfields = textfields + numfields
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
74
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
75 def __init__(self, url):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
76 f = urllib.urlopen(url)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
77
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
78 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
79 f.close()
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
80
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 if (self[i] == ""):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
90 del self[i]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
91
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
92 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
93 if (self[i] == 0):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
94 del self[i]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
95
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
96 class QueueMgr(object):
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
97 queuedjobs = []
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
98 activejobs = []
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
99
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
100 def __init__(self, callback = None, maxjobs = 2):
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
101 self.callback = callback
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
102 self.maxjobs = maxjobs
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
103 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
104
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
105 def add(self, job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
106 log.debug("Job added")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
107 self.queuedjobs.append(job)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
108
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
109 def poll(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
110 """ 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
111 if (len(self.activejobs) == 0):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
112 needajob = True
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
113 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
114 needajob = False
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
115
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
116 for j in self.activejobs:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
117 if j.isfinished():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
118 log.debug("job is done")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
119 needajob = True
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
120 self.activejobs.remove(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
121 if (self.callback != None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
122 self.callback(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
123
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
124 if needajob:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
125 #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
126 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
127 newjob = self.queuedjobs.pop(0)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
128 newjob.start()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
129 self.activejobs.append(newjob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
130
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
131 def isidle(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
132 """ Returns true if both queues are empty """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
133 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
134
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
135 class TranscodeJob(object):
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
136 # Programs used to decode (to a wav stream)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
137 decode = {}
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
138 decode["mp3"] = ["mpg123", "-w", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
139 decode["ogg"] = ["ogg123", "-d", "wav", "-f", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
140 # 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
141 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
142 decode["m4a"] = decode["mp4"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
143 decode["flac"] = ["flac", "-d", "-c", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
144
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
145 # Programs used to encode (from a wav stream)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
146 encode = {}
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
147 encode["mp3"] = ["lame", "--abr", "128", "-", "-"]
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
148 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
149 encode["mp4"] = ["faac", "-wo", "/dev/stdout", "-"]
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
150 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
151
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
152 # 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
153 # 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
154 # 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
155 #encode["flac"] = ["flac", "-c", "-"]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
156
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
157 # 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
158 tagopt = {}
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
159 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
160 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
161 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
162 #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
163
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
164 def __init__(self, _inurl, _tofmt):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
165 self.errormsg = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
166 log.debug("Creating job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
167 self.inurl = _inurl
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
168 self.tofmt = string.lower(_tofmt)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
169 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
170 if (self.inext in self.decode):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
171 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
172 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
173 log.debug("unable to decode " + self.inext)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
174 raise KeyError("no available decoder")
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
175
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
176 if (self.tofmt in self.encode):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
177 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
178 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
179 log.debug("unable to encode " + self.tofmt)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
180 raise KeyError("no available encoder")
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 def start(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
183 log.debug("Starting job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
184 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
185 self.inputfile = urllib.urlopen(self.inurl)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
186 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
187 #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
188
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
189 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
190 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
191 log.debug("Outputting to " + self.outfname + " " + self.outurl + ")")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
192 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
193
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
194 # 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
195 encoder = []
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
196 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
197
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
198 try:
5
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
199 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
200 taginfo = tagpywrap(self.inurl)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
201 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
202 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
203 inf = taginfo[f]
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
204 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
205 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
206 # 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
207 # 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
208 # 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
209 # 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
210 # 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
211 # after their file list.
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
212 if ('%s' in opt):
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
213 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
214 encoder.insert(1, opt)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
215 else:
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 encoder.insert(2, inf)
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
218 finally:
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
219 pass
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
220
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
221 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
222 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
223 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
224 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
225 log.debug("Processes connected")
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
226 except Exception, e:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
227 log.debug("Failed to start - " + str(e))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
228 self.errormsg = str(e)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
229 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
230 os.unlink(self.outfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
231 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
232 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
233
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
234 def isfinished(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
235 if (self.errormsg != None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
236 return(True)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
237
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
238 rtn = self.encoder.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
239 if (rtn == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
240 return(False)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
241
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
242 if (rtn == 0):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
243 os.unlink(self.errfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
244 self.errormsg = None
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
245 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
246 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
247 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
248 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
249 os.unlink(self.outfname)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
250 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
251 pass
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
252
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
253 return(True)
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 ############################################################################
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
256 # amaKode
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
257 ############################################################################
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
258 class amaKode(object):
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
259 """ The main application"""
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
260
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
261 def __init__(self, args):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
262 """ 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
263 log.debug("Started.")
5
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
264 if (have_tagpy):
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
265 log.debug("Using tagpy")
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
266 else:
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
267 log.debug("Warning: tagpy is unavailable")
f11c5ed0178e Make using tagpy optional.
darius@inchoate.localdomain
parents: 4
diff changeset
268
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
269 self.readSettings()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
270
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
271 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
272
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
273 while True:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
274 # Check for finished jobs, etc
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
275 self.queue.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
276 # Check if there's anything waiting on stdin
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
277 res = select.select([sys.stdin.fileno()], [], [], 0.1)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
278 if (sys.stdin.fileno() in res[0]):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
279 # 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
280 line = sys.stdin.readline()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
281 if line:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
282 self.customEvent(line)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
283 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
284 break
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
285
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
286 def readSettings(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
287 """ Reads settings from configuration file """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
288
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
289 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
290 foovar = config.get("General", "foo")
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 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
293 log.debug("No config file found, using defaults.")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
294
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
295 def customEvent(self, string):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
296 """ Handles notifications """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
297
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
298 #log.debug("Received notification: " + str(string))
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 if string.find("transcode") != -1:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
301 self.transcode(str(string))
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
302
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
303 if string.find("quit") != -1:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
304 self.quit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
305
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
306 def transcode(self, line):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
307 """ Called when requested to transcode a track """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
308 args = string.split(line)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
309 if (len(args) != 3):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
310 log.debug("Invalid transcode command")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
311 return
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
312
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
313 log.debug("transcoding " + args[1] + " to " + args[2])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
314 try:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
315 newjob = TranscodeJob(args[1], args[2])
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
316 except:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
317 log.debug("Can't create transcoding job")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
318 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
319
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
320 self.queue.add(newjob)
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 def notify(self, job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
323 """ Report to amarok that the job is done """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
324 if (job.errormsg == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
325 log.debug("Job " + job.inurl + " completed successfully")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
326 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
327 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
328 log.debug("Job " + job.inurl + " failed - " + job.errormsg)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
329 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
330
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
331 def quit(self):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
332 log.debug("quitting")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
333 sys.exit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
334
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
335 ############################################################################
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 def debug(message):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
338 """ Prints debug message to stdout """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
339 log.debug(message)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
340
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
341 def onStop(signum, stackframe):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
342 """ Called when script is stopped by user """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
343 log.debug("signalled exit")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
344 sys.exit()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
345
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
346 def initLog():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
347 # Init our logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
348 global log
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
349 log = logging.getLogger("amaKode")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
350 # Default to warts and all logging
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
351 log.setLevel(logging.DEBUG)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
352
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
353 # Log to this file
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
354 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/amakode.log",
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
355 maxBytes = 10000, backupCount = 3)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
356
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
357 # And stderr
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
358 logstderr = logging.StreamHandler()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
359
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
360 # Format it nicely
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
361 formatter = logging.Formatter("[%(name)s] %(message)s")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
362
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
363 # Glue it all together
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
364 logfile.setFormatter(formatter)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
365 logstderr.setFormatter(formatter)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
366 log.addHandler(logfile)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
367 log.addHandler(logstderr)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
368 return(log)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
369
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
370 def reportJob(job):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
371 """ Report to amarok that the job is done """
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
372 if (job.errormsg == None):
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
373 log.debug("Job " + job.inurl + " completed successfully")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
374 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
375 else:
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
376 log.debug("Job " + job.inurl + " failed - " + job.errormsg)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
377 log.debug("dcop amarok mediabrowser transcodingFinished " + job.inurl + "\"\"")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
378
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
379 if __name__ == "__main__":
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
380 initLog()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
381 signal.signal(signal.SIGINT, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
382 signal.signal(signal.SIGHUP, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
383 signal.signal(signal.SIGTERM, onStop)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
384 if 1:
4
65a9f99302cd Incorporate changes from Jens Zurheide <jens.zurheide@gmx.de> to read tags
darius@inchoate.localdomain
parents: 3
diff changeset
385 # Run normal application
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
386 app = amaKode(sys.argv)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
387 else:
3
de86a9e19151 Misc bug fixes, whitespace changes.
darius@inchoate.localdomain
parents: 1
diff changeset
388 # Quick test case
1
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
389 q = QueueMgr(reportJob)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
390 j = TranscodeJob("file:///tmp/test.mp3", "ogg")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
391 q.add(j)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
392 j2 = TranscodeJob("file:///tmp/test2.mp3", "m4a")
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
393 q.add(j2)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
394 while not q.isidle():
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
395 q.poll()
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
396 res = select.select([], [], [], 1)
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
397
07e3d8655a29 Initial import of Amakode a transcoding script for Amarok written entirely
darius
parents:
diff changeset
398 log.debug("jobs all done")