From a07d789e66bacff04586e9c030c7216d57745a23 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 8 Apr 2022 00:29:09 +0700 Subject: [PATCH] add base64 blur video thumb --- tubearchivist/home/src/download/thumbnails.py | 19 ++++++++++++++++++- tubearchivist/home/src/es/index_mapping.json | 4 ++++ tubearchivist/home/src/index/video.py | 3 +++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tubearchivist/home/src/download/thumbnails.py b/tubearchivist/home/src/download/thumbnails.py index 0ce492f..d25f4d1 100644 --- a/tubearchivist/home/src/download/thumbnails.py +++ b/tubearchivist/home/src/download/thumbnails.py @@ -4,8 +4,10 @@ functionality: - check for missing thumbnails """ +import base64 import os from collections import Counter +from io import BytesIO from time import sleep import requests @@ -15,7 +17,7 @@ from home.src.ta.config import AppConfig from home.src.ta.helper import ignore_filelist from home.src.ta.ta_redis import RedisArchivist from mutagen.mp4 import MP4, MP4Cover -from PIL import Image +from PIL import Image, ImageFilter class ThumbManager: @@ -241,6 +243,21 @@ class ThumbManager: } RedisArchivist().set_message("message:download", mess_dict) + def get_base64_blur(self, youtube_id): + """return base64 encoded placeholder""" + img_path = self.vid_thumb_path(youtube_id) + file_path = os.path.join(self.CACHE_DIR, img_path) + img_raw = Image.open(file_path) + img_raw.thumbnail((img_raw.width // 20, img_raw.height // 20)) + img_blur = img_raw.filter(ImageFilter.BLUR) + buffer = BytesIO() + img_blur.save(buffer, format="JPEG") + img_data = buffer.getvalue() + img_base64 = base64.b64encode(img_data).decode() + data_url = f"data:image/jpg;base64,{img_base64}" + + return data_url + @staticmethod def vid_thumb_path(youtube_id): """build expected path for video thumbnail from youtube_id""" diff --git a/tubearchivist/home/src/es/index_mapping.json b/tubearchivist/home/src/es/index_mapping.json index 29f6b7e..0fddc11 100644 --- a/tubearchivist/home/src/es/index_mapping.json +++ b/tubearchivist/home/src/es/index_mapping.json @@ -73,6 +73,10 @@ "type": "text", "index": false }, + "vid_thumb_base64": { + "type": "text", + "index": false + }, "date_downloaded": { "type": "date" }, diff --git a/tubearchivist/home/src/index/video.py b/tubearchivist/home/src/index/video.py index 411b4af..811059e 100644 --- a/tubearchivist/home/src/index/video.py +++ b/tubearchivist/home/src/index/video.py @@ -10,6 +10,7 @@ from datetime import datetime import requests from django.conf import settings +from home.src.download.thumbnails import ThumbManager from home.src.es.connect import ElasticWrap from home.src.index import channel as ta_channel from home.src.index.generic import YouTubeItem @@ -389,12 +390,14 @@ class YoutubeVideo(YouTubeItem, YoutubeSubtitle): upload_date_time = datetime.strptime(upload_date, "%Y%m%d") published = upload_date_time.strftime("%Y-%m-%d") last_refresh = int(datetime.now().strftime("%s")) + base64_blur = ThumbManager().get_base64_blur(self.youtube_id) # build json_data basics self.json_data = { "title": self.youtube_meta["title"], "description": self.youtube_meta["description"], "category": self.youtube_meta["categories"], "vid_thumb_url": self.youtube_meta["thumbnail"], + "vid_thumb_base64": base64_blur, "tags": self.youtube_meta["tags"], "published": published, "vid_last_refresh": last_refresh,