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(upload_to='youtube/intro_card', blank=True)
22 intro_card_duration = models.FloatField(null=True, blank=True)
23 card = models.FileField(upload_to='youtube/card', blank=True)
24 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
25 outro_card = models.FileField(upload_to='youtube/outro_card', blank=True)
26 outro_card_duration = models.FloatField(null=True, blank=True)
27 thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
28 genres = models.CharField(max_length=2048, blank=True)
31 verbose_name = _("YouTube configuration")
32 verbose_name_plural = _("YouTube configurations")
34 def publish(self, audiobook, path):
35 ctx = Context(dict(audiobook=audiobook))
36 description = Template(self.description_template).render(ctx)
37 title = Template(self.title_template).render(ctx)
43 description=description,
45 # categoryId=category,
49 privacyStatus=privacy,
51 # selfDeclaredMadeForKids
53 # recordingDetails=dict(
57 part = ",".join(data.keys())
59 with open(path, "rb") as f:
60 response = youtube_call(
62 "https://www.googleapis.com/upload/youtube/v3/videos",
63 params={'part': part},
67 data = response.json()
68 audiobook.youtube_id = data['id']
69 audiobook.save(update_fields=['youtube_id'])
72 def prepare_file(self, input_path, output_path=None):
73 duration = get_duration(input_path)
74 video = self.prepare_video(duration)
75 output = mux([video, input_path], output_path=output_path)
79 def prepare_video(self, duration):
84 fps = get_framerate(self.loop_video.path)
88 loop_duration = duration
89 if self.intro_card and self.intro_card_duration:
90 loop_duration -= self.intro_card_duration
91 intro = video_from_image(
92 self.intro_card.path, self.intro_card_duration, fps=fps
97 if self.outro_card and self.outro_card_duration:
98 loop_duration -= self.outro_card_duration
99 outro = video_from_image(
100 self.outro_card.path, self.outro_card_duration, fps=fps
106 loop_video_duration = get_duration(self.loop_video.path)
107 times_loop = int(loop_duration // loop_video_duration)
109 leftover_duration = loop_duration % loop_video_duration
110 leftover = cut_video(self.loop_video.path, leftover_duration)
111 concat[1:1] = [self.loop_video.path] * times_loop + [leftover]
112 delete.append(leftover)
114 leftover = video_from_image(self.card.path, loop_duration)
115 concat.insert(1, video_from_image(self.card.path, loop_duration, fps=fps))
116 delete.append(leftover)
118 output = concat_videos(concat)
125 # selfDeclaredMadeForKids