10 from subprocess import *
14 def __init__(self, config):
22 self.mencoder_pid = None
23 self.mplayer_pid = None
24 signal.signal(signal.SIGABRT, self.kill_handler)
26 def kill_handler(self, sig, frame):
28 os.kill(self.mplayer_pid.pid + 1, signal.SIGKILL)
31 lib.log("Problems closing child")
33 def set_args(self, options):
38 self.mplayer = os.popen("which mplayer").read().strip()
40 elif opt.find("language=") >= 0:
42 self.language = opt.split("=")[1]
44 lib.log("Bad language option")
46 elif opt.find("format=") >= 0:
48 self.mux += " -mpegopts format=%s" % opt.split("=")[1]
50 lib.log("Bad format option")
53 def run_mplayer(self):
54 msg = "%s 1>/dev/null 2>/dev/null" % self.filename
56 if self.kind == "dvd":
59 self.mplayer += " " + msg
60 self.mplayer_pid = Popen(self.mplayer, shell=True)
62 def setup_mencoder(self):
63 self.path = self.config.get("Mencoder", "path")
64 a, b = os.popen2(self.path)
65 version = b.read().split("MEncoder ")[1].split(" (C)")[0].split("-")[-1]
67 if version > "4.1.1": self.mencoder_old = False
68 else: self.mencoder_old = True
70 lib.log("Mencoder version: %s" % version)
76 self.fifo = self.config.get("Mencoder", "fifo_path")
80 def setup_filename(self, filename):
82 self.kind, self.filename = filename.split("://")
84 return (False, "Wrong filename protocol")
86 if self.acodec == "mp3lame":
87 audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
89 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\
90 self.acodec, self.abitrate)
92 if self.kind == "file":
93 if not os.path.exists(self.filename):
94 msg = "File requested does not exist. SETUP failed."
98 self.args += " %s -mf fps=%s -of %s %s"\
99 " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\
100 " -really-quiet -o %s 2>/dev/null" % (
101 self.filename, self.fps, self.mux, audio, self.vcodec,
102 self.vbitrate, self.width, self.height, self.fifo)
104 elif self.kind == "dvd":
105 self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\
106 " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s"\
107 " -ofps %s -really-quiet -o %s 2>/dev/null" % (
108 self.filename, self.language, self.width, self.height, audio,
109 self.mux, self.vcodec, self.vbitrate, self.fps, self.fifo)
114 MENCODER SETUP DESCRIPTION
115 ===========================
117 -> mux, vcodecs and acodecs
118 |-> mencoder (-of | -ovc | -oac) help
120 -> if used mpeg as mux:
121 |-> to setup format: format=%s as an option at the end
125 def setup(self, filename, mux, vcodec, vbitrate,\
126 fps, acodec, abitrate, width, height, port, options):
130 self.vbitrate = vbitrate
133 self.abitrate = abitrate
136 self.port = int(port)
138 self.setup_mencoder()
139 ret_val = self.setup_filename(filename)
144 self.set_args(options)
146 # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 file
147 # /tmp/dvb.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000 format=mpeg1 file
148 #4 mpeg mpeg1video 400 25 mp3lame 192 400 240 5000 language=en local dvd
149 if self.socket != None:
152 self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
153 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
154 self.socket.bind( ('', self.port) )
155 self.socket.listen(1)
162 if self.mencoder_old:
166 lib.log("Fifo already exists")
169 lib.log("Starting Mencoder: %s %s" % (self.path, self.args) )
171 if self.mencoder_old:
172 self.mencoder_pid = Popen(self.path + self.args, shell=True)
173 self.pout = open(self.fifo)
175 self.path += self.args
176 pin, self.pout = os.popen2(self.path)
178 self.child_pid = os.fork()
180 if self.child_pid == 0:
181 conn,addr= self.socket.accept()
182 lib.log("Sending Data to client: %s" % addr[0])
184 data = self.pout.read(1024)
189 while data != "" and retry < 5:
192 r, w, x = select([conn], [], [], 0)
194 back = conn.recv(1024)
195 if back == "OK" and self.mplayer and not self.mplayer_pid:
199 lib.log("Socket error (maybe timeout ?)")
202 data = self.pout.read(1024)
205 lib.log("Finished sending Data to client: %s" % addr[0])
207 lib.log("Client timed out")
215 os.kill(self.mencoder_pid.pid + 1, signal.SIGKILL)
220 lib.log("Trying to stop before playing...")
222 if self.socket != None:
223 lib.log("Closing socket")
226 lib.log("Trying to stop Mencoder process")
227 if self.child_pid != None:
228 os.kill(self.child_pid, signal.SIGABRT)