1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import os from mutagen.flac import FLAC from mutagen.id3 import ID3, TIT2, TPE1, TALB, TPE2, TCOM, TDRC, TCON, TRCK, TPOS, COMM, USLT, APIC
def copy_vorbis_to_id3(flac_path, mp3_path): flac_tags = FLAC(flac_path) try: audio = ID3(mp3_path) except: audio = ID3() audio.delete() tag_map = { 'title': TIT2, 'artist': TPE1, 'album': TALB, 'albumartist': TPE2, 'composer': TCOM, 'date': TDRC, 'genre': TCON, 'tracknumber': TRCK, 'discnumber': TPOS, 'comment': COMM, } for key, frame_class in tag_map.items(): if key in flac_tags: value = flac_tags[key][0] audio.add(frame_class(encoding=3, text=value)) if 'lyrics' in flac_tags: lyrics_text = flac_tags['lyrics'][0] audio.add(USLT(encoding=3, lang='eng', desc='Lyrics', text=lyrics_text)) if flac_tags.pictures: flac_cover = flac_tags.pictures[0] mime_type = flac_cover.mime image_data = flac_cover.data audio.add( APIC( encoding=3, mime=mime_type, type=3, desc='Cover', data=image_data ) ) audio.save(mp3_path, v2_version=3) print(f"Copied tags for {os.path.basename(flac_path)}")
def process_music_folders(flac_dir, mp3_dir): if not os.path.exists(mp3_dir): os.makedirs(mp3_dir) for root, dirs, files in os.walk(flac_dir): for flac_file in files: if flac_file.endswith('.flac'): flac_path = os.path.join(root, flac_file) relative_path = os.path.relpath(flac_path, flac_dir) mp3_file = os.path.splitext(os.path.basename(flac_file))[0] + '.mp3' mp3_path = os.path.join(mp3_dir, os.path.dirname(relative_path), mp3_file) mp3_sub_dir = os.path.dirname(mp3_path) if not os.path.exists(mp3_sub_dir): os.makedirs(mp3_sub_dir) if os.path.exists(mp3_path): print(f"Processing {flac_file}...") copy_vorbis_to_id3(flac_path, mp3_path)
if __name__ == '__main__': flac_folder = input("请输入 FLAC 文件夹路径: ").strip() mp3_folder = input("请输入 MP3 输出文件夹路径: ").strip() process_music_folders(flac_folder, mp3_folder)
|