mirror of
https://github.com/jiojciojsioe3/a3cjroijsiojiorj.git
synced 2024-12-23 09:46:15 +08:00
generate previews for songs
This commit is contained in:
parent
d6a42c0f38
commit
cb8b8b4a61
80
app.py
80
app.py
@ -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,34 @@ 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 -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__':
|
||||
app.run(port=34801)
|
||||
|
@ -1263,16 +1263,17 @@ 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)
|
||||
}
|
||||
}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){
|
||||
songObj.sound = sound
|
||||
songObj.preview_sound = sound
|
||||
this.preview = sound
|
||||
this.previewLoaded(startLoad, prvTime)
|
||||
}
|
||||
@ -1285,7 +1286,7 @@ class SongSelect{
|
||||
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, 0)
|
||||
}
|
||||
endPreview(){
|
||||
this.previewId++
|
||||
|
Loading…
Reference in New Issue
Block a user