add base64 blur video thumb

This commit is contained in:
simon 2022-04-08 00:29:09 +07:00
parent 6a1cb15114
commit a07d789e66
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 25 additions and 1 deletions

View File

@ -4,8 +4,10 @@ functionality:
- check for missing thumbnails - check for missing thumbnails
""" """
import base64
import os import os
from collections import Counter from collections import Counter
from io import BytesIO
from time import sleep from time import sleep
import requests 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.helper import ignore_filelist
from home.src.ta.ta_redis import RedisArchivist from home.src.ta.ta_redis import RedisArchivist
from mutagen.mp4 import MP4, MP4Cover from mutagen.mp4 import MP4, MP4Cover
from PIL import Image from PIL import Image, ImageFilter
class ThumbManager: class ThumbManager:
@ -241,6 +243,21 @@ class ThumbManager:
} }
RedisArchivist().set_message("message:download", mess_dict) 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 @staticmethod
def vid_thumb_path(youtube_id): def vid_thumb_path(youtube_id):
"""build expected path for video thumbnail from youtube_id""" """build expected path for video thumbnail from youtube_id"""

View File

@ -73,6 +73,10 @@
"type": "text", "type": "text",
"index": false "index": false
}, },
"vid_thumb_base64": {
"type": "text",
"index": false
},
"date_downloaded": { "date_downloaded": {
"type": "date" "type": "date"
}, },

View File

@ -10,6 +10,7 @@ from datetime import datetime
import requests import requests
from django.conf import settings from django.conf import settings
from home.src.download.thumbnails import ThumbManager
from home.src.es.connect import ElasticWrap from home.src.es.connect import ElasticWrap
from home.src.index import channel as ta_channel from home.src.index import channel as ta_channel
from home.src.index.generic import YouTubeItem 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") upload_date_time = datetime.strptime(upload_date, "%Y%m%d")
published = upload_date_time.strftime("%Y-%m-%d") published = upload_date_time.strftime("%Y-%m-%d")
last_refresh = int(datetime.now().strftime("%s")) last_refresh = int(datetime.now().strftime("%s"))
base64_blur = ThumbManager().get_base64_blur(self.youtube_id)
# build json_data basics # build json_data basics
self.json_data = { self.json_data = {
"title": self.youtube_meta["title"], "title": self.youtube_meta["title"],
"description": self.youtube_meta["description"], "description": self.youtube_meta["description"],
"category": self.youtube_meta["categories"], "category": self.youtube_meta["categories"],
"vid_thumb_url": self.youtube_meta["thumbnail"], "vid_thumb_url": self.youtube_meta["thumbnail"],
"vid_thumb_base64": base64_blur,
"tags": self.youtube_meta["tags"], "tags": self.youtube_meta["tags"],
"published": published, "published": published,
"vid_last_refresh": last_refresh, "vid_last_refresh": last_refresh,