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 random
import subprocess
import multiprocessing
from urllib import parse
from flask import Flask, Response, render_template, send_file, url_for
from flask_restful import reqparse, abort, Api, Resource
import mutagen
import mutagen.mp3
MUSIC_DIR = "/home/iou1name/music/Music"
MUSIC_EXT = ['flac', 'mp3', 'wav', 'm4a']
@ -25,7 +27,7 @@ FFMPEG_CMD = [
]
class Track:
def __init__(self, filepath=None, d=None, coverart=""):
def __init__(self, filepath=None, d=None):
if d:
for attr, value in d.items():
setattr(self, attr, value)
@ -37,30 +39,50 @@ class Track:
m = mutagen.File(filepath)
self.tracknumber = m.get('tracknumber', [''])[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.date = m.get('date', [''])[0]
self.length = str(int(m.info.length) // 60) + ":"
self.length += str(int(m.info.length) % 60)
self.filepath = filepath
self.coverart = coverart
self.coverart = os.path.join(
os.path.dirname(self.filepath), 'folder.jpg')
def build_library(root_dir):
"""Walks the music directory and builds a library of tracks."""
print("Building library")
tracks = []
filepaths = []
for dir_name, sub_dirs, files in os.walk(root_dir):
for file in files:
if not os.path.splitext(file)[1][1:] in MUSIC_EXT:
continue
filepath = os.path.join(root_dir, dir_name, file)
if "folder.jpg" in files:
coverart = os.path.join(root_dir, dir_name, "folder.jpg")
else:
coverart = ""
track = Track(filepath, coverart=coverart)
tracks.append(track)
filepaths.append(filepath)
global worker
def worker(filepath):
"""Worker for multi-processing tracks."""
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")
return tracks
@ -225,10 +247,10 @@ def coverart(artist, album, track):
else:
abort(404, message="Track does not exist.")
if t.coverart:
if os.path.isfile(t.coverart):
return send_file(t.coverart)
else:
return "False"
return "No cover art for this track found."
if __name__ == "__main__":