Ejemplo n.º 1
0
def enrichment_to_vtt(list_enrichment, video):
    webvtt = WebVTT()
    for enrich in list_enrichment:
        start = datetime.datetime.utcfromtimestamp(
            enrich.start).strftime("%H:%M:%S.%f")[:-3]
        end = datetime.datetime.utcfromtimestamp(
            enrich.end).strftime("%H:%M:%S.%f")[:-3]
        url = enrichment_to_vtt_type(enrich)
        caption = Caption(
            "{0}".format(start),
            "{0}".format(end),
            [
                "{",
                '"title": "{0}",'.format(enrich.title),
                '"type": "{0}",'.format(enrich.type),
                '"stop_video": "{0}",'.format("%s" %
                                              1 if enrich.stop_video else 0),
                '"url": "{0}"'.format(url),
                "}",
            ],
        )
        caption.identifier = enrich.slug
        webvtt.captions.append(caption)
    temp_vtt_file = NamedTemporaryFile(suffix=".vtt")
    with open(temp_vtt_file.name, "w") as f:
        webvtt.write(f)
    if FILEPICKER:
        videodir, created = UserFolder.objects.get_or_create(name="%s" %
                                                             video.slug,
                                                             owner=video.owner)
        previousEnrichmentFile = CustomFileModel.objects.filter(
            name__startswith="enrichment",
            folder=videodir,
            created_by=video.owner,
        )
        for enr in previousEnrichmentFile:
            enr.delete()  # do it like this to delete file
        enrichmentFile, created = CustomFileModel.objects.get_or_create(
            name="enrichment", folder=videodir, created_by=video.owner)

        if enrichmentFile.file and os.path.isfile(enrichmentFile.file.path):
            os.remove(enrichmentFile.file.path)
    else:
        enrichmentFile, created = CustomFileModel.objects.get_or_create()
    enrichmentFile.file.save("enrichment.vtt", File(temp_vtt_file))
    enrichmentVtt, created = EnrichmentVtt.objects.get_or_create(video=video)
    enrichmentVtt.src = enrichmentFile
    enrichmentVtt.save()
    return enrichmentFile.file.path
Ejemplo n.º 2
0
    def translate(self):
        newVTT = WebVTT()
        fileName = self.fileNameWOType + '.vtt'
        for caption in webvtt.read(fileName):
            #            print(caption.start)
            #            print(caption.end)
            #            print(caption.text)
            translation = Translate.AWSTranslate.translate_text(
                Text=caption.text,
                SourceLanguageCode=self.sourceLanguage,
                TargetLanguageCode=self.targetLanguage)

            newCaption = Caption(caption.start, caption.end,
                                 translation.get('TranslatedText'))
            newCaption.identifier = caption.identifier
            newVTT.captions.append(newCaption)

        translatedFileName = self.fileNameWOType + '_' + self.targetLanguage + '.vtt'
        newVTT.save(translatedFileName)
        return 1