Skip to content

Instantly share code, notes, and snippets.

@m5wdev
Last active October 4, 2018 10:54
Show Gist options
  • Save m5wdev/171b20ffdd6e903876d8bbfc8a9faa56 to your computer and use it in GitHub Desktop.
Save m5wdev/171b20ffdd6e903876d8bbfc8a9faa56 to your computer and use it in GitHub Desktop.
Find mp3s with broken bitrate inside folder
import os
import glob
# Mutagen docs https://mutagen.readthedocs.io/
from mutagen.mp3 import MP3
# Settings
PATH_TO_MP3_DIR = 'D:\\mp3\\'
RESULTS_FILENAME = 'mp3_with_errors.txt'
mp3s_with_errors = list()
i = j = 0
for filename in glob.iglob(PATH_TO_MP3_DIR + '**/*.mp3', recursive=True):
# Try to read file bitrate and determinate defective tracks
try:
bitrate = int(MP3(filename).info.bitrate / 1000)
except Exception as e:
bitrate = "ERROR: {}".format(e)
mp3s_with_errors.append("{0}\nERROR: {1}".format(str(filename), e))
# Try to rename file and sinitize names use .replace()
try:
# Put in .rename() whataever you wish to replace in file name in addition you can lowercase filenames with .lower()
print("{0}) {1} | Bitrate: {2}".format(i, filename, bitrate))
os.rename(filename, filename.replace(" ", ""))
print("Renamed to: {}\n".format(filename))
except Exception as e:
print("Something goes wrong: {}".format(e))
i += 1
print("\n#######################################\n\nTotal mp3's found: {0}\nmp3s with errors: {1}".format(i, len(mp3s_with_errors)))
# Try to clean file mp3_with_errors.txt content if file exist
try:
open(RESULTS_FILENAME, 'w').close()
except:
pass
# Create file with results
res_file = open(RESULTS_FILENAME, "+w", encoding="utf-8")
for error in mp3s_with_errors:
j += 1
res_file.write("#{0}\n{1}\r\n\n".format(j, error))
res_file.close()
print("\nFile \"{0}\" saved in {1}".format(RESULTS_FILENAME, os.getcwd()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment