Created
April 13, 2018 21:58
-
-
Save xtotdam/850b8515c49c7d2e529c3b7a9fcecfdc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import codecs | |
import datetime | |
import eyed3 | |
import json | |
from math import log | |
import os | |
import shutil | |
import sys | |
unit_list = zip(['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'], [0, 0, 1, 2, 2, 2]) | |
def sizeof_fmt(num): | |
""" | |
Human friendly file size | |
https://stackoverflow.com/a/10171475/4392466 | |
""" | |
if num > 1: | |
exponent = min(int(log(num, 1024)), len(unit_list) - 1) | |
quotient = float(num) / 1024**exponent | |
unit, num_decimals = unit_list[exponent] | |
format_string = '{:.%sf} {}' % (num_decimals) | |
return format_string.format(quotient, unit) | |
if num == 0: | |
return '0 bytes' | |
if num == 1: | |
return '1 byte' | |
mp3name = sys.argv[1] | |
downloadpath = os.path.dirname(mp3name) | |
os.chdir(downloadpath) | |
print 'Working dir is', downloadpath | |
mp3name = os.path.basename(mp3name) | |
descrname = mp3name[:-4] + '.description' | |
jsonname = mp3name[:-4] + '.info.json' | |
covername = mp3name[:-4] + '.jpg' | |
if not os.path.exists(covername): | |
covername = mp3name[:-4] + '.png' | |
for p in [mp3name, descrname, jsonname, covername]: | |
if not os.path.exists(p): | |
print 'Something went wrong: can\'t find %s\nabort...' % (p) | |
exit() | |
else: | |
print p | |
### TASKLIST | |
# prettify json + | |
# print info + | |
# insert cover # is done by youtube-dl | |
# insert description and chapters if present into text + | |
# move metadata into own folder + | |
# parse and prettify json | |
parsedjson = json.load(open(jsonname, 'r')) | |
json.dump(parsedjson, open(jsonname, 'w'), indent=4, sort_keys=4) | |
# print info | |
if isinstance(parsedjson['chapters'], list): | |
chap_num = len(parsedjson['chapters']) | |
else: | |
chap_num = None | |
print '''\ | |
id: {id} | |
duration: {0} | |
size: {1} | |
chapters present: {2}'''.format( | |
str(datetime.timedelta(seconds=int(parsedjson['duration']))), | |
sizeof_fmt(os.path.getsize(mp3name)), | |
chap_num, | |
**parsedjson | |
), | |
# insert description and chapters if present into text | |
audioFile = eyed3.load(mp3name) | |
tag = audioFile.tag | |
lyrics_text = codecs.open(descrname, encoding='utf-8').read() | |
if isinstance(parsedjson['chapters'], list): | |
lyrics_text += '\n\n\nCHAPTERS\n\n' | |
for chap in parsedjson['chapters']: | |
start_time = unicode(datetime.timedelta(seconds=float(chap['start_time']))) | |
end_time = unicode(datetime.timedelta(seconds=float(chap['end_time']))) | |
title = unicode(chap['title']) | |
lyrics_text += u'{} -- {} : {}\n'.format(start_time, end_time, title) | |
tag.lyrics.set(lyrics_text) | |
tag.save() | |
# move metadata into own folder | |
metadata_dir = 'metadata' | |
try: | |
os.mkdir(metadata_dir) | |
except: | |
pass | |
for f in [descrname, jsonname, covername]: | |
try: | |
shutil.move(f, metadata_dir + os.sep + f) | |
except: | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to be used together with https://gist.github.com/xtotdam/f1842c68f90776824d85ea129b792c45