Last active
May 6, 2024 10:29
-
-
Save mapi68/a202f46b108b922a8c3344f27ca3133a to your computer and use it in GitHub Desktop.
This Python script provides a simple command-line interface to manipulate metadata tags in FLAC audio files within a specified directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from mutagen import File | |
class AudioFile: | |
def __init__(self, file_path): | |
self.audio = File(file_path, easy=True) | |
def get_title(self): | |
return self.audio.get("title", [None])[0] | |
def set_title(self, new_title): | |
self.audio["title"] = new_title | |
self.audio.save() | |
def remove_text_from_title(self, text_to_remove): | |
current_title = self.get_title() | |
if current_title and text_to_remove in current_title: | |
new_title = current_title.replace(text_to_remove, "") | |
self.set_title(new_title) | |
return True | |
return False | |
def edit_text_in_title(self, text_to_edit, new_text): | |
current_title = self.get_title() | |
if current_title and text_to_edit in current_title: | |
new_title = current_title.replace(text_to_edit, new_text) | |
self.set_title(new_title) | |
return True | |
return False | |
def rename_flac_files(directory, text_to_edit=None, new_text=None): | |
choice = input("Do you want to remove or edit the text in the tags? (r/e): ") | |
if choice.lower() == "r": | |
text_to_remove = input("Enter the text to remove from the tags: ") | |
files_renamed = False | |
for root, _, files in os.walk(directory): | |
flac_files = [file for file in files if file.lower().endswith(".flac")] | |
for flac_file in flac_files: | |
audio_file = AudioFile(os.path.join(root, flac_file)) | |
if audio_file.remove_text_from_title(text_to_remove): | |
print(f"Tag renamed for the file: {flac_file}") | |
files_renamed = True | |
if not files_renamed: | |
print("No files renamed.") | |
elif choice.lower() == "e": | |
if text_to_edit is None: | |
text_to_edit = input("Enter the text to edit in the tags: ") | |
if new_text is None: | |
new_text = input("Enter the new text to replace in the tags: ") | |
files_renamed = False | |
for root, _, files in os.walk(directory): | |
flac_files = [file for file in files if file.lower().endswith(".flac")] | |
for flac_file in flac_files: | |
audio_file = AudioFile(os.path.join(root, flac_file)) | |
if audio_file.edit_text_in_title(text_to_edit, new_text): | |
print(f"Tag edited for the file: {flac_file}") | |
files_renamed = True | |
if not files_renamed: | |
print("No files edited.") | |
else: | |
print("Invalid choice. Please try again and enter 'r' or 'e.") | |
if __name__ == "__main__": | |
current_directory = os.getcwd() | |
rename_flac_files(current_directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment