1 # helpers.py: functions to interpret coder program output.
2 # mp3togo Portable Music Manager
4 # Copyright 2005: Simeon Veldstra <reallifesim@gmail.com>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 # ***********************************************************
22 # The code to parse output from external programs is based on
23 # the jack CD ripping program by Arne Zellentin.
25 # 'jack' is responsible for my full hard disks
26 # and has inspired me to produce this program.
27 # Thanks for everything Arne.
33 #import mp3togo.conf as conf
41 def parse_ogg123(buf):
44 sum = float(t.split(':')[0]) * 60
45 sum += float(t.split(':')[1])
48 if l.startswith('Time') and l.endswith('% '):
50 return sumtime(line[1])/sumtime(line[4]) * 100.0
53 def parse_mpg321(buf, f=frames()):
62 if y0 != -1 and y1 != -1:
64 f.tframes = float(s[y0+1:y1])
69 r = float(s.split()[1])
73 f.tframes = f.frames = 0
76 return f.frames/f.tframes * 100.0
79 def parse_oggdec(buf):
81 if len(s) == 2 and len(s[1]) == 9:
82 return float(s[1][2:7])
86 def parse_oggenc(buf):
95 if y0 != -1 and y1 != -1:
96 percent = float(s[y0 + 1:y1])
102 # originaly from "jack"
106 if len(s) == 1: s=s[0]
107 if s.find("%") >= 0: # status reporting starts here
109 y1 = y[1].split("(")[0]
110 percent = float(y[0]) / float(y1) * 100.0
111 elif s.find("Frame:") >= 0: # older versions, like 3.13
113 y0 = y[0].split("[")[-1]
114 y1 = y[1].split("]")[0]
115 percent = float(y0) / float(y1) * 100.0
120 def parse_flac_enc(buf):
123 if len (s) >= 2: s = s[-2]
124 if len (s) == 1: s = s[0]
127 if y0 != -1 and y1 != -1:
128 return float(s[y0 + 1:y1])
132 def parse_flac_dec(buf):
133 # I wrote this one myself
134 buf = buf.split('\r')
136 buf = buf.replace('%', '')
138 return int(buf.split()[1])
142 #### wav file "encoder/decoder"
143 #def recode_wav(input, output, args=None):
144 # """Factory returns function to copy wav file
146 # Returned function takes no args and copies
147 # input to output. Throws OSError on error."""
148 # return lambda: shutil.copyfile(input, output)
150 def parse_m4a_dec(buf):
151 n = buf.find('% decoding')
152 # if n==1 then we are at single digit progress (i.e. 5%) and
153 # at the beginning of the buffer
156 # else we are somewhere deeper in the buffer...check if we are at
157 # single or double digit progress by looking two characters back
158 elif buf[n-2] == '\n':
165 ## Command line escape sequences:
168 # ---------------------
175 'oggenc' : {'parser': parse_oggenc,
176 'cmd': 'oggenc %a -o %o %i',
180 'lame' : {'parser': parse_lame,
181 'cmd': 'lame --nohist %a %i %o',
185 # 'flac_enc':{'parser': parse_flac_enc,
186 # 'cmd': 'flac ???????',
188 # 'action': 'encode'},
189 'wav_enc': {'parser': None,
195 # 'ogg123' : {'parser': parse_ogg123,
196 # 'cmd': 'ogg123 -d wav -f %o %i',
199 # 'action': 'decode'},
200 'ogg123' : {'parser': parse_oggdec,
201 'cmd': 'oggdec -o %o %i',
205 'mpg321' : {'parser': parse_mpg321,
206 'cmd': 'mpg321 -v -w %o %i',
210 'flac_dec':{'parser': parse_flac_dec,
211 'cmd': 'flac --decode -F -o %o %i',
215 'm4a_dec': {'parser': parse_m4a_dec,
216 'cmd': 'faad -o %o %i',
220 'wav_dec': {'parser': None,