From 29e5e218254098ad877b4782b4acbc224e6f5794 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 26 Apr 2025 14:46:01 +0300 Subject: [PATCH] Init: add encoding conversion script --- README.md | 3 +++ music-tools.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 README.md create mode 100755 music-tools.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..34bbb1f --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Music Tools + +A set of scripts to manage a music library in MP3 and FLAC formats. diff --git a/music-tools.py b/music-tools.py new file mode 100755 index 0000000..bbd25ec --- /dev/null +++ b/music-tools.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import os +import sys +import argparse + +def convert_encoding(filepath): + try: + with open(filepath, 'r', encoding='cp1251', errors='strict') as f: + content = f.read() + + with open(filepath, 'w', encoding='utf-8', errors='strict') as f: + f.write(content) + + print(f"Success: {filepath}") + + except UnicodeDecodeError: + print(f"Error: {filepath} - not cp1251 encoded") + except Exception as e: + print(f"Error: {filepath} - {str(e)}") + +def process_directory(root_dir): + for root, _, files in os.walk(root_dir): + for file in files: + if file.endswith(('.cue', '.log')): + file_path = os.path.join(root, file) + convert_encoding(file_path) + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description='Recursively convert .cue and .log files from cp1251 to utf-8', + epilog='Example: python convert_encoding.py /path/to/directory' + ) + parser.add_argument( + 'directory', + type=str, + help='Path to target directory for conversion' + ) + + args = parser.parse_args() + + if not os.path.isdir(args.directory): + print(f"Error: '{args.directory}' is not a valid directory") + sys.exit(1) + + process_directory(args.directory) \ No newline at end of file