Commit 2b9def6b authored by Julian Rother's avatar Julian Rother
Browse files

Moved functions from jobtypes.py back to their origional context and fixed some style issues

parent 3214cd4b
import json
from server import *
from jobmanagement import job_handler
@job_handler('probe', 'probe-raw')
def import_xmp_chapters(jobid, jobtype, data, state, status):
......
from server import *
from sorter import insert_video
import os.path
from jobtypes import schedule_remux, schedule_transcode
import json
def set_metadata(dest, course, lecture):
chapters = query('SELECT text, time FROM chapters WHERE lecture_id = ? AND visible ORDER BY time', lecture['id'])
metadata = {'title': lecture['title'], 'album': course['title'],
'description': lecture['comment'],
'date': lecture['time'].strftime('%m/%d/%Y'),
'artist': lecture['speaker'] if lecture['speaker'] else course['organizer']}
dest['metadata'] = metadata
dest['chapters'] = chapters
def schedule_remux(lectureid, videoid=None):
lecture = query('SELECT * FROM lectures WHERE id = ?', lectureid)[0]
course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
videos = query('''SELECT videos.*, sources.path AS srcpath, sources.hash AS srchash, formats.options AS fmtopts
FROM videos
JOIN sources ON videos.source = sources.id
JOIN formats ON videos.video_format = formats.id
WHERE videos.lecture_id = ?''', lectureid)
for video in videos:
if not video['source']:
continue
if videoid and video['id'] != videoid:
continue
data = {'path': video['path'], 'srcpath': video['srcpath'],
'srchash': video['srchash'], 'video_id': video['id']}
fmt = json.loads(video['fmtopts'])
if 'format' in fmt:
data['format'] = fmt['format']
data['options'] = fmt.get('options', {})
set_metadata(data, course, lecture)
schedule_job('remux', data)
@app.route('/internal/jobs/add/remux', methods=['GET', 'POST'])
@mod_required
......@@ -13,6 +45,46 @@ def add_remux_job():
schedule_remux(lectureid, videoid)
return redirect(request.values.get('ref', url_for('jobs_overview')))
def schedule_transcode(source, fmt_id=None, video=None):
if video:
fmt_id = video['video_format']
assert(video['lecture_id'] == source['lecture_id'])
assert(fmt_id != None)
fmt = query('SELECT * FROM formats WHERE id = ?', fmt_id)[0]
lecture = query('SELECT * FROM lectures WHERE id = ?', source['lecture_id'])[0]
course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
data = {'input': {'path': source['path'], 'streams': []}, 'output': json.loads(fmt['options']), 'filters': []}
if source['type'] == 'plain':
stream = {'name': 'video', 'type': 'video'}
data['input']['streams'].append(stream)
stream = {'name': 'audio', 'type': 'audio'}
data['input']['streams'].append(stream)
else:
assert(False)
set_metadata(data['output'], course, lecture)
basename = os.path.basename(source['path']).rsplit('.', 1)[0]
data['output']['path'] = 'pub/'+course['handle']+'/'+basename+fmt['suffix']
if video:
old_source = query('SELECT * FROM sources WHERE id = ?', video['source'])[0]
data['output']['path'] = video['path']
data['video_id'] = video['id']
data['srcpath'] = old_source['path']
data['srchash'] = old_source['hash']
else:
data['lecture_id'] = lecture['id']
data['format_id'] = fmt['id']
data['source_id'] = source['id']
return schedule_job('transcode', data, queue="background")
@job_handler('transcode')
def insert_transcoded_video(jobid, jobtype, data, state, status):
if 'lecture_id' not in data or 'source_id' not in data or 'format_id' not in data:
return
if 'video_id' in data:
return
video_id = insert_video(data['lecture_id'], data['output']['path'], data['format_id'], status['hash'], status['filesize'], status['duration'], data['source_id'])
schedule_remux(data['lecture_id'], video_id)
@app.route('/internal/jobs/add/reencode', methods=['GET', 'POST'])
@mod_required
@csrf_protect
......
......@@ -50,6 +50,7 @@ def schedule_job(jobtype, data=None, priority=0, queue="default"):
data = {}
return modify('INSERT INTO jobs (type, priority, queue, data, time_created) VALUES (?, ?, ?, ?, ?)',
jobtype, priority, queue, json.dumps(data, default=date_json_handler), datetime.now())
def cancel_job(job_id):
query('UPDATE jobs SET state = "deleted" WHERE id = ? AND state = "ready"', job_id)
query('UPDATE jobs SET canceled = 1 WHERE id = ?', job_id)
......
......@@ -2,7 +2,6 @@ from server import *
import json
import random
from time import sleep
from jobmanagement import *
@app.route('/internal/jobs/overview')
@register_navbar('Jobs', iconlib='fa', icon='suitcase', group='weitere')
......@@ -114,11 +113,3 @@ def jobs_schedule(hostname):
return 'no jobs', 503
return Response(json.dumps(job, default=date_json_handler), mimetype='application/json')
@app.route('/internal/jobs/add/thumbnail', methods=['GET', 'POST'])
@mod_required
@csrf_protect
@handle_errors('jobs_overview', 'Zu dieser Veranstaltung existieren keine Videos!', 404, IndexError)
def add_thumbnail_job():
schedule_thumbnail(request.values['lectureid'])
return redirect(request.values.get('ref', url_for('jobs_overview')))
from jobmanagement import schedule_job
import json
from server import query
def set_metadata(dest, course, lecture):
chapters = query('SELECT text, time FROM chapters WHERE lecture_id = ? AND visible ORDER BY time', lecture['id'])
metadata = {'title': lecture['title'], 'album': course['title'],
'description': lecture['comment'],
'date': lecture['time'].strftime('%m/%d/%Y'),
'artist': lecture['speaker'] if lecture['speaker'] else course['organizer']}
dest['metadata'] = metadata
dest['chapters'] = chapters
def schedule_thumbnail(lectureid):
videos = query('''
SELECT videos.path
FROM videos
JOIN formats ON (videos.video_format = formats.id)
WHERE videos.lecture_id = ?
ORDER BY formats.prio DESC''', lectureid)
return schedule_job('thumbnail', {'lectureid': str(lectureid), 'path': videos[0]['path']})
def schedule_remux(lectureid, videoid=None):
lecture = query('SELECT * FROM lectures WHERE id = ?', lectureid)[0]
course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
videos = query('''SELECT videos.*, sources.path AS srcpath, sources.hash AS srchash, formats.options AS fmtopts
FROM videos
JOIN sources ON videos.source = sources.id
JOIN formats ON videos.video_format = formats.id
WHERE videos.lecture_id = ?''', lectureid)
for video in videos:
if not video['source']:
continue
if videoid and video['id'] != videoid:
continue
data = {'path': video['path'], 'srcpath': video['srcpath'],
'srchash': video['srchash'], 'video_id': video['id']}
fmt = json.loads(video['fmtopts'])
if 'format' in fmt:
data['format'] = fmt['format']
data['options'] = fmt.get('options', {})
set_metadata(data, course, lecture)
schedule_job('remux', data)
def schedule_transcode(source, fmt_id=None, video=None):
if video:
fmt_id = video['video_format']
assert(video['lecture_id'] == source['lecture_id'])
assert(fmt_id != None)
fmt = query('SELECT * FROM formats WHERE id = ?', fmt_id)[0]
lecture = query('SELECT * FROM lectures WHERE id = ?', source['lecture_id'])[0]
course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
data = {'input': {'path': source['path'], 'streams': []}, 'output': json.loads(fmt['options']), 'filters': []}
if source['type'] == 'plain':
stream = {'name': 'video', 'type': 'video'}
data['input']['streams'].append(stream)
stream = {'name': 'audio', 'type': 'audio'}
data['input']['streams'].append(stream)
else:
assert(False)
set_metadata(data['output'], course, lecture)
basename = os.path.basename(source['path']).rsplit('.', 1)[0]
data['output']['path'] = 'pub/'+course['handle']+'/'+basename+fmt['suffix']
if video:
old_source = query('SELECT * FROM sources WHERE id = ?', video['source'])[0]
data['output']['path'] = video['path']
data['video_id'] = video['id']
data['srcpath'] = old_source['path']
data['srchash'] = old_source['hash']
else:
data['lecture_id'] = lecture['id']
data['format_id'] = fmt['id']
data['source_id'] = source['id']
return schedule_job('transcode', data, queue="background")
from server import *
from jobmanagement import cancel_job, restart_job, schedule_job, job_handler
@app.route('/internal/streaming/legacy_auth', methods=['GET', 'POST'])
@app.route('/internal/streaming/legacy_auth/<server>', methods=['GET', 'POST'])
......
......@@ -476,8 +476,8 @@ def dbstatus():
def date_json_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
import jobs
from edit import edit_handler
from jobmanagement import job_handler, job_handler_handle, job_set_state, schedule_job, cancel_job, restart_job
import feeds
import importer
import stats
......@@ -489,4 +489,6 @@ import timetable
import chapters
import icalexport
import livestreams
import encoding
import cutprogress
import jobs
from server import *
from jobmanagement import schedule_job, job_handler
from jobtypes import schedule_thumbnail
import traceback
import os.path
......@@ -39,6 +37,23 @@ def update_video_metadata(jobid, jobtype, data, state, status):
modify('UPDATE videos_data SET hash = ?, file_size = ?, duration = ? WHERE id = ?',
status['hash'], status['filesize'], status['duration'], data['video_id'])
def schedule_thumbnail(lectureid):
videos = query('''
SELECT videos.path
FROM videos
JOIN formats ON (videos.video_format = formats.id)
WHERE videos.lecture_id = ?
ORDER BY formats.prio DESC''', lectureid)
return schedule_job('thumbnail', {'lectureid': str(lectureid), 'path': videos[0]['path']})
@app.route('/internal/jobs/add/thumbnail', methods=['GET', 'POST'])
@mod_required
@csrf_protect
@handle_errors('jobs_overview', 'Zu dieser Veranstaltung existieren keine Videos!', 404, IndexError)
def add_thumbnail_job():
schedule_thumbnail(request.values['lectureid'])
return redirect(request.values.get('ref', url_for('jobs_overview')))
def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1, duration=-1, sourceid=None):
visible = query('SELECT courses.autovisible FROM courses JOIN lectures ON lectures.course_id = courses.id WHERE lectures.id = ?', lectureid)[0]['autovisible']
video_id = modify('''INSERT INTO videos_data
......@@ -46,9 +61,7 @@ def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1, dura
VALUES
(?, ?, ?, ?, "", "", "", ?, ?, ?, ?, ?, ?, ?)''',
lectureid, visible, dbfilepath, fileformatid, datetime.now(), datetime.now(), datetime.now(), -1, hash, filesize, duration, source)
if sourceid:
schedule_remux(lectureid, video_id)
else:
if not sourceid:
query('INSERT INTO sortlog (lecture_id,video_id,path,`when`) VALUES (?,?,?,?)', lectureid, video_id, dbfilepath, datetime.now())
schedule_job('probe', {'path': dbfilepath, 'lecture_id': lectureid, 'video_id': video_id, 'import-chapters': True})
schedule_thumbnail(lectureid)
......@@ -56,14 +69,7 @@ def insert_video(lectureid, dbfilepath, fileformatid, hash="", filesize=-1, dura
lecture = query('SELECT * FROM lectures WHERE id = ?', lectureid)[0]
course = query('SELECT * FROM courses WHERE id = ?', lecture['course_id'])[0]
notify_mods('new_video', course['id'], course=course, lecture=lecture, video=video)
@job_handler('transcode')
def insert_transcoded_video(jobid, jobtype, data, state, status):
if 'lecture_id' not in data or 'source_id' not in data or 'format_id' not in data:
return
if 'video_id' in data:
return
insert_video(data['lecture_id'], data['output']['path'], data['format_id'], status['hash'], status['filesize'], status['duration'], data['source_id'] )
return video_id
def split_filename(filename):
# '_' and ' ' are handled like '-'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment