Skip to content

Instantly share code, notes, and snippets.

@apuignav
Last active February 27, 2018 19:15
Show Gist options
  • Select an option

  • Save apuignav/30db0abd8a6d1a7d20d6b076e4f5cd96 to your computer and use it in GitHub Desktop.

Select an option

Save apuignav/30db0abd8a6d1a7d20d6b076e4f5cd96 to your computer and use it in GitHub Desktop.
MP3 renamer
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# @file rename-mp3.py
# @author Albert Puig (albert.puig@cern.ch)
# @date 27.02.2018
# =============================================================================
"""MP3 file renamer"""
import os
import shutil
import glob
import argparse
from mutagen.easyid3 import EasyID3
def get_file_name(file_name):
dir_name = os.path.dirname(file_name)
tags = EasyID3(file_name)
if 'disknumber' in tags:
file_name = '{:d}{:02d} - {}.mp3'.format(int(tags['disknumber'][0].split('/')[0]),
int(tags['tracknumber'][0].split('/')[0]),
tags['title'][0])
else:
file_name = '{:02d} - {}.mp3'.format(int(tags['tracknumber'][0].split('/')[0]),
tags['title'][0])
return os.path.join(dir_name, file_name)
def main():
# Command line options
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--dry-run", action='store_true')
parser.add_argument("files", nargs='+')
args = parser.parse_args()
# Rename
for file_ in args.files:
output_file_name = get_file_name(file_)
if file_ == output_file_name:
print("Not moving {}, file name is already correct".format(file_))
continue
if not args.dry_run:
shutil.move(file_, output_file_name)
else:
print("Move {} to {}".format(file_, output_file_name))
if __name__ == "__main__":
main()
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment