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
8 from archive.settings import LICENSE, LICENSE_NAME
20 from .thumbnail import create_thumbnail
23 class YouTube(models.Model):
24 title_template = models.CharField(max_length=1024, blank=True)
25 description_template = models.TextField(blank=True)
26 category = models.IntegerField(null=True, blank=True, choices=[
29 intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
30 outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
31 loop_card = models.FileField(upload_to='youtube/card', blank=True)
32 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
33 thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
34 thumbnail_definition = models.TextField(blank=True)
35 privacy_status = models.CharField(max_length=16, choices=[
36 ('public', _('public')),
37 ('unlisted', _('unlisted')),
38 ('private', _('private')),
40 genres = models.CharField(max_length=2048, blank=True)
43 verbose_name = _("YouTube configuration")
44 verbose_name_plural = _("YouTube configurations")
46 def get_context(self, audiobook):
50 LICENSE_NAME=LICENSE_NAME,
53 def get_description(self, audiobook):
54 return Template(self.description_template).render(self.get_context(audiobook))
56 def get_title(self, audiobook):
57 return Template(self.title_template).render(self.get_context(audiobook))
59 def get_data(self, audiobook):
62 title=self.get_title(audiobook),
63 description=self.get_description(audiobook),
64 categoryId=self.category,
66 defaultAudioLanguage='pl',
69 privacyStatus=self.privacy_status,
73 def publish(self, audiobook, path):
74 data = self.get_data(audiobook)
75 part = ",".join(data.keys())
77 with open(path, "rb") as f:
78 response = youtube_call(
80 "https://www.googleapis.com/upload/youtube/v3/videos",
81 params={'part': part},
83 resumable_data=f.read(),
85 data = response.json()
86 audiobook.youtube_id = data['id']
87 audiobook.save(update_fields=['youtube_id'])
89 self.update_thumbnail(audiobook)
92 def update_data(self, audiobook):
93 data = self.get_data(audiobook)
94 data['id'] = audiobook.youtube_id
95 part = ",".join(data.keys())
98 "https://www.googleapis.com/youtube/v3/videos",
99 params={"part": part},
103 def prepare_file(self, input_path, output_path=None):
104 audio = self.prepare_audio(input_path)
105 duration = self.get_duration(input_path)
106 video = self.prepare_video(duration)
107 output = mux([video, audio], output_path=output_path)
112 def get_duration(self, input_path):
113 d = get_duration(input_path)
115 d += get_duration(self.intro_flac.path)
117 d += get_duration(self.outro_flac.path)
120 def prepare_audio(self, input_path):
124 files.append(standardize_audio(self.intro_flac.path))
125 delete.append(files[-1])
126 files.append(input_path)
128 files.append(standardize_audio(self.outro_flac.path))
129 delete.append(files[-1])
130 output = concat_audio(files)
136 def prepare_video(self, duration):
142 fps = get_framerate(self.loop_video.path)
143 loop_video = standardize_video(self.loop_video.path)
147 loop_duration = duration
148 for card in self.card_set.filter(duration__gt=0):
149 loop_duration -= card.duration
150 card_video = video_from_image(
151 card.image.path, card.duration, fps=fps
153 (concat if card.order < 0 else outro).append(card_video)
154 delete.append(card_video)
157 loop_video_duration = get_duration(loop_video)
158 times_loop = int(loop_duration // loop_video_duration)
160 leftover_duration = loop_duration % loop_video_duration
161 leftover = cut_video(loop_video, leftover_duration)
162 concat.extend([loop_video] * times_loop + [leftover])
163 delete.append(leftover)
165 leftover = video_from_image(self.loop_card.path, loop_duration)
166 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
167 delete.append(leftover)
170 output = concat_videos(concat)
178 # selfDeclaredMadeForKids
180 def update_thumbnail(self, audiobook):
181 thumbnail = self.prepare_thumbnail(audiobook)
182 response = youtube_call(
184 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
185 params={'videoId': audiobook.youtube_id},
186 data=thumbnail.getvalue(),
189 def prepare_thumbnail(self, audiobook):
190 img = create_thumbnail(
191 self.thumbnail_template.path,
192 self.thumbnail_definition,
194 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
195 "title": audiobook.book['title'],
197 lambda name: Font.objects.get(name=name).truetype.path
200 img.save(buf, format='PNG')
204 class Card(models.Model):
205 youtube = models.ForeignKey(YouTube, models.CASCADE)
206 order = models.SmallIntegerField()
207 image = models.FileField(upload_to='youtube/card')
208 duration = models.FloatField()
211 ordering = ('order', )
214 class Font(models.Model):
215 name = models.CharField(max_length=255, unique=True)
216 truetype = models.FileField(upload_to='youtube/font')