diff --git a/.gitignore b/.gitignore index 700f2a5..90edb69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +test/* venv/* *.mp3 *.flac diff --git a/cttf.py b/cttf.py new file mode 100755 index 0000000..399ca48 --- /dev/null +++ b/cttf.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +from hashlib import new +import ffmpeg, os +from mutagen.flac import FLAC + + +FROM_CODEC = "flac" +TO_CODEC = "mp3" +FORMAT_STRING = "{TITLE} - {ARTIST}.{TO_CODEC}" + +#Recursively add files matching FROM_CODEC to the list +original_files = [] +for root, dirs, files in os.walk(os.getcwd()): + for file in files: + if file.endswith(FROM_CODEC): + path = os.path.join(root, file) + original_files.append(path) + +print(f"Found {len(original_files)} files") + +#Process files in the list +for file in original_files: + audio = FLAC(file) + album_folder = os.path.dirname(file) + + #Create converted folder + if not os.path.exists(f"{album_folder}/processed"): + os.makedirs(f"{album_folder}/processed") + + #Extract cover image if it doesn't exist + if not os.path.exists(f"{album_folder}/cover.jpg") and audio.pictures[0]: + open(f"{os.path.dirname(file)}/cover.jpg", 'wb').write(audio.pictures[0].data) + if audio.tags is None: + print(f"[ERR] NO TAGS ON {file}") + + #Assemble new filename + new_name = f"{os.path.dirname(file)}/{FORMAT_STRING.format(TITLE=audio['title'][0], ARTIST=audio['albumartist'][0],TO_CODEC=TO_CODEC)}" + + #Transcode + stream = ffmpeg.input(file) + stream = ffmpeg.output(stream, new_name, audio_bitrate='320k') + ffmpeg.run(stream) + + #Move original file to processed + os.rename(file, f"{album_folder}/processed/{os.path.basename(file)}" ) + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..dad2a9c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +ffmpeg-python==0.2.0 +future==0.18.2 +mutagen==1.45.1 +pkg-resources==0.0.0