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
8 from apiclient import youtube_call
20 from .thumbnail import create_thumbnail
23 YOUTUBE_TITLE_LIMIT = 100
26 class YouTube(models.Model):
27 name = models.CharField(max_length=255)
28 title_template = models.CharField(max_length=1024, blank=True)
29 description_template = models.TextField(blank=True)
30 category = models.IntegerField(null=True, blank=True, choices=[
33 intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
34 outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
35 loop_card = models.FileField(upload_to='youtube/card', blank=True)
36 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
37 privacy_status = models.CharField(max_length=16, choices=[
38 ('public', _('public')),
39 ('unlisted', _('unlisted')),
40 ('private', _('private')),
42 genres = models.CharField(max_length=2048, blank=True)
45 verbose_name = _("YouTube configuration")
46 verbose_name_plural = _("YouTube configurations")
51 def get_context(self, audiobook):
56 def get_description(self, audiobook):
57 return Template(self.description_template).render(self.get_context(audiobook))
59 def get_title(self, audiobook):
60 return Template(self.title_template).render(self.get_context(audiobook))[:YOUTUBE_TITLE_LIMIT]
62 def get_data(self, audiobook):
65 title=self.get_title(audiobook),
66 description=self.get_description(audiobook),
67 categoryId=self.category,
69 defaultAudioLanguage='pl',
72 privacyStatus=self.privacy_status,
76 def publish(self, audiobook, path):
77 data = self.get_data(audiobook)
78 part = ",".join(data.keys())
80 response = youtube_call(
82 "https://www.googleapis.com/upload/youtube/v3/videos",
83 params={'part': part},
85 resumable_file_path=path,
87 data = response.json()
88 audiobook.youtube_id = data['id']
89 audiobook.save(update_fields=['youtube_id'])
91 self.update_thumbnail(audiobook)
94 def update_data(self, audiobook):
95 data = self.get_data(audiobook)
96 data['id'] = audiobook.youtube_id
97 part = ",".join(data.keys())
100 "https://www.googleapis.com/youtube/v3/videos",
101 params={"part": part},
105 def prepare_file(self, input_paths, output_path=None):
106 audio = self.prepare_audio(input_paths)
107 duration = self.get_duration(input_paths)
108 video = self.prepare_video(duration)
109 output = mux([video, audio], output_path=output_path)
114 def get_duration(self, input_paths):
116 for input_path in input_paths:
117 d += get_duration(input_path)
119 d += get_duration(self.intro_flac.path)
121 d += get_duration(self.outro_flac.path)
124 def prepare_audio(self, input_paths):
127 files.append(standardize_audio(self.intro_flac.path))
128 for input_path in input_paths:
129 files.append(standardize_audio(input_path, cache=False))
131 files.append(standardize_audio(self.outro_flac.path))
132 output = concat_audio(files)
137 def prepare_video(self, duration):
143 fps = get_framerate(self.loop_video.path)
144 loop_video = standardize_video(self.loop_video.path)
148 loop_duration = duration
149 for card in self.card_set.filter(duration__gt=0):
150 loop_duration -= card.duration
151 card_video = video_from_image(
152 card.image.path, card.duration, fps=fps
154 (concat if card.order < 0 else outro).append(card_video)
155 delete.append(card_video)
158 loop_video_duration = get_duration(loop_video)
159 times_loop = int(loop_duration // loop_video_duration)
161 leftover_duration = loop_duration % loop_video_duration
162 leftover = cut_video(loop_video, leftover_duration)
163 concat.extend([loop_video] * times_loop + [leftover])
164 delete.append(leftover)
166 leftover = video_from_image(self.loop_card.path, loop_duration)
167 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
168 delete.append(leftover)
171 output = concat_videos(concat)
179 # selfDeclaredMadeForKids
181 def update_thumbnail(self, audiobook):
182 thumbnail = self.prepare_thumbnail(audiobook)
183 if thumbnail is not None:
184 response = youtube_call(
186 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
187 params={'videoId': audiobook.youtube_id},
188 data=thumbnail.getvalue(),
191 def prepare_thumbnail(self, audiobook):
192 for thumbnail_template in ThumbnailTemplate.objects.filter(is_active=True).order_by('order'):
193 if not thumbnail_template.is_for_audiobook(audiobook):
195 thumbnail = thumbnail_template.generate(audiobook)
196 if thumbnail is not None:
200 class Card(models.Model):
201 youtube = models.ForeignKey(YouTube, models.CASCADE)
202 order = models.SmallIntegerField()
203 image = models.FileField(upload_to='youtube/card')
204 duration = models.FloatField()
207 ordering = ('order', )
210 class Font(models.Model):
211 name = models.CharField(max_length=255, unique=True)
212 truetype = models.FileField(upload_to='youtube/font')
218 class ThumbnailTemplate(models.Model):
219 youtube = models.ForeignKey(YouTube, models.CASCADE)
220 order = models.SmallIntegerField()
221 is_active = models.BooleanField()
222 background = models.FileField(upload_to='youtube/thumbnail')
223 definition = models.TextField()
224 authors = models.CharField(max_length=255, blank=True)
225 epochs = models.CharField(max_length=255, blank=True)
226 kinds = models.CharField(max_length=255, blank=True)
227 genres = models.CharField(max_length=255, blank=True)
228 collections = models.CharField(max_length=255, blank=True)
231 ordering = ('order', )
233 def generate(self, audiobook):
235 title = audiobook.book['title']
236 if audiobook.book.get('parent'):
237 parent_title = audiobook.book['parent']['title']
238 if not title.startswith(parent_title):
239 title = ", ".join((parent_title, title))
241 img = create_thumbnail(
242 self.background.path,
245 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
247 "part": (audiobook.youtube_volume or audiobook.part_name).strip(),
249 lambda name: Font.objects.get(name=name).truetype.path
251 except Exception as e:
256 img.save(buf, format='PNG')
259 def is_for_audiobook(self, audiobook):
260 for category in 'authors', 'epochs', 'kinds', 'genres':
261 if getattr(self, category):
262 book_slugs = set([g['slug'] for g in audiobook.book[category]])
263 template_slugs = set([g.strip() for g in getattr(self, category).split(',')])
264 if not book_slugs.intersection(template_slugs):
268 template_collections = set([g.strip() for g in self.collections.split(',')])
270 for collection in template_collections:
271 apidata = requests.get(
272 f'https://wolnelektury.pl/api/collections/{collection}/'
274 for book in apidata['books']:
275 if book['slug'] == audiobook.slug: