Created
December 28, 2024 05:42
-
-
Save informationsea/ef29c186879b5e8a6d8ae552dc874076 to your computer and use it in GitHub Desktop.
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
from mutagen.mp4 import MP4 | |
import argparse | |
import os | |
import os.path | |
import pprint | |
import re | |
def _main(): | |
parser = argparse.ArgumentParser( | |
description='Rename M4A files based on their title metadata') | |
parser.add_argument('directory', help='M4A scan directory') | |
parser.add_argument('--start', type=int, default=1, | |
help='Start number for renaming (default: 1)') | |
args = parser.parse_args() | |
files = [os.path.join(args.directory, x) | |
for x in os.listdir(args.directory) if x.endswith(".m4a")] | |
files.sort() | |
pprint.pprint(files) | |
for i, one_file in enumerate(files): | |
audio = MP4(one_file) | |
if '\xa9nam' in audio: | |
title = audio['\xa9nam'][0] | |
title = re.sub(r'^\d+ ', '', title) | |
new_title = f"{(i+args.start):03d} {title}" | |
print(f"\"{title}\" -> \"{new_title}\"") | |
audio['\xa9nam'] = new_title | |
audio.save() | |
os.rename(one_file, os.path.join( | |
args.directory, f"{new_title}.m4a")) | |
else: | |
print(f"No title metadata found in {one_file}") | |
if __name__ == '__main__': | |
_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment