Musik/musik.py

85 lines
2.0 KiB
Python
Raw Normal View History

2018-09-12 13:04:00 -04:00
#!/usr/bin/env python3
"""
Music streaming.
"""
2018-09-13 14:22:27 -04:00
import os
import json
2018-09-12 13:04:00 -04:00
import subprocess
2018-09-13 14:22:27 -04:00
from urllib import parse
2018-09-12 13:04:00 -04:00
from flask import Flask, Response, render_template
2018-09-13 14:22:27 -04:00
from werkzeug.utils import secure_filename
2018-09-12 13:04:00 -04:00
2018-09-13 14:22:27 -04:00
MUSIC_DIR = "/mnt/music/Music"
2018-09-12 13:04:00 -04:00
FFMPEG_CMD = [
'ffmpeg', '-y',
'-loglevel', 'panic',
'-i', '',
'-codec:a', 'libopus',
'-b:a', '64k',
'-f', 'opus',
'-'
]
app = Flask(__name__)
@app.route('/')
def index():
"""Main index page."""
2018-09-13 14:22:27 -04:00
nav_items = os.listdir(MUSIC_DIR)
nav_items.sort()
nav_items = [item + '/' for item in nav_items]
initial_track = "/Accept/Accept - Balls to the Wall (1983) [FLAC] {74321 93214 2}/01 - Balls to the Wall.flac"
cd = "/"
return render_template('index.html', **locals())
2018-09-12 13:04:00 -04:00
2018-09-13 14:22:27 -04:00
@app.route('/stream/<path:track>')
2018-09-12 13:04:00 -04:00
def stream(track):
"""View for the raw audio file."""
2018-09-13 14:22:27 -04:00
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
2018-09-12 13:04:00 -04:00
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")
2018-09-13 14:22:27 -04:00
@app.route('/get_dir/<path:directory>/')
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)
2018-09-12 13:04:00 -04:00
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5150)