3 from tempfile import NamedTemporaryFile
4 from django.db import models
5 from django.utils.translation import gettext_lazy as _
6 from django.template import Template, Context
7 from apiclient import youtube_call
18 from .thumbnail import create_thumbnail
21 class YouTube(models.Model):
22 title_template = models.CharField(max_length=1024, blank=True)
23 description_template = models.TextField(blank=True)
24 category = models.IntegerField(null=True, blank=True, choices=[
27 intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
28 outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
29 loop_card = models.FileField(upload_to='youtube/card', blank=True)
30 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
31 thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
32 thumbnail_definition = models.TextField(blank=True)
33 privacy_status = models.CharField(max_length=16, choices=[
34 ('public', _('public')),
35 ('unlisted', _('unlisted')),
36 ('private', _('private')),
38 genres = models.CharField(max_length=2048, blank=True)
41 verbose_name = _("YouTube configuration")
42 verbose_name_plural = _("YouTube configurations")
44 def get_context(self, audiobook):
45 return Context(dict(audiobook=audiobook))
47 def get_description(self, audiobook):
48 return Template(self.description_template).render(self.get_context(audiobook))
50 def get_title(self, audiobook):
51 return Template(self.title_template).render(self.get_context(audiobook))
53 def get_data(self, audiobook):
56 title=self.get_title(audiobook),
57 description=self.get_description(audiobook),
58 categoryId=self.category,
60 defaultAudioLanguage='pl',
63 privacyStatus=self.privacy_status,
67 def publish(self, audiobook, path):
68 data = self.get_data(audiobook)
69 part = ",".join(data.keys())
71 with open(path, "rb") as f:
72 response = youtube_call(
74 "https://www.googleapis.com/upload/youtube/v3/videos",
75 params={'part': part},
77 resumable_data=f.read(),
79 data = response.json()
80 audiobook.youtube_id = data['id']
81 audiobook.save(update_fields=['youtube_id'])
83 self.update_thumbnail(audiobook)
86 def update_data(self, audiobook):
87 data = self.get_data(audiobook)
88 data['id'] = audiobook.youtube_id
89 part = ",".join(data.keys())
92 "https://www.googleapis.com/youtube/v3/videos",
93 params={"part": part},
97 def prepare_file(self, input_path, output_path=None):
98 audio = self.prepare_audio(input_path)
99 duration = self.get_duration(input_path)
100 video = self.prepare_video(duration)
101 output = mux([video, audio], output_path=output_path)
106 def get_duration(self, input_path):
107 d = get_duration(input_path)
109 d += get_duration(self.intro_flac.path)
111 d += get_duration(self.outro_flac.path)
114 def prepare_audio(self, input_path):
117 files.append(self.intro_flac.path)
118 files.append(input_path)
120 files.append(self.outro_flac.path)
121 return concat_audio(files)
123 def prepare_video(self, duration):
129 fps = get_framerate(self.loop_video.path)
130 loop_video = standardize_video(self.loop_video.path)
134 loop_duration = duration
135 for card in self.card_set.filter(duration__gt=0):
136 loop_duration -= card.duration
137 card_video = video_from_image(
138 card.image.path, card.duration, fps=fps
140 (concat if card.order < 0 else outro).append(card_video)
141 delete.append(card_video)
144 loop_video_duration = get_duration(loop_video)
145 times_loop = int(loop_duration // loop_video_duration)
147 leftover_duration = loop_duration % loop_video_duration
148 leftover = cut_video(loop_video, leftover_duration)
149 concat.extend([loop_video] * times_loop + [leftover])
150 delete.append(leftover)
152 leftover = video_from_image(self.loop_card.path, loop_duration)
153 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
154 delete.append(leftover)
157 output = concat_videos(concat)
165 # selfDeclaredMadeForKids
167 def update_thumbnail(self, audiobook):
168 thumbnail = self.prepare_thumbnail(audiobook)
169 response = youtube_call(
171 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
172 params={'videoId': audiobook.youtube_id},
173 data=thumbnail.getvalue(),
176 def prepare_thumbnail(self, audiobook):
177 img = create_thumbnail(
178 self.thumbnail_template.path,
179 self.thumbnail_definition,
181 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
182 "title": audiobook.book['title'],
184 lambda name: Font.objects.get(name=name).truetype.path
187 img.save(buf, format='PNG')
191 class Card(models.Model):
192 youtube = models.ForeignKey(YouTube, models.CASCADE)
193 order = models.SmallIntegerField()
194 image = models.FileField(upload_to='youtube/card')
195 duration = models.FloatField()
198 ordering = ('order', )
201 class Font(models.Model):
202 name = models.CharField(max_length=255, unique=True)
203 truetype = models.FileField(upload_to='youtube/font')