Support WebVTT subtitle export

This commit is contained in:
Will Thompson 2016-03-11 11:48:13 +00:00 committed by j
parent 36463a8120
commit eeaeda3970
3 changed files with 17 additions and 11 deletions

View file

@ -1613,7 +1613,7 @@ class Item(models.Model):
self.add_empty_clips() self.add_empty_clips()
return False return False
def srt(self, layer, language=None): def srt(self, layer, language=None, encoder=ox.srt):
def format_value(value): def format_value(value):
value = value.replace('<br/>', '<br>').replace('<br>\n', '\n').replace('<br>', '\n') value = value.replace('<br/>', '<br>').replace('<br>\n', '\n').replace('<br>', '\n')
value = value.replace('\n\n', '<br>\n') value = value.replace('\n\n', '<br>\n')
@ -1621,7 +1621,8 @@ class Item(models.Model):
annotations = self.annotations.filter(layer=layer).exclude(value='') annotations = self.annotations.filter(layer=layer).exclude(value='')
if language: if language:
annotations = annotations.filter(languages__contains=language) annotations = annotations.filter(languages__contains=language)
return ox.srt.encode([{
return encoder.encode([{
'in': a.start, 'in': a.start,
'out': a.end, 'out': a.end,
'value': format_value(a.value) 'value': format_value(a.value)

View file

@ -31,10 +31,7 @@ urlpatterns = [
url(r'^(?P<id>[A-Z0-9].*)/xml$', views.item_xml), url(r'^(?P<id>[A-Z0-9].*)/xml$', views.item_xml),
#srt export #srt export
url(r'^(?P<id>[A-Z0-9].*)/(?P<layer>.+)\.(?P<language>.{2})\.srt$', views.srt), url(r'^(?P<id>[A-Z0-9].*)/(?P<layer>.+)\.(?:(?P<language>.{2})\.)?(?P<ext>srt|vtt)$', views.srt),
#srt export
url(r'^(?P<id>[A-Z0-9].*)/(?P<layer>.+)\.srt$', views.srt),
#icon #icon
url(r'^(?P<id>[A-Z0-9].*)/icon(?P<size>\d*)\.jpg$', views.icon), url(r'^(?P<id>[A-Z0-9].*)/icon(?P<size>\d*)\.jpg$', views.icon),

View file

@ -968,19 +968,27 @@ def video(request, id, resolution, format, index=None, track=None):
response['Cache-Control'] = 'public' response['Cache-Control'] = 'public'
return response return response
def srt(request, id, layer, language=None, index=None):
_subtitle_formats = {
'srt': ('text/x-srt', ox.srt),
'vtt': ('text/vtt', ox.vtt),
}
def srt(request, id, layer, language=None, index=None, ext='srt'):
item = get_object_or_404(models.Item, public_id=id) item = get_object_or_404(models.Item, public_id=id)
if not item.access(request.user): if not item.access(request.user):
response = HttpResponseForbidden() response = HttpResponseForbidden()
else: else:
content_type, encoder = _subtitle_formats[ext]
response = HttpResponse() response = HttpResponse()
if language: if language:
filename = u"%s.%s.srt" % (item.get('title'), language) filename = u"%s.%s.%s" % (item.get('title'), language, ext)
else: else:
filename = u"%s.srt" % item.get('title') filename = u"%s.%s" % (item.get('title'), ext)
response['Content-Disposition'] = "attachment; filename*=UTF-8''%s" % quote(filename.encode('utf-8')) response['Content-Disposition'] = "attachment; filename*=UTF-8''%s" % quote(filename.encode('utf-8'))
response['Content-Type'] = 'text/x-srt' response['Content-Type'] = content_type
response.write(item.srt(layer, language)) response.write(item.srt(layer, language, encoder=encoder))
return response return response
def random_annotation(request): def random_annotation(request):