made initial library processing multithreaded and use albumartist instead of artist

This commit is contained in:
iou1name 2019-03-13 12:49:38 -04:00
parent 0233cfc09e
commit 9152ed1c83

View File

@ -6,11 +6,13 @@ import os
import json import json
import random import random
import subprocess import subprocess
import multiprocessing
from urllib import parse from urllib import parse
from flask import Flask, Response, render_template, send_file, url_for from flask import Flask, Response, render_template, send_file, url_for
from flask_restful import reqparse, abort, Api, Resource from flask_restful import reqparse, abort, Api, Resource
import mutagen import mutagen
import mutagen.mp3
MUSIC_DIR = "/home/iou1name/music/Music" MUSIC_DIR = "/home/iou1name/music/Music"
MUSIC_EXT = ['flac', 'mp3', 'wav', 'm4a'] MUSIC_EXT = ['flac', 'mp3', 'wav', 'm4a']
@ -25,7 +27,7 @@ FFMPEG_CMD = [
] ]
class Track: class Track:
def __init__(self, filepath=None, d=None, coverart=""): def __init__(self, filepath=None, d=None):
if d: if d:
for attr, value in d.items(): for attr, value in d.items():
setattr(self, attr, value) setattr(self, attr, value)
@ -37,30 +39,50 @@ class Track:
m = mutagen.File(filepath) m = mutagen.File(filepath)
self.tracknumber = m.get('tracknumber', [''])[0] self.tracknumber = m.get('tracknumber', [''])[0]
self.title = m.get('title', [''])[0] self.title = m.get('title', [''])[0]
self.artist = m.get('artist', [''])[0] if m.get('albumartist'):
self.artist = m.get('albumartist', [''])[0]
else:
self.artist = m.get('artist', [''])[0]
self.album = m.get('album', [''])[0] self.album = m.get('album', [''])[0]
self.date = m.get('date', [''])[0] self.date = m.get('date', [''])[0]
self.length = str(int(m.info.length) // 60) + ":" self.length = str(int(m.info.length) // 60) + ":"
self.length += str(int(m.info.length) % 60) self.length += str(int(m.info.length) % 60)
self.filepath = filepath self.filepath = filepath
self.coverart = coverart self.coverart = os.path.join(
os.path.dirname(self.filepath), 'folder.jpg')
def build_library(root_dir): def build_library(root_dir):
"""Walks the music directory and builds a library of tracks.""" """Walks the music directory and builds a library of tracks."""
print("Building library") print("Building library")
tracks = [] filepaths = []
for dir_name, sub_dirs, files in os.walk(root_dir): for dir_name, sub_dirs, files in os.walk(root_dir):
for file in files: for file in files:
if not os.path.splitext(file)[1][1:] in MUSIC_EXT: if not os.path.splitext(file)[1][1:] in MUSIC_EXT:
continue continue
filepath = os.path.join(root_dir, dir_name, file) filepath = os.path.join(root_dir, dir_name, file)
if "folder.jpg" in files: filepaths.append(filepath)
coverart = os.path.join(root_dir, dir_name, "folder.jpg")
else: global worker
coverart = "" def worker(filepath):
track = Track(filepath, coverart=coverart) """Worker for multi-processing tracks."""
tracks.append(track) track = Track(filepath)
return track
with multiprocessing.Pool() as pool:
mapping = pool.imap(worker, filepaths)
tracks = []
prev_percent = 0
while True:
try:
tracks.append(mapping.next())
except StopIteration:
break
percent = round(len(tracks) / len(filepaths) * 100, 2)
if percent >= prev_percent + 2.5:
print(f"{percent}%")
prev_percent = percent
print("Done") print("Done")
return tracks return tracks
@ -225,10 +247,10 @@ def coverart(artist, album, track):
else: else:
abort(404, message="Track does not exist.") abort(404, message="Track does not exist.")
if t.coverart: if os.path.isfile(t.coverart):
return send_file(t.coverart) return send_file(t.coverart)
else: else:
return "False" return "No cover art for this track found."
if __name__ == "__main__": if __name__ == "__main__":