Skip to content

Instantly share code, notes, and snippets.

@dotcomboom
Created February 5, 2022 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotcomboom/35feab0fa395fc5ead7dcb7da5a8dea3 to your computer and use it in GitHub Desktop.
Save dotcomboom/35feab0fa395fc5ead7dcb7da5a8dea3 to your computer and use it in GitHub Desktop.
Batch mp3guessenc script, outputs a plot
# python script to check using mp3guessenc every mp3 file in a directory and guess the encoding
# it will get the output from mp3guessenc, and parse out the text after "Maybe this file is encoded by"
import os
import subprocess
import sys
import re
# make a fancy graph of the results
import matplotlib.pyplot as plt
import numpy as np
#folder = "D:\\"
folder = "C:\\Users\\Eric\\Sync\\Music\\Music\\"
mp3guesspath = "C:\\Users\\Eric\\XP\\Sync\\CMD\\mp3guessenc.exe"
results = {}
# python: return output of a shell command
# function to report progress as an ascii progress bar spanning 10 characters
def progress(count, total, status=''):
# calculate the percentage of the total
pct_complete = float(count) / total
# from the percentage, calculate a number between 0 and 10 that represents progress
prog_bar = int(pct_complete * 10)
p = '\r[{0}] {1}% {2} '.format('#' * prog_bar + '-' * (10 - prog_bar), round(pct_complete * 100, 1), status)
# print the progress bar
print(p, end='')
# get the width of the terminal and subtract the progress bar width from it
print("░" * (os.get_terminal_size().columns - len(p)), end='')
# flush the output buffer
sys.stdout.flush()
# get every mp3 file in the folder recursively
for root, dirs, files in os.walk(folder):
# sort the files by date
files.sort(key=lambda x: os.path.getmtime(os.path.join(root, x)))
c = 0
for file in files:
c+=1
if file.endswith(".mp3"):
# get the encoding of the file
temp = ''
try:
output_stream = os.popen("\"" + mp3guesspath + "\" \"%s\"" % os.path.join(root, file))
encoding = output_stream.read()
temp = encoding
# get the encoding from the output
encoding = re.search("(?<=Maybe this file is encoded by ).*", encoding)
encoding = encoding.group(0)
except subprocess.CalledProcessError:
encoding = "-- called process error --"
except AttributeError:
# check if 'MusicMatch' is in temp and if so set the encoding to 'MusicMatch (xing)'
if "MusicMatch" in temp:
encoding = "MusicMatch (xing)"
else:
encoding = "Unknown"
# add the file and encoding to the dictionary
results[os.path.join(root, file)] = encoding
# path: encoding
# update the progress bar
progress(c, len(files), file)
encoding_counts = {}
# count the number of times each encoding occurs
for path, encoding in results.items():
if encoding in encoding_counts:
encoding_counts[encoding] += 1
else:
encoding_counts[encoding] = 1
# sort the encodings by count
encoding_counts = sorted(encoding_counts.items(), key=lambda x: x[1], reverse=True)
# make a bar graph of the encodings
plt.bar(range(len(encoding_counts)), [x[1] for x in encoding_counts], align='center')
plt.xticks(range(len(encoding_counts)), [x[0] for x in encoding_counts])
# show the graph
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment