only update subtitles if needed

This commit is contained in:
j 2016-09-06 00:13:40 +02:00
parent d53659c0c4
commit 4d1711cb01
2 changed files with 66 additions and 69 deletions

View file

@ -133,8 +133,7 @@ class Annotation(models.Model):
return {} return {}
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
from .tasks import update_matches delay_matches = kwargs.pop('delay_matches', False)
async = kwargs.pop('async', False)
set_public_id = not self.id or not self.public_id set_public_id = not self.id or not self.public_id
layer = self.get_layer() layer = self.get_layer()
@ -181,11 +180,8 @@ class Annotation(models.Model):
# update clip.findvalue # update clip.findvalue
self.clip.save() self.clip.save()
# editAnnotations needs to be in snyc # update matches in bulk if called from load_subtitles
# load_subtitles can not be in sync if not delay_matches:
if async:
update_matches.delay(self.id)
else:
self.update_matches() self.update_matches()
def update_matches(self): def update_matches(self):

View file

@ -1588,69 +1588,70 @@ class Item(models.Model):
# only import on 0xdb for now or if forced manually # only import on 0xdb for now or if forced manually
# since this will remove all existing subtitles # since this will remove all existing subtitles
if force or not existing.count() or settings.USE_IMDB: if force or not existing.count() or settings.USE_IMDB:
with transaction.atomic(): new = []
Annotation.objects.filter(layer=layer, item=self).delete() current = [(v.start, v.end, v.value) for v in Annotation.objects.filter(layer=layer, item=self)]
AnnotationSequence.reset(self) current.sort()
offset = 0 offset = 0
language = '' language = ''
subtitles = self.files.filter(selected=True, is_subtitle=True, available=True) subtitles = self.files.filter(selected=True, is_subtitle=True, available=True)
languages = [f.language for f in subtitles] languages = [f.language for f in subtitles]
if languages: if languages:
if 'en' in languages: if 'en' in languages:
language = 'en' language = 'en'
elif '' in languages: elif '' in languages:
language = '' language = ''
else: else:
language = languages[0] language = languages[0]
# loop over all videos # loop over all videos
for f in self.files.filter(Q(is_audio=True) | Q(is_video=True)) \ for f in self.files.filter(Q(is_audio=True) | Q(is_video=True)) \
.filter(selected=True).order_by('sort_path'): .filter(selected=True).order_by('sort_path'):
subtitles_added = False subtitles_added = False
prefix = os.path.splitext(f.path)[0] prefix = os.path.splitext(f.path)[0]
if f.instances.all().count() > 0: if f.instances.all().count() > 0:
user = f.instances.all()[0].volume.user user = f.instances.all()[0].volume.user
else: else:
# FIXME: allow annotations from no user instead? # FIXME: allow annotations from no user instead?
user = User.objects.all().order_by('id')[0] user = User.objects.all().order_by('id')[0]
# if there is a subtitle with the same prefix, import # if there is a subtitle with the same prefix, import
q = subtitles.filter(path__startswith=prefix, q = subtitles.filter(path__startswith=prefix,
language=language) language=language)
if q.count() == 1: if q.count() == 1:
s = q[0] s = q[0]
for data in s.srt(offset): for data in s.srt(offset):
subtitles_added = True subtitles_added = True
value = data['value'].replace('\n', '<br>\n').replace('<br><br>\n', '<br>\n') value = data['value'].replace('\n', '<br>\n').replace('<br><br>\n', '<br>\n')
if data['in'] < self.json['duration'] and data['out'] > self.json['duration']: if data['in'] < self.json['duration'] and data['out'] > self.json['duration']:
data['out'] = self.json['duration'] data['out'] = self.json['duration']
if data['in'] < self.json['duration']: if data['in'] < self.json['duration']:
annotation = Annotation( new.append((float('%0.03f' % data['in']), float('%0.03f' % data['out']), value))
item=self, # otherwise add empty 5 seconds annotation every minute
layer=layer, if not subtitles_added:
start=float('%0.03f' % data['in']), start = offset and int(offset / 60) * 60 + 60 or 0
end=float('%0.03f' % data['out']), for i in range(start,
value=value, int(offset + f.duration) - 5,
user=user 60):
) new.append((i, i+5, ''))
annotation.save(async=True) offset += f.duration
# otherwise add empty 5 seconds annotation every minute if current != new:
if not subtitles_added: with transaction.atomic():
start = offset and int(offset / 60) * 60 + 60 or 0 # FIXME: only reset if most subtitles are new
for i in range(start, Annotation.objects.filter(layer=layer, item=self).delete()
int(offset + f.duration) - 5, AnnotationSequence.reset(self)
60): for start, end, value in new:
annotation = Annotation( annotation = Annotation(
item=self, item=self,
layer=layer, layer=layer,
start=i, start=start,
end=i + 5, end=end,
value='', value=value,
user=user user=user
) )
annotation.save(async=True) annotation.save(delay_matches=True)
offset += f.duration # remove left over clips without annotations
# remove left over clips without annotations Clip.objects.filter(item=self, annotations__id=None).delete()
Clip.objects.filter(item=self, annotations__id=None).delete() for a in self.annotations.filter(layer=layer):
a.update_matches()
return True return True
else: else:
self.add_empty_clips() self.add_empty_clips()