def test_import(self): cases = [ (td.FLAC_PATH, td.TRACK_1_TAGS, td.MP3_PATH, "test_import_flac.mp3"), (td.M4A_PATH, td.TRACK_3_TAGS, td.MP3_PATH, "test_import_mp4.mp3"), (td.FLAC_PATH, td.TRACK_1_TAGS, td.M4A_PATH, "test_import_flac.m4a"), (td.MP3_PATH, td.TRACK_2_TAGS, td.M4A_PATH, "test_import_mp3.m4a"), (td.MP3_PATH, td.TRACK_2_TAGS, td.FLAC_PATH, "test_import_mp3.flac"), (td.M4A_PATH, td.TRACK_3_TAGS, td.FLAC_PATH, "test_import_mp4.flac"), ] for case in cases: print case source, tags, original, target = case _, ext = os.path.splitext(source) tagged_source = os.path.join(td.TEMP_DIR, "tagged" + ext) shutil.copy(source, tagged_source) audiofile.open(tagged_source).write_tags(tags) target = os.path.join(td.TEMP_DIR, target) shutil.copy(original, target) sourcetags = audiofile.open(tagged_source).read_tags() af = audiofile.open(target) af.clear_tags() af.write_tags(sourcetags) af.close() self.assertEquals(sourcetags, audiofile.open(target).read_tags())
def test_write__mp4(self): target = os.path.join(td.TEMP_DIR, "test_write.m4a") shutil.copy(td.M4A_PATH, target) af = audiofile.open(target) af.write_tags(td.TRACK_2_TAGS) del af self.assertEquals(td.TRACK_2_TAGS, audiofile.open(target).read_tags())
def main(): args = parse_args() source_file = audiofile.open(args.source_file) target_file = audiofile.open(args.target_file) target_file.write_tags(source_file.read_tags()) cover_art = source_file.extract_cover() or source_file.folder_cover() if cover_art: target_file.embed_cover(cover_art)
def test_write__mp3(self): target = os.path.join(td.TEMP_DIR, "test_write.mp3") shutil.copy(td.MP3_PATH, target) af = audiofile.open(target) af.write_tags(td.TRACK_1_TAGS) af.flush() print audiofile.open(target).read_tags() self.assertEquals(td.TRACK_1_TAGS, audiofile.open(target).read_tags())
def test_image_embed_and_extract__mp4(self): target = os.path.join(td.TEMP_DIR, "test_write_image.m4a") shutil.copy(td.M4A_PATH, target) img = audiofile.load_folder_art(td.FLAC_PATH, "cover.jpg") af = audiofile.open(target) af.embed_cover(img) af.flush() af = audiofile.open(target) self.assertTrue(af.has_embedded_cover) self.assertEquals(img, audiofile.open(target).extract_cover())
def test_join_multival_on_write(self): cases = [(td.MP3_PATH, "test_multival.mp3"), (td.M4A_PATH, "test_multival.mp4")] for case in cases: print case source, target = case target = os.path.join(td.TEMP_DIR, target) shutil.copy(source, target) af = audiofile.open(target) af.write_tags({"artist": ["a", "b"]}) af.flush() af = audiofile.open(target) self.assertEquals(["a/b"], af.read_tags()["artist"])
def test_only_write_one_track_number(self): cases = [(td.MP3_PATH, "test_parts.mp3"), (td.M4A_PATH, "test_parts.mp4")] for case in cases: print case source, target = case target = os.path.join(td.TEMP_DIR, target) shutil.copy(source, target) af = audiofile.open(target) af.write_tags({"track_number": [9, 10]}) af.flush() af = audiofile.open(target) self.assertEquals([9], af.read_tags()["track_number"])
def main(): echonest.config_service() args = parse_args() if args.command == "playlist.static": print "[" for file_path in args.files: try: af = audiofile.open(file_path) id = af.read_tags().get("echonest_id", None) songs_by_file = {} if id: songs = [] for song in echonest.playlist.static( song_id=id, results=20, seed_catalog="CAMQJMD133B59A5B1C", type="catalog" ): songs.append( { "id": song.id, "title": song.title, "artist_name": song.artist_name, "artist_id": song.artist_id, } ) print json.dumps({file_path: songs}, sort_keys=True) print "," except Exception, e: sys.stderr.write(str(e)) print "]"
def main(): args = parse_args() art = None if args.cover: if args.cover.startswith('http://') or args.cover.startswith('https://'): art = albumart.load_url(args.cover) else: art = albumart.load(args.cover) for filepath in args.files: target_file = audiofile.open(filepath) if art: target_file.embed_cover(art, format=args.cover_format)
def main(): args = parse_args() if args.print0: delim = '\x00' else: delim = '\n' for file_path in args.files: af = audiofile.open(file_path) if args.tags: tags = args.tags.split(",") results = {k: v for k, v in af.read_tags().items() if k in tags} if results: sys.stdout.write(file_path + delim)
def test_read_multiple_track_number(self): from mutagen import mp3, id3, mp4 def package_trck(trck_list): return id3.TRCK(0, ["%i/%i" % (t[0], t[1]) for t in trck_list]) cases = [ (td.MP3_PATH, "test_parts_read.mp3", mp3.MP3, "TRCK", package_trck), (td.M4A_PATH, "test_parts_read.mp4", mp4.MP4, "trkn", lambda x: x), ] for case in cases: print case source, target, mtg_class, mtg_key, mtg_packager = case target = os.path.join(td.TEMP_DIR, target) shutil.copy(source, target) mtg_file = mtg_class(target) mtg_file[mtg_key] = mtg_packager([[9, 11], [10, 11]]) mtg_file.save() af = audiofile.open(target) self.assertEquals([9, 10], af.read_tags()["track_number"])
def main(): args = parse_args() hashlinks = os.listdir(args.out_dir) for filename in args.files: try: md5val = audiofile.open(filename).audio_md5() _, ext = os.path.splitext(filename) matches = [os.path.join(args.out_dir, hl) for hl in hashlinks if fnmatch.fnmatch(hl, "%s*" % md5val)] if matches: already_linked = False for match in matches: inode = os.stat(os.readlink(match)).st_ino source_inode = os.stat(filename).st_ino if inode == source_inode: print "%s: File already linked: %s" % (filename, os.path.basename(match)) already_linked = True if already_linked: continue matches = sorted([os.path.splitext(m)[0] for m in matches]) last_dupe_match = re.match('.+dupe-(\d+)', matches[-1]) if last_dupe_match: dupe_num = int(last_dupe_match.group(1)) + 1 else: dupe_num = 1 dupe_part = "-dupe-%02i" % dupe_num else: dupe_part = "" fname = md5val + dupe_part + ext link_target = os.path.join(args.out_dir, fname) os.symlink(os.path.abspath(filename), link_target) hashlinks.append(fname) except Exception as e: print 'Trouble with file %s: %s' % (filename, e)
def test_read_folder_image_path(self): af = audiofile.open(td.FLAC_PATH) self.assertTrue(af.has_folder_cover()) self.assertEquals( td.COVER_PATH, af.folder_cover_path() )
} ) print json.dumps({file_path: songs}, sort_keys=True) print "," except Exception, e: sys.stderr.write(str(e)) print "]" elif args.command == "catalog.create": catalog = _get_or_create_catalog(args) print catalog.id elif args.command == "catalog.update": catalog = _get_or_create_catalog(args) items = [] for file_path in args.files: try: af = audiofile.open(file_path) tags = af.read_tags() # API requires you to provide an id unique within the catalog item_id = hashlib.md5(os.path.abspath(file_path)).hexdigest() item = { "song_id": tags["echonest_id"][0], "release": tags.get("album_title", [None])[0], "track_number": tags.get("track_number", [None])[0], "disc_number": tags.get("disc_number", [None])[0], "item_id": item_id, } try: int(item["track_number"]) except: item.pop("track_number")
def main(): args = parse_args() for filepath in args.files: # try: target_file = audiofile.open(filepath) tags = target_file.read_tags() if args.cover \ and (not args.if_needed or not target_file.has_embedded_cover()): lfm_service = lastfm.make_service() cover_url = lfm_service.get_cover_art_url(tags) if args.update: target_file.embed_cover( albumart.load_url(cover_url), format=args.cover_format ) print '%s: updated with cover %s' % (filepath, cover_url) else: print cover_url elif args.echonest_id \ and (not args.if_needed or 'echonest_id' not in target_file.read_tags()): echonest.config_service(codegen_start_=args.codegen_start, codegen_duration_=args.codegen_duration) echonest_id = echonest.song.identify(filepath, codegen_start = echonest.codegen_start, codegen_duration = echonest.codegen_duration)[0].id if args.update: target_file.write_tags({'echonest_id':echonest_id}) print '%s: updated with id %s' % (filepath, echonest_id) else: print echonest_id elif args.echonest_profile: echonest.config_service() buckets=["audio_summary", "artist_familiarity", "artist_hotttnesss", "artist_location", "song_hotttnesss", "tracks", "id:7digital-US"] song = echonest.song.profile( target_file.read_tags()['echonest_id'], buckets=buckets )[0] track = echonest.track.track_from_id(target_file.read_tags()['echonest_id']) if args.update: target_file.write_tags({'echonest_id':echonest_id}) print '%s: updated with id %s' % (filepath, echonest_id) else: print "Audio Summary" print song.audio_summary print print "Artist Familiarity" print song.artist_familiarity print print "Artist Hotttnesss" print song.artist_hotttnesss print print "Artist Location" print song.artist_location print print "Song Hotttnesss" print song.song_hotttnesss, print print "Tracks" song = echonest.song.Song(target_file.read_tags()['echonest_id']) print song.get_tracks('7digital')[0] print print "Track analysis" print track.__dict__ else: print '%s: nothing to do' % filepath # except Exception as e: # print 'Trouble with file %s: %s' % (filepath, e) sys.stdout.flush()