def modeswitch(self, mode, opts): if mode == "mp3": encoder = mp3(opts['lameopts']) elif mode == "ogg" or mode == "vorbis": encoder = vorbis(opts['oggencopts']) elif mode == "aacplus": encoder = aacplus(opts['aacplusopts']) elif mode == "opus": encoder = opus(opts['opusencopts']) elif mode == "flac": encoder = flac(opts['flacopts']) elif mode == "test": pass # 'test' is special as it isn't a converter, it is handled below elif mode == "_copy": encoder = filecopy(opts) elif mode[0:2] == "f:": encoder = ffmpeg(opts, mode[2:]) # Second argument is the codec else: return None if mode == "test": encoder = flac(opts['flacopts']) encf = encoder.flactest else: encf = encoder.convert return encf
def convert(self, infile, outfile): pipe = "/tmp/flac2all_%s-%s" % (uuid.uuid4(), uuid.uuid4()) os.mkfifo(pipe) startTime = time() inmetadata = flac().getflacmeta(infile) try: metastring = self.generate_lame_meta(inmetadata) except (UnboundLocalError): metastring = [] # If we do not get meta information. leave blank stderr = flacdecode(infile, pipe)() cmd = [ "%slame" % ipath.lamepath, "--silent", ] cmd.extend(metastring) cmd.extend(self.opts.split(' ')) cmd.extend([pipe, "%s.mp3" % outfile]) rc = sp.call(cmd) errline = stderr.read().decode('utf-8') if os.path.exists(pipe): os.unlink(pipe) errline = stderr.read() errline = errline.upper() if rc != 0: return [ infile, outfile, "mp3", "ERROR: %s" % errline, -1, time() - startTime ] return [infile, outfile, "mp3", "SUCCESS", 0, time() - startTime]
def mp3convert(self,infile,outfile,logq): startTime = time() inmetadata = flac().getflacmeta(infile) try: metastring = self.generateLameMeta(inmetadata) except(UnboundLocalError): metastring = "" #If we do not get meta information. leave blank #rb stands for read-binary, which is what we are doing, with a 1024 byte buffer decoder = flacdecode(infile)() if decoder == None: logq.put([infile,outfile,"mp3","ERROR: Could not open flac file for decoding.",-1, time() - startTime]) return None #wb stands for write-binary encoder = os.popen("%slame --silent %s - -o %s.mp3 %s" % ( lamepath, self.opts, shell().parseEscapechars(outfile), metastring ) ,'wb',8192) for line in decoder.readlines(): #while data exists in the decoders buffer encoder.write(line) #write it to the encoders buffer decoder.flush() #if there is any data left in the buffer, clear it decoder.close() #somewhat self explanetory encoder.flush() #as above encoder.close() logq.put([infile,outfile,"mp3","SUCCESS",0, time() - startTime])
def AACPconvert(self,infile,outfile,logq): inmetadata = flac().getflacmeta("\"" + infile + "\"") tagcmd = "%sneroAacTag " % neroaacpath try: metastring = self.generateNeroTags(inmetadata) except(UnboundLocalError): metastring = "" decoder = flacdecode(infile)() #wb stands for write-binary encoder = os.popen("%sneroAacEnc %s -if - -of %s.mp4 > /tmp/aacplusLog" % ( neroaacpath, self.opts, shell().parseEscapechars(outfile), ) ,'wb',8192) for line in decoder.readlines(): #while data exists in the decoders buffer encoder.write(line) #write it to the encoders buffer decoder.flush() #if there is any data left in the buffer, clear it decoder.close() #somewhat self explanetory encoder.flush() #as above encoder.close() #Now as the final event, load up the tags rc = os.system("%s \"%s.mp4\" %s" % (tagcmd, outfile, metastring)) # print "%s %s.mp4 %s" % (tagcmd, outfile, metastring) if rc != 0: logq.put([infile,outfile,"aacNero","WARNING: Could not tag AAC file",rc, time() - startTime]) else: logq.put([infile,outfile,"aacNero","SUCCESS",0, time() - startTime])
def encode_thread(taskq, opts, logq): while taskq.empty() == False: try: task = taskq.get(timeout=60) #Get the task, with one minute timeout except Queue.Empty: #No more tasks after 60 seconds, we can quit sys.exit() mode = task[3].lower() infile = task[0] outfile = task[0].replace(task[1], os.path.join(task[2], task[3]) ) outpath = os.path.dirname(outfile) try: if not os.path.exists(outpath): os.makedirs(outpath) except OSError as e: #Error 17 means folder exists already. We can reach this despite the check above #due to a race condition when a bunch of processes spawned all try to mkdir #So if Error 17, continue, otherwise re-raise the exception if e.errno != 17: raise(e) if mode == "mp3": encoder = mp3(opts['lameopts']) encf = encoder.mp3convert elif mode == "ogg" or mode == "vorbis": encoder = vorbis(opts['oggencopts']) encf = encoder.oggconvert elif mode == "aacplus": encoder = aacplus(opts['aacplusopts']) encf = encoder.AACPconvert elif mode == "aacplusnero": encoder = aacplusNero(opts['neroaacplusopts']) encf = encoder.AACPconvert elif mode == "opus": encoder = opus(opts['opusencopts']) encf = encoder.opusConvert elif mode == "flac": encoder = flac(opts['flacopts']) encf = encoder.flacConvert elif mode == "test": logq.put([infile,outfile,mode,"ERROR: Flac testing not implemented yet", 1,0]) continue else: logq.put([infile,outfile,mode,"ERROR: Error understanding mode '%s' is mode valid?" % mode,1,0]) raise modeError outfile = outfile.rstrip('.flac') print "Converting: \t %-40s target: %8s " % (task[0].split('/')[-1],task[3]) encf(infile,outfile,logq) sys.exit()
def convert(self, infile, outfile, logq): print("WARNING: Nero AAC is deprecated and will be removed in a future release") startTime = time() inmetadata = flac().getflacmeta("\"" + infile + "\"") tagcmd = "%sneroAacTag " % ipath.neropath try: metastring = self.generateNeroTags(inmetadata) except(UnboundLocalError): metastring = "" decoder = flacdecode(infile)() encoder = os.popen("%sneroAacEnc %s -if - -of %s.mp4 >/tmp/neroLog" % ( ipath.neropath, self.opts, shell().parse_escape_chars(outfile), ), 'wb', 8192) # while data exists in the decoders buffer for line in decoder.readlines(): encoder.write(line) # write it to the encoders buffer decoder.flush() # if there is any data left in the buffer, clear it decoder.close() # somewhat self explanetory encoder.flush() encoder.close() # Now as the final event, load up the tags rc = os.system("%s \"%s.mp4\" %s" % (tagcmd, outfile, metastring)) if rc != 0: logq.put([ infile, outfile, "aacNero", "WARNING: Could not tag AAC file", rc, time() - startTime ]) else: logq.put([ infile, outfile, "aacNero", "SUCCESS", 0, time() - startTime ])
def AACPconvert(self,infile,outfile,logq): inmetadata = flac().getflacmeta("\"" + infile + "\"") decoder = flacdecode(infile)() encoder = os.popen("%saac-enc %s - \"%s.aac\" > /tmp/aacplusLog" % ( aacpath, self.opts, outfile, ) ,'wb',8192) for line in decoder.readlines(): #while data exists in the decoders buffer encoder.write(line) #write it to the encoders buffer decoder.flush() #if there is any data left in the buffer, clear it decoder.close() #somewhat self explanetory encoder.flush() #as above encoder.close()
def mp3convert(self, infile, outfile, logq): pipe = "/tmp/flac2all_%s" % str(uuid.uuid4()).strip() startTime = time() inmetadata = flac().getflacmeta(infile) os.mkfifo(pipe) try: metastring = self.generate_lame_meta(inmetadata) except(UnboundLocalError): metastring = [] # If we do not get meta information. leave blank (decoder, stderr) = flacdecode(infile, pipe)() cmd = [ "%slame" % ipath.lamepath, "--silent", ] cmd.extend(metastring) cmd.extend(self.opts.split(' ')) cmd.extend([pipe, "%s.mp3" % outfile]) rc = sp.check_call(cmd) os.unlink(pipe) errline = stderr.read() errline = errline.upper() if errline.strip() != '': print "ERRORLINE: %s" % errline if errline.find("ERROR") != -1 or rc != 0: logq.put([ infile, "mp3", "ERROR: decoder error: %s" % ( errline, -1, time() - startTime )], timeout=10) return False logq.put([ infile, outfile, "mp3", "SUCCESS", 0, time() - startTime ])
def run(self): taskq = self.taskq opts = self.opts logq = self.logq while not taskq.empty(): try: # Get the task, with one minute timeout task = taskq.get(timeout=60) except Queue.Empty: # No more tasks after 60 seconds, we can quit return True mode = task[3].lower() infile = task[0] outfile = task[0].replace(task[1], os.path.join(task[2], task[3])) outpath = os.path.dirname(outfile) try: if not os.path.exists(outpath): os.makedirs(outpath) except OSError as e: # Error 17 means folder exists already. We can reach this # despite the check above, due to a race condition when a # bunch of spawned processes all try to mkdir at once. # So if Error 17, continue, otherwise re-raise the exception if e.errno != 17: raise (e) if mode == "mp3": encoder = mp3(opts['lameopts']) encf = encoder.mp3convert elif mode == "ogg" or mode == "vorbis": encoder = vorbis(opts['oggencopts']) encf = encoder.oggconvert elif mode == "aacplus": encoder = aacplus(opts['aacplusopts']) encf = encoder.AACPconvert elif mode == "aacplusnero": encoder = aacplusNero(opts['neroaacplusopts']) encf = encoder.AACPconvert elif mode == "opus": encoder = opus(opts['opusencopts']) encf = encoder.opusConvert elif mode == "flac": encoder = flac(opts['flacopts']) encf = encoder.flacConvert elif mode == "test": logq.put([ infile, outfile, mode, "ERROR: Flac testing not implemented yet", 1, 0 ]) continue else: logq.put([ infile, outfile, mode, "ERROR: Not understanding mode '%s' is mode valid?" % mode, 1, 0 ]) raise modeError outfile = outfile.replace('.flac', '') if opts['overwrite'] is False: if os.path.exists(outfile + "." + mode): print "Output file already exists, skipping" continue print "Converting: \t %-40s target: %8s " % ( task[0].split('/')[-1], task[3]) encf(infile, outfile, logq)