def transcode (self, dest_format, destination_path=None): """Transcode the source file. Keyword arguments: dest_format -- destination format for the media destination_path -- destination path for the media (by default same location and filename as source) """ if not dest_format.lower() in SUPPORTED_TYPES.keys(): msg = "Destination media format '%s' not supported!" % dest_format raise AudioSlaveException(msg) destination_path = self._get_dest_path(dest_format, destination_path) # Check if we already have the transcoded file ready to go preexisting = self._get_preexisting(dest_format, destination_path) if preexisting: return preexisting if not self.wavefile: if dest_format == 'wav': #We have less work if we're going to WAV self._decode(destination_path) return destination_path # Make a temporary WAV file self._decode() self.wave_temp = True return self._encode(dest_format, destination_path)
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
def main(): """CLI Driver""" parser = argparse.ArgumentParser(description='Audio Transcoder', \ prog='pyaudioslave.py') subparsers = parser.add_subparsers() transcode_parser = subparsers.add_parser('transcode') transcode_parser.add_argument('transcodeSource', help='File to transcode') transcode_parser.add_argument('dest_format', help='Destination format', \ type=str, choices=SUPPORTED_TYPES.keys()) transcode_parser.add_argument('-d', '--dest', dest='destination_path', \ help='Path to destination', action='store') fingerprint_parser = subparsers.add_parser('fingerprint') fingerprint_parser.add_argument('fingerprintSource', \ help='File to fingerprint') fingerprint_parser.add_argument('-q', '--query', dest='query', \ help='Query Echonest', action='store_true') args = parser.parse_args() if hasattr(args, 'transcodeSource'): afile = AudioSlave(args.transcodeSource) print "Source file type: %s" % afile.get_type() print "Destination file type : %s" % args.dest_format if args.destination_path: transcoded_file = afile.transcode(str(args.dest_format), \ str(args.destination_path)) else: transcoded_file = afile.transcode(str(args.dest_format)) print "Transcoded to : %s" % transcoded_file sys.exit(0) elif hasattr(args, 'fingerprintSource'): afile = AudioSlave(args.fingerprintSource, echoprint=True) if args.query: result_tags = afile.get_tags(echoprint=True) if not result_tags: print "No matches found!" else: print "Title: " + result_tags[0].title print "Artist: " + result_tags[0].artist_name else: pprint(afile.get_echoprint_code()) else: print "ERROR: invalid mode. Use either 'transcode' or 'fingerprint'" sys.exit(1) raise Exception
def _get_type(self, file_path=None): """Returns the file_type of the classes source media file or the type of the provided file.""" if not file_path: file_path = self.sourcefile magic = Magic() file_type = magic.from_file(file_path) for media_type in SUPPORTED_TYPES.keys(): for file_string in SUPPORTED_TYPES[media_type]: if file_string in file_type: return media_type raise AudioSlaveException("File type '%s' not supported!" % file_type)
def get_transcodeable_types(): """Return supported destination file formats""" return SUPPORTED_TYPES.keys()