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):
123 files.append(standardize_audio(self.intro_flac.path))
124 files.append(standardize_audio(input_path, cache=False))
126 files.append(standardize_audio(self.outro_flac.path))
127 output = concat_audio(files)
133 def prepare_video(self, duration):
139 fps = get_framerate(self.loop_video.path)
140 loop_video = standardize_video(self.loop_video.path)
144 loop_duration = duration
145 for card in self.card_set.filter(duration__gt=0):
146 loop_duration -= card.duration
147 card_video = video_from_image(
148 card.image.path, card.duration, fps=fps
150 (concat if card.order < 0 else outro).append(card_video)
151 delete.append(card_video)
154 loop_video_duration = get_duration(loop_video)
155 times_loop = int(loop_duration // loop_video_duration)
157 leftover_duration = loop_duration % loop_video_duration
158 leftover = cut_video(loop_video, leftover_duration)
159 concat.extend([loop_video] * times_loop + [leftover])
160 delete.append(leftover)
162 leftover = video_from_image(self.loop_card.path, loop_duration)
163 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
164 delete.append(leftover)
167 output = concat_videos(concat)
175 # selfDeclaredMadeForKids
177 def update_thumbnail(self, audiobook):
178 thumbnail = self.prepare_thumbnail(audiobook)
179 response = youtube_call(
181 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
182 params={'videoId': audiobook.youtube_id},
183 data=thumbnail.getvalue(),
186 def prepare_thumbnail(self, audiobook):
187 img = create_thumbnail(
188 self.thumbnail_template.path,
189 self.thumbnail_definition,
191 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
192 "title": audiobook.book['title'],
194 lambda name: Font.objects.get(name=name).truetype.path
197 img.save(buf, format='PNG')
201 class Card(models.Model):
202 youtube = models.ForeignKey(YouTube, models.CASCADE)
203 order = models.SmallIntegerField()
204 image = models.FileField(upload_to='youtube/card')
205 duration = models.FloatField()
208 ordering = ('order', )
211 class Font(models.Model):
212 name = models.CharField(max_length=255, unique=True)
213 truetype = models.FileField(upload_to='youtube/font')