Merge pull request #59 from bui/make-preview

Generate previews for songs
This commit is contained in:
Bui 2018-10-18 14:13:45 +01:00 committed by GitHub
commit 1a2753c411
2 changed files with 74 additions and 21 deletions

69
app.py
View File

@ -1,10 +1,13 @@
#!/usr/bin/env python2
from __future__ import division
import json
import sqlite3
import re
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__)
DATABASE = 'taiko.db'
@ -60,6 +63,21 @@ def get_osu_key(osu, section, key, default=None):
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):
tja_lines = open(tja, 'r').read().replace('\x00', '').split('\n')
@ -95,6 +113,24 @@ def route_index():
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')
def route_api_songs():
songs = query_db('select * from songs where enabled = 1')
@ -107,18 +143,7 @@ def route_api_songs():
for song in songs:
song_id = song[0]
song_type = song[10]
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:
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
preview = get_preview(song_id, song_type)
category_out = categories[song[9]] if song[9] in categories else def_category
songs_out.append({
@ -138,5 +163,23 @@ def route_api_songs():
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 -ar 32000 -b:a 92k -y -loglevel panic'})
ff.run()
return prev_path
if __name__ == '__main__':
app.run(port=34801)

View File

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