def _decode(self, destination_path=None): """ Transcode sourcefile to WAV file. By default use temporary WAV file path, optionally transcode to provided path. """ if self.wavefile and os.path.exists(self.wavefile): # WAV file already exists, no need to decode if self.wavefile == destination_path: return self.wavefile else: setup_dest_path(destination_path) shutil.copy(self.wavefile, destination_path) return destination_path if not destination_path: destination_path= tempfile.mkstemp(dir=TEMP_PREFIX)[1] self.wave_temp = True else: setup_dest_path(destination_path) self.wave_temp = False if self.file_type in DECODE_TO_WAV: command_line = DECODE_TO_WAV[self.file_type].substitute(sourcefile=self.sourcefile, \ wavefile=destination_path) command_list = shlex.split(command_line) with open('/dev/null', 'w') as DEVNULL: returncode = subprocess.call(command_list, stdout=DEVNULL, stderr=DEVNULL) if returncode == 0: # If Successful self.wavefile = os.path.abspath(destination_path) return os.path.abspath(destination_path) self.wave_temp = None raise AudioSlaveException("Problem decoding wave file.")
def _get_dest_path(self, dest_format, destination_path=None): """ Returns a destination path for the transcoded file. Ensures directories are created. """ if not destination_path: # Use sourcefile path destination_path = "%s.%s" % (os.path.splitext(self.sourcefile)[0], dest_format) else: if destination_path.endswith('/') or not destination_path.endswith(tuple(SUPPORTED_TYPES.keys())): destination_path = "%s.%s" % (os.path.join(destination_path, os.path.splitext(os.path.split(self.sourcefile)[1])[0]), dest_format) setup_dest_path(destination_path) elif '/' in destination_path and destination_path.endswith(tuple(SUPPORTED_TYPES.keys())): setup_dest_path(destination_path) return destination_path