2 from tempfile import NamedTemporaryFile
3 from django.db import models
4 from django.utils.translation import gettext_lazy as _
5 from django.template import Template, Context
6 from apiclient import youtube_call
17 class YouTube(models.Model):
18 title_template = models.CharField(max_length=1024, blank=True)
19 description_template = models.TextField(blank=True)
20 category = models.IntegerField(null=True, blank=True) # get categories
21 intro_card = models.FileField(blank=True)
22 intro_card_duration = models.FloatField(null=True, blank=True)
23 card = models.FileField(blank=True)
24 loop_video = models.FileField(blank=True)
25 outro_card = models.FileField(blank=True)
26 outro_card_duration = models.FloatField(null=True, blank=True)
29 verbose_name = _("YouTube configuration")
30 verbose_name_plural = _("YouTube configurations")
32 def publish(self, audiobook, path):
33 ctx = Context(dict(audiobook=audiobook))
34 description = Template(self.description_template).render(ctx)
35 title = Template(self.title_template).render(ctx)
41 description=description,
43 # categoryId=category,
47 privacyStatus=privacy,
49 # selfDeclaredMadeForKids
51 # recordingDetails=dict(
55 part = ",".join(data.keys())
57 with open(path, "rb") as f:
58 response = youtube_call(
60 "https://www.googleapis.com/upload/youtube/v3/videos",
61 params={'part': part},
65 data = response.json()
66 audiobook.youtube_id = data['id']
67 audiobook.save(update_fields=['youtube_id'])
70 def prepare_file(self, input_path, output_path=None):
71 duration = get_duration(input_path)
72 video = self.prepare_video(duration)
73 output = mux([video, input_path], output_path=output_path)
77 def prepare_video(self, duration):
82 fps = get_framerate(self.loop_video.path)
86 loop_duration = duration
87 if self.intro_card and self.intro_card_duration:
88 loop_duration -= self.intro_card_duration
89 intro = video_from_image(
90 self.intro_card.path, self.intro_card_duration, fps=fps
95 if self.outro_card and self.outro_card_duration:
96 loop_duration -= self.outro_card_duration
97 outro = video_from_image(
98 self.outro_card.path, self.outro_card_duration, fps=fps
104 loop_video_duration = get_duration(self.loop_video.path)
105 times_loop = int(loop_duration // loop_video_duration)
107 leftover_duration = loop_duration % loop_video_duration
108 leftover = cut_video(self.loop_video.path, leftover_duration)
109 concat[1:1] = [self.loop_video.path] * times_loop + [leftover]
110 delete.append(leftover)
112 leftover = video_from_image(self.card.path, loop_duration)
113 concat.insert(1, video_from_image(self.card.path, loop_duration, fps=fps))
114 delete.append(leftover)
116 output = concat_videos(concat)
123 # selfDeclaredMadeForKids