From 63ff543407f7b20cc5cbf292ed845a9d28102d38 Mon Sep 17 00:00:00 2001
From: Andreas <andreasv@fsmpi.rwth-aachen.de>
Date: Sun, 11 Sep 2016 23:55:28 +0200
Subject: [PATCH] added chapter display, not yet editable #22

---
 server.py              | 10 ++++++++++
 templates/chapters.srt | 11 +++++++++++
 templates/macros.html  |  1 +
 3 files changed, 22 insertions(+)
 create mode 100644 templates/chapters.srt

diff --git a/server.py b/server.py
index 9edddf0..718633b 100644
--- a/server.py
+++ b/server.py
@@ -396,6 +396,16 @@ def suggest_chapter(lectureid):
 		return redirect(request.values['ref'])
 	return 'OK',  200
 
+@app.route('/chapters/<int:lectureid>')
+def chapters(lectureid):
+	chapters = query("SELECT * FROM chapters WHERE lecture_id = ? and visible ORDER BY time DESC", lectureid)
+	last = None
+	for c in chapters:
+		c['start'] = c['time']
+		c['end'] = last['start'] if last else 9999
+		last = c
+	return Response(render_template('chapters.srt',chapters=chapters), 200, {'Content-Type':'text/vtt'})
+
 @app.route('/sitemap.xml')
 def sitemap():
 	pages=[]
diff --git a/templates/chapters.srt b/templates/chapters.srt
new file mode 100644
index 0000000..bfb4632
--- /dev/null
+++ b/templates/chapters.srt
@@ -0,0 +1,11 @@
+{% macro vtttime(time) %}{{ '%02d:%02d:%02d.000'|format( time//3600, (time//60)%60, time%60) }}{% endmacro %}
+WEBVTT
+
+
+{% for c in chapters|sort(attribute='start') %}
+{{ loop.index }}
+{# time format is 'minutes:seconds.ms' we expect full seconds #}
+{{ vtttime(c['start']) }} --> {{ vtttime(c['end']) }}
+{{ c['text'] }}
+
+{% endfor %}
diff --git a/templates/macros.html b/templates/macros.html
index 38aca6b..35e5291 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -49,6 +49,7 @@
 	{% for v in videos|sort(attribute='player_prio', reverse=True) %}
 		<source type="video/mp4" src="{{ config.VIDEOPREFIX }}/{{ v.path }}" title="{{ v.format_description }}"/>
 	{% endfor %}
+	<track srclang="en" kind="chapters" src="{{ url_for('chapters',lectureid=lecture.id) }}" />
 </video>
 <script>
 		$(function($)
-- 
GitLab