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 loop_card = models.FileField(upload_to='youtube/card', blank=True)
22 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
23 thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
24 thumbnail_definition = models.TextField(blank=True)
25 genres = models.CharField(max_length=2048, blank=True)
28 verbose_name = _("YouTube configuration")
29 verbose_name_plural = _("YouTube configurations")
31 def publish(self, audiobook, path):
32 ctx = Context(dict(audiobook=audiobook))
33 description = Template(self.description_template).render(ctx)
34 title = Template(self.title_template).render(ctx)
40 description=description,
42 # categoryId=category,
46 privacyStatus=privacy,
48 # selfDeclaredMadeForKids
50 # recordingDetails=dict(
54 part = ",".join(data.keys())
56 with open(path, "rb") as f:
57 response = youtube_call(
59 "https://www.googleapis.com/upload/youtube/v3/videos",
60 params={'part': part},
64 data = response.json()
65 audiobook.youtube_id = data['id']
66 audiobook.save(update_fields=['youtube_id'])
69 def prepare_file(self, input_path, output_path=None):
70 duration = get_duration(input_path)
71 video = self.prepare_video(duration)
72 output = mux([video, input_path], output_path=output_path)
76 def prepare_video(self, duration):
82 fps = get_framerate(self.loop_video.path)
86 loop_duration = duration
87 for card in self.card_set.filter(order__lt=0, duration__gt=0):
88 loop_duration -= card.duration
89 card_video = video_from_image(
90 card.image.path, card.duration, fps=fps
92 (concat if card.order < 0 else outro).append(card_video)
96 loop_video_duration = get_duration(self.loop_video.path)
97 times_loop = int(loop_duration // loop_video_duration)
99 leftover_duration = loop_duration % loop_video_duration
100 leftover = cut_video(self.loop_video.path, leftover_duration)
101 concat.extend([self.loop_video.path] * times_loop + [leftover])
102 delete.append(leftover)
104 leftover = video_from_image(self.loop_card.path, loop_duration)
105 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
106 delete.append(leftover)
109 output = concat_videos(concat)
116 # selfDeclaredMadeForKids
119 class Card(models.Model):
120 youtube = models.ForeignKey(YouTube, models.CASCADE)
121 order = models.SmallIntegerField()
122 image = models.FileField(upload_to='youtube/card')
123 duration = models.FloatField()
126 ordering = ('order', )