3 from tempfile import NamedTemporaryFile
4 from django.conf import settings
5 from django.db import models
6 from django.utils.translation import gettext_lazy as _
7 from django.template import Template, Context
8 from apiclient import youtube_call
19 from .thumbnail import create_thumbnail
22 class YouTube(models.Model):
23 title_template = models.CharField(max_length=1024, blank=True)
24 description_template = models.TextField(blank=True)
25 category = models.IntegerField(null=True, blank=True, choices=[
28 intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
29 outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
30 loop_card = models.FileField(upload_to='youtube/card', blank=True)
31 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
32 thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
33 thumbnail_definition = models.TextField(blank=True)
34 privacy_status = models.CharField(max_length=16, choices=[
35 ('public', _('public')),
36 ('unlisted', _('unlisted')),
37 ('private', _('private')),
39 genres = models.CharField(max_length=2048, blank=True)
42 verbose_name = _("YouTube configuration")
43 verbose_name_plural = _("YouTube configurations")
45 def get_context(self, audiobook):
48 LICENSE=settings.LICENSE,
49 LICENSE_NAME=settings.LICENSE_NAME,
52 def get_description(self, audiobook):
53 return Template(self.description_template).render(self.get_context(audiobook))
55 def get_title(self, audiobook):
56 return Template(self.title_template).render(self.get_context(audiobook))
58 def get_data(self, audiobook):
61 title=self.get_title(audiobook),
62 description=self.get_description(audiobook),
63 categoryId=self.category,
65 defaultAudioLanguage='pl',
68 privacyStatus=self.privacy_status,
72 def publish(self, audiobook, path):
73 data = self.get_data(audiobook)
74 part = ",".join(data.keys())
76 with open(path, "rb") as f:
77 response = youtube_call(
79 "https://www.googleapis.com/upload/youtube/v3/videos",
80 params={'part': part},
82 resumable_data=f.read(),
84 data = response.json()
85 audiobook.youtube_id = data['id']
86 audiobook.save(update_fields=['youtube_id'])
88 self.update_thumbnail(audiobook)
91 def update_data(self, audiobook):
92 data = self.get_data(audiobook)
93 data['id'] = audiobook.youtube_id
94 part = ",".join(data.keys())
97 "https://www.googleapis.com/youtube/v3/videos",
98 params={"part": part},
102 def prepare_file(self, input_path, output_path=None):
103 audio = self.prepare_audio(input_path)
104 duration = self.get_duration(input_path)
105 video = self.prepare_video(duration)
106 output = mux([video, audio], output_path=output_path)
111 def get_duration(self, input_path):
112 d = get_duration(input_path)
114 d += get_duration(self.intro_flac.path)
116 d += get_duration(self.outro_flac.path)
119 def prepare_audio(self, input_path):
122 files.append(self.intro_flac.path)
123 files.append(input_path)
125 files.append(self.outro_flac.path)
126 return concat_audio(files)
128 def prepare_video(self, duration):
134 fps = get_framerate(self.loop_video.path)
135 loop_video = standardize_video(self.loop_video.path)
139 loop_duration = duration
140 for card in self.card_set.filter(duration__gt=0):
141 loop_duration -= card.duration
142 card_video = video_from_image(
143 card.image.path, card.duration, fps=fps
145 (concat if card.order < 0 else outro).append(card_video)
146 delete.append(card_video)
149 loop_video_duration = get_duration(loop_video)
150 times_loop = int(loop_duration // loop_video_duration)
152 leftover_duration = loop_duration % loop_video_duration
153 leftover = cut_video(loop_video, leftover_duration)
154 concat.extend([loop_video] * times_loop + [leftover])
155 delete.append(leftover)
157 leftover = video_from_image(self.loop_card.path, loop_duration)
158 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
159 delete.append(leftover)
162 output = concat_videos(concat)
170 # selfDeclaredMadeForKids
172 def update_thumbnail(self, audiobook):
173 thumbnail = self.prepare_thumbnail(audiobook)
174 response = youtube_call(
176 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
177 params={'videoId': audiobook.youtube_id},
178 data=thumbnail.getvalue(),
181 def prepare_thumbnail(self, audiobook):
182 img = create_thumbnail(
183 self.thumbnail_template.path,
184 self.thumbnail_definition,
186 "author": ', '.join((a['name'] for a in audiobook.book['authors'])),
187 "title": audiobook.book['title'],
189 lambda name: Font.objects.get(name=name).truetype.path
192 img.save(buf, format='PNG')
196 class Card(models.Model):
197 youtube = models.ForeignKey(YouTube, models.CASCADE)
198 order = models.SmallIntegerField()
199 image = models.FileField(upload_to='youtube/card')
200 duration = models.FloatField()
203 ordering = ('order', )
206 class Font(models.Model):
207 name = models.CharField(max_length=255, unique=True)
208 truetype = models.FileField(upload_to='youtube/font')