annotate amakode.py @ 4:65a9f99302cd

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