Napisałam dla własnych potrzeb taki program. Na razie może zmieniać tagi wszystkich plików MP3 w danym folderze (docelowo albumy, ale nie tylko), poza tym moze tez konwertowac z FLAC do MP3 i zapisywac z odpowiednimi tagami. Funkcjonalnosci dopisywane w razie potrzeb, choc moze macie jakies pomysly
from sys import argv
import json
import requests
import os
import io
import pathlib
import shutil
import taglib
import pydub
import pathlib
class MusicDirectory:
def __init__(self,filepath):
self.filepath=pathlib.Path(filepath)
self.modes={
'1':['new album name','ALBUM','change_tag'],
'2':['from flac to mp3',None,'to_flac']
}
self.tags={}
def take_input(self,mode,variable):
if variable==None:
while True:
print(mode," . Press Y to confirm.")
y=input()
if y=='Y' or y=='y' or y=='yes': break
if y=='n' or y=='N' or y=='no': exit(1)
else:
print("Provide {}".format(variable))
setattr(self, variable,input())
def change_tag(self,tag='ALBUM',filepath=None):
if filepath==None: filepath=self.filepath
for l in os.listdir(filepath):
print("Changing {} in {}".format(tag,l))
mp3=taglib.File(str(filepath / l))
mp3.tags[tag]=[getattr(self, tag)]
mp3.save()
print(mp3.tags)
def collect_existing_tags(self):
for l in os.listdir(self.filepath):
#mp3=taglib.File(io.FileIO(self.filepath / l,'rb'))
if not os.path.isfile(self.filepath/l): break
mp3=taglib.File(str(self.filepath/l))
key=l.replace(".flac","")
self.tags[key]=mp3.tags
def update_all_tags(self, new_dir):
self.collect_existing_tags()
for song in self.tags:
#mp3=taglib.File(str(new_dir / (song+".mp3")))
for tag in self.tags[song]:
self.change_tag(tag=tag,filepath=str(new_dir/(song+'.mp3')))
#mp3.tags[tag]=self.tags[song][tag]
#mp3.save()
print("Song: {} got updated tags: {}".format(song,mp3.tags))
def to_flac(self):
mp3dir=self.filepath / "MP3_Version"
if not os.path.isdir(mp3dir):
os.makedirs(mp3dir)
for l in os.listdir(self.filepath):
if l=="MP3_Version": break
print(self.filepath / l)
flac=pydub.AudioSegment.from_file(self.filepath / l)
mp3_filename=l.replace(".flac",".mp3")
flac.export(mp3dir / mp3_filename, bitrate='312k')#parameters=['-progress pipe:1'])
self.update_all_tags(mp3dir)
def coordinate_input(self,mode):
self.take_input(self.modes[mode][0],self.modes[mode][1])
getattr(self,self.modes[mode][2])()
if __name__ == "__main__":
if len(argv)<=1 or len(argv)>2:
print(len(argv))
print("USAGE ./script FILE_DIRECTORY")
exit()
url=argv[1]
#url=url.replace("\\","\\\\")
print(url)
md=MusicDirectory(url)
#d.collect_existing_tags()
print("Program mode:\n [1 Change album names]\n[2 Convert all files from FLAC to MP3]")
mode=input()
md.coordinate_input(mode)