generate previews for songs

This commit is contained in:
Bui 2018-10-17 22:16:53 +01:00
parent d6a42c0f38
commit cb8b8b4a61
2 changed files with 73 additions and 18 deletions

80
app.py
View File

@ -1,10 +1,13 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
from __future__ import division
import json import json
import sqlite3 import sqlite3
import re import re
import os import os
from flask import Flask, g, jsonify, render_template from flask import Flask, g, jsonify, render_template, request, abort, redirect
from ffmpy import FFmpeg
app = Flask(__name__) app = Flask(__name__)
DATABASE = 'taiko.db' DATABASE = 'taiko.db'
@ -60,6 +63,21 @@ def get_osu_key(osu, section, key, default=None):
return default return default
def get_preview(song_id, song_type):
preview = 0
if song_type == "tja":
if os.path.isfile('public/songs/%s/main.tja' % song_id):
preview = get_tja_preview('public/songs/%s/main.tja' % song_id)
else:
osus = [osu for osu in os.listdir('public/songs/%s' % song_id) if osu in ['easy.osu', 'normal.osu', 'hard.osu', 'oni.osu']]
if osus:
osud = parse_osu('public/songs/%s/%s' % (song_id, osus[0]))
preview = int(get_osu_key(osud, 'General', 'PreviewTime', 0))
return preview
def get_tja_preview(tja): def get_tja_preview(tja):
tja_lines = open(tja, 'r').read().replace('\x00', '').split('\n') tja_lines = open(tja, 'r').read().replace('\x00', '').split('\n')
@ -95,6 +113,24 @@ def route_index():
return render_template('index.html', version=version) return render_template('index.html', version=version)
@app.route('/api/preview')
def route_api_preview():
song_id = request.args.get('id', None)
if not song_id or not re.match('^[0-9]+$', song_id):
abort(400)
song_row = query_db('select * from songs where id = ? and enabled = 1', (song_id,))
if not song_row:
abort(400)
song_type = song_row[0][10]
prev_path = make_preview(song_id, song_type)
if not prev_path:
return redirect(''.join([request.host_url, '/songs/%s/main.mp3' % song_id]))
return redirect(''.join([request.host_url, '/songs/%s/preview.mp3' % song_id]))
@app.route('/api/songs') @app.route('/api/songs')
def route_api_songs(): def route_api_songs():
songs = query_db('select * from songs where enabled = 1') songs = query_db('select * from songs where enabled = 1')
@ -107,18 +143,7 @@ def route_api_songs():
for song in songs: for song in songs:
song_id = song[0] song_id = song[0]
song_type = song[10] song_type = song[10]
if song_type == "tja": preview = get_preview(song_id, song_type)
if os.path.isfile('public/songs/%s/main.tja' % song_id):
preview = get_tja_preview('public/songs/%s/main.tja' % song_id)
else:
preview = 0
else:
osus = [osu for osu in os.listdir('public/songs/%s' % song_id) if osu in ['easy.osu', 'normal.osu', 'hard.osu', 'oni.osu']]
if osus:
osud = parse_osu('public/songs/%s/%s' % (song_id, osus[0]))
preview = int(get_osu_key(osud, 'General', 'PreviewTime', 0))
else:
preview = 0
category_out = categories[song[9]] if song[9] in categories else def_category category_out = categories[song[9]] if song[9] in categories else def_category
songs_out.append({ songs_out.append({
@ -138,5 +163,34 @@ def route_api_songs():
return jsonify(songs_out) return jsonify(songs_out)
def make_preview(song_id, song_type):
song_path = 'public/songs/%s/main.mp3' % song_id
prev_path = 'public/songs/%s/preview.mp3' % song_id
if os.path.isfile(song_path) and not os.path.isfile(prev_path):
preview = get_preview(song_id, song_type) / 1000
if not preview or preview <= 0.1:
print 'Skipping #%s due to no preview' % song_id
return False
print 'Making preview.mp3 for song #%s' % song_id
ff = FFmpeg(inputs={song_path: '-ss %s' % preview},
outputs={prev_path: '-codec:a libmp3lame -b:a 128k -y -loglevel panic'})
ff.run()
return prev_path
def check_song_previews():
with app.app_context():
songs = query_db('select * from songs where enabled = 1')
for song in songs:
make_preview(song[0], song[10])
check_song_previews()
if __name__ == '__main__': if __name__ == '__main__':
app.run(port=34801) app.run(port=34801)

View File

@ -1263,16 +1263,17 @@ class SongSelect{
} }
var songObj = assets.songs.find(song => song.id == id) var songObj = assets.songs.find(song => song.id == id)
if(songObj.sound){ if(songObj.preview_sound){
if(!loadOnly){ if(!loadOnly){
this.preview = songObj.sound this.preview = songObj.preview_sound
this.preview.gain = snd.previewGain this.preview.gain = snd.previewGain
this.previewLoaded(startLoad, prvTime) this.previewLoaded(startLoad, prvTime)
} }
}else{ }else{
snd.previewGain.load("/songs/" + id + "/main.mp3").then(sound => { var previewFilename = prvTime > 0.1 ? "/preview.mp3" : "/main.mp3"
snd.previewGain.load("/songs/" + id + previewFilename).then(sound => {
if(currentId === this.previewId){ if(currentId === this.previewId){
songObj.sound = sound songObj.preview_sound = sound
this.preview = sound this.preview = sound
this.previewLoaded(startLoad, prvTime) this.previewLoaded(startLoad, prvTime)
} }
@ -1285,7 +1286,7 @@ class SongSelect{
var difference = endLoad - startLoad var difference = endLoad - startLoad
var minDelay = 300 var minDelay = 300
var delay = minDelay - Math.min(minDelay, difference) var delay = minDelay - Math.min(minDelay, difference)
this.preview.playLoop(delay / 1000, false, prvtime / 1000) this.preview.playLoop(delay / 1000, false, 0)
} }
endPreview(){ endPreview(){
this.previewId++ this.previewId++