139 lines
3.3 KiB
Python
Executable File
139 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Music streaming.
|
|
"""
|
|
import os
|
|
import re
|
|
import json
|
|
import random
|
|
import subprocess
|
|
from urllib import parse
|
|
from collections import defaultdict
|
|
|
|
from flask import Flask, Response, render_template, send_file
|
|
from werkzeug.utils import secure_filename
|
|
|
|
MUSIC_DIR = "/mnt/music/Music"
|
|
MUSIC_EXT = ['flac', 'mp3', 'wav', 'm4a']
|
|
FFMPEG_CMD = [
|
|
'ffmpeg', '-y',
|
|
'-loglevel', 'panic',
|
|
'-i', '',
|
|
'-codec:a', 'libopus',
|
|
'-b:a', '64k',
|
|
'-f', 'opus',
|
|
'-'
|
|
]
|
|
|
|
def build_tree(root_dir):
|
|
"""Walks the music directory and builds a tree."""
|
|
print("Building tree.")
|
|
tree = defaultdict(dict)
|
|
for dirName, subDirs, files in os.walk(root_dir):
|
|
if dirName == root_dir:
|
|
continue
|
|
reg = re.search(".*/(.+?) - (.+) \(\d{4}\)", dirName)
|
|
if not reg:
|
|
print(dirName)
|
|
continue
|
|
artist, album = reg.groups()
|
|
tracks = [f for f in files if f.rpartition('.')[2] in MUSIC_EXT]
|
|
tree[artist][album] = tracks
|
|
return tree
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
"""Main index page."""
|
|
nav_items = os.listdir(MUSIC_DIR)
|
|
nav_items.sort()
|
|
nav_items = [item + '/' for item in nav_items]
|
|
cd = "/"
|
|
return render_template('index.html', **locals())
|
|
|
|
|
|
@app.route('/stream/<path:track>')
|
|
def stream(track):
|
|
"""View for the raw audio file."""
|
|
track = parse.unquote(track)
|
|
path = os.path.join(MUSIC_DIR, track)
|
|
path = os.path.abspath(path)
|
|
if not path.startswith(MUSIC_DIR):
|
|
return "False"
|
|
if not os.path.isfile(path):
|
|
return "False"
|
|
FFMPEG_CMD[5] = path
|
|
|
|
def generate():
|
|
with subprocess.Popen(FFMPEG_CMD, stdout=subprocess.PIPE) as proc:
|
|
data = proc.stdout.read(1024)
|
|
while data:
|
|
yield data
|
|
data = proc.stdout.read(1024)
|
|
return Response(generate(), mimetype="audio/ogg")
|
|
|
|
|
|
@app.route('/get_dir/<path:directory>/')
|
|
@app.route('/get_dir/')
|
|
def get_dir(directory=""):
|
|
"""Returns the contents of the requested directory."""
|
|
directory = directory.replace("DOTDOT", "..")
|
|
directory = os.path.join(MUSIC_DIR, directory)
|
|
directory = os.path.abspath(directory)
|
|
if not directory.startswith(MUSIC_DIR):
|
|
return "False"
|
|
if not os.path.isdir(directory):
|
|
return "False"
|
|
|
|
nav_items = os.listdir(directory)
|
|
nav_items.sort()
|
|
if directory != MUSIC_DIR:
|
|
nav_items = [".."] + nav_items
|
|
|
|
nav_items_new = []
|
|
for item in nav_items:
|
|
if os.path.isdir(os.path.join(directory, item)):
|
|
item += '/'
|
|
nav_items_new.append(item)
|
|
nav_items_new = [directory.replace(MUSIC_DIR, '') + '/'] + nav_items_new
|
|
|
|
return json.dumps(nav_items_new)
|
|
|
|
|
|
@app.route('/get_shuffle')
|
|
def shuffle():
|
|
"""Returns a randomly selected track from the library."""
|
|
item = random.choice(os.listdir(MUSIC_DIR))
|
|
path = os.path.join(MUSIC_DIR, item)
|
|
n = 0
|
|
while not item.rpartition('.')[2] in MUSIC_EXT:
|
|
n += 1
|
|
item = random.choice(os.listdir(path))
|
|
if os.path.isdir(os.path.join(path, item)):
|
|
path = os.path.join(path, item)
|
|
if n == 5:
|
|
item = random.choice(os.listdir(MUSIC_DIR))
|
|
path = os.path.join(MUSIC_DIR, item)
|
|
n = 0
|
|
path = os.path.join(path, item)
|
|
return path.replace(MUSIC_DIR, '')
|
|
|
|
|
|
@app.route('/album_cover/<path:cover>')
|
|
def album_cover(cover):
|
|
"""View for the raw audio file."""
|
|
path = os.path.join(MUSIC_DIR, cover)
|
|
path = os.path.abspath(path)
|
|
if not path.startswith(MUSIC_DIR):
|
|
return "False"
|
|
if not os.path.isfile(path):
|
|
return "False"
|
|
|
|
return send_file(path)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host='0.0.0.0', port=5150)
|