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
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) # get categories
26 intro_flac = models.FileField(upload_to='youtube/intro_flac', blank=True)
27 outro_flac = models.FileField(upload_to='youtube/outro_flac', blank=True)
28 loop_card = models.FileField(upload_to='youtube/card', blank=True)
29 loop_video = models.FileField(upload_to='youtube/loop_video', blank=True)
30 thumbnail_template = models.FileField(upload_to='youtube/thumbnail', blank=True)
31 thumbnail_definition = models.TextField(blank=True)
32 genres = models.CharField(max_length=2048, blank=True)
35 verbose_name = _("YouTube configuration")
36 verbose_name_plural = _("YouTube configurations")
38 def publish(self, audiobook, path):
39 ctx = Context(dict(audiobook=audiobook))
40 description = Template(self.description_template).render(ctx)
41 title = Template(self.title_template).render(ctx)
47 description=description,
49 # categoryId=category,
53 privacyStatus=privacy,
55 # selfDeclaredMadeForKids
57 # recordingDetails=dict(
61 part = ",".join(data.keys())
63 with open(path, "rb") as f:
64 response = youtube_call(
66 "https://www.googleapis.com/upload/youtube/v3/videos",
67 params={'part': part},
69 resumable_data=f.read(),
71 data = response.json()
72 audiobook.youtube_id = data['id']
73 audiobook.save(update_fields=['youtube_id'])
75 self.update_thumbnail(audiobook)
78 def prepare_file(self, input_path, output_path=None):
79 audio = self.prepare_audio(input_path)
80 duration = self.get_duration(input_path)
81 video = self.prepare_video(duration)
82 output = mux([video, audio], output_path=output_path)
87 def get_duration(self, input_path):
88 d = get_duration(input_path)
90 d += get_duration(self.intro_flac.path)
92 d += get_duration(self.outro_flac.path)
95 def prepare_audio(self, input_path):
98 files.append(self.intro_flac.path)
99 files.append(input_path)
101 files.append(self.outro_flac.path)
102 return concat_audio(files)
104 def prepare_video(self, duration):
110 fps = get_framerate(self.loop_video.path)
111 loop_video = standardize_video(self.loop_video.path)
115 loop_duration = duration
116 for card in self.card_set.filter(duration__gt=0):
117 loop_duration -= card.duration
118 card_video = video_from_image(
119 card.image.path, card.duration, fps=fps
121 (concat if card.order < 0 else outro).append(card_video)
122 delete.append(card_video)
125 loop_video_duration = get_duration(loop_video)
126 times_loop = int(loop_duration // loop_video_duration)
128 leftover_duration = loop_duration % loop_video_duration
129 leftover = cut_video(loop_video, leftover_duration)
130 concat.extend([loop_video] * times_loop + [leftover])
131 delete.append(leftover)
133 leftover = video_from_image(self.loop_card.path, loop_duration)
134 concat.append(video_from_image(self.loop_card.path, loop_duration, fps=fps))
135 delete.append(leftover)
138 output = concat_videos(concat)
146 # selfDeclaredMadeForKids
148 def update_thumbnail(self, audiobook):
149 thumbnail = self.prepare_thumbnail(audiobook)
150 response = youtube_call(
152 "https://www.googleapis.com/upload/youtube/v3/thumbnails/set",
153 params={'videoId': audiobook.youtube_id},
154 data=thumbnail.getvalue(),
157 def prepare_thumbnail(self, audiobook):
158 slug = audiobook.url.rstrip('/').rsplit('/', 1)[-1]
159 apidata = requests.get(f'https://wolnelektury.pl/api/books/{slug}/').json()
160 img = create_thumbnail(
161 self.thumbnail_template.path,
162 self.thumbnail_definition,
164 "author": ', '.join((a['name'] for a in apidata['authors'])),
165 "title": apidata['title'],
167 lambda name: Font.objects.get(name=name).truetype.path
170 img.save(buf, format='PNG')
174 class Card(models.Model):
175 youtube = models.ForeignKey(YouTube, models.CASCADE)
176 order = models.SmallIntegerField()
177 image = models.FileField(upload_to='youtube/card')
178 duration = models.FloatField()
181 ordering = ('order', )
184 class Font(models.Model):
185 name = models.CharField(max_length=255, unique=True)
186 truetype = models.FileField(upload_to='youtube/font')