support vtt subtitles

This commit is contained in:
j 2016-07-01 18:10:01 +02:00
parent f8f951c966
commit 6f4a0ba90a
3 changed files with 18 additions and 8 deletions

View file

@ -19,7 +19,7 @@ class Command(BaseCommand):
"""
import annotations
"""
help = 'import annotations from srt'
help = 'import annotations from srt or vtt'
args = 'username item layername filename.srt'
option_list = BaseCommand.option_list + (
)
@ -33,6 +33,9 @@ class Command(BaseCommand):
item = Item.objects.get(public_id=public_id)
layer = filter(lambda l: l['id'] == layer_id, settings.CONFIG['layers'])[0]
if filename.endswith('.vtt'):
annotations = ox.vtt.load(filename)
else:
annotations = ox.srt.load(filename)
print('importing %d annotations into %s/%s' % (len(annotations), public_id, layer_id))
for i in range(len(annotations)-1):

View file

@ -256,7 +256,7 @@ class File(models.Model):
self.sort_path = utils.sort_string(self.path)
self.is_audio = self.type == 'audio'
self.is_video = self.type == 'video'
self.is_subtitle = self.path.endswith('.srt')
self.is_subtitle = self.path.split('.')[-1] in ('srt', 'vtt')
if self.type not in ('audio', 'video'):
self.duration = None
@ -289,7 +289,11 @@ class File(models.Model):
def srt(self, offset=0):
srt = []
subtitles = []
for s in ox.srt.load(self.data.path):
if self.data.path.endswith('.vtt'):
load = ox.vtt.load
else:
load = ox.srt.load
for s in load(self.data.path):
if s['in'] <= s['out'] and s['value'].strip():
key = '%s --> %s\n%s' % (s['in'], s['out'], s['value'])
if key not in subtitles:

View file

@ -103,10 +103,13 @@ def update(request, data):
file__wanted=True)]
if filter(lambda l: l['id'] == 'subtitles', settings.CONFIG['layers']):
response['data']['file'] = [f.file.oshash
for f in files.filter(file__is_subtitle=True,
file__available=False,
path__endswith='.srt')]
qs = files.filter(
file__is_subtitle=True,
file__available=False
).filter(
Q(path__endswith='.srt') | Q(path__endswith='.vtt')
)
response['data']['file'] = [f.file.oshash for f in qs]
else:
response['data']['file'] = []
return render_to_json_response(response)