Spaces:
Paused
Paused
Update DragMusic/utils/thumbnails.py
Browse files- DragMusic/utils/thumbnails.py +17 -38
DragMusic/utils/thumbnails.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
import logging
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
import aiofiles
|
| 6 |
import aiohttp
|
| 7 |
-
import traceback
|
| 8 |
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
|
| 9 |
from youtubesearchpython.__future__ import VideosSearch
|
| 10 |
-
import tempfile
|
| 11 |
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
|
|
@@ -108,9 +109,8 @@ def draw_text_with_shadow(background, draw, position, text, font, fill, shadow_o
|
|
| 108 |
|
| 109 |
async def gen_thumb(videoid: str):
|
| 110 |
try:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
return os.path.join(temp_dir, f"{videoid}_v4.png")
|
| 114 |
|
| 115 |
url = f"https://www.youtube.com/watch?v={videoid}"
|
| 116 |
results = VideosSearch(url, limit=1)
|
|
@@ -144,12 +144,10 @@ async def gen_thumb(videoid: str):
|
|
| 144 |
channel = "Unknown Channel"
|
| 145 |
|
| 146 |
|
| 147 |
-
if not thumbnail:
|
| 148 |
-
logging.error(f"No thumbnail URL found for video {videoid}")
|
| 149 |
-
return None
|
| 150 |
-
|
| 151 |
async with aiohttp.ClientSession() as session:
|
| 152 |
async with session.get(thumbnail) as resp:
|
|
|
|
|
|
|
| 153 |
if resp.status == 200:
|
| 154 |
content_type = resp.headers.get('Content-Type')
|
| 155 |
if 'jpeg' in content_type or 'jpg' in content_type:
|
|
@@ -160,26 +158,16 @@ async def gen_thumb(videoid: str):
|
|
| 160 |
logging.error(f"Unexpected content type: {content_type}")
|
| 161 |
return None
|
| 162 |
|
| 163 |
-
filepath =
|
| 164 |
f = await aiofiles.open(filepath, mode="wb")
|
| 165 |
await f.write(await resp.read())
|
| 166 |
await f.close()
|
| 167 |
# os.system(f"file {filepath}")
|
| 168 |
|
| 169 |
|
| 170 |
-
image_path =
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
if not os.path.exists(image_path) or os.path.getsize(image_path) == 0:
|
| 174 |
-
logging.error(f"Thumbnail file not found or empty: {image_path}")
|
| 175 |
-
return None
|
| 176 |
-
|
| 177 |
-
try:
|
| 178 |
-
youtube = Image.open(image_path)
|
| 179 |
-
image1 = changeImageSize(1280, 720, youtube)
|
| 180 |
-
except Exception as e:
|
| 181 |
-
logging.error(f"Error opening or processing thumbnail image: {e}")
|
| 182 |
-
return None
|
| 183 |
|
| 184 |
image2 = image1.convert("RGBA")
|
| 185 |
background = image2.filter(filter=ImageFilter.BoxBlur(20))
|
|
@@ -193,18 +181,9 @@ async def gen_thumb(videoid: str):
|
|
| 193 |
background = Image.blend(background, gradient_image, alpha=0.2)
|
| 194 |
|
| 195 |
draw = ImageDraw.Draw(background)
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
arial = ImageFont.truetype("DragMusic/assets/font2.ttf", 30)
|
| 200 |
-
font = ImageFont.truetype("DragMusic/assets/font.ttf", 30)
|
| 201 |
-
title_font = ImageFont.truetype("DragMusic/assets/font3.ttf", 45)
|
| 202 |
-
except Exception as e:
|
| 203 |
-
logging.error(f"Error loading fonts: {e}")
|
| 204 |
-
# Use default font as fallback
|
| 205 |
-
arial = ImageFont.load_default()
|
| 206 |
-
font = ImageFont.load_default()
|
| 207 |
-
title_font = ImageFont.load_default()
|
| 208 |
|
| 209 |
|
| 210 |
circle_thumbnail = crop_center_circle(youtube, 400, 20, start_gradient_color)
|
|
@@ -254,13 +233,13 @@ async def gen_thumb(videoid: str):
|
|
| 254 |
draw_text_with_shadow(background, draw, (text_x_position, 400), "00:00", arial, (255, 255, 255))
|
| 255 |
draw_text_with_shadow(background, draw, (1080, 400), duration, arial, (255, 255, 255))
|
| 256 |
|
| 257 |
-
play_icons = Image.open("
|
| 258 |
play_icons = play_icons.resize((580, 62))
|
| 259 |
background.paste(play_icons, (text_x_position, 450), play_icons)
|
| 260 |
|
| 261 |
-
os.remove(
|
| 262 |
|
| 263 |
-
background_path =
|
| 264 |
background.save(background_path)
|
| 265 |
|
| 266 |
return background_path
|
|
|
|
| 1 |
+
# ATLEAST GIVE CREDITS IF YOU STEALING :(((((((((((((((((((((((((((((((((((((
|
| 2 |
+
# ELSE NO FURTHER PUBLIC THUMBNAIL UPDATES
|
| 3 |
+
|
| 4 |
import random
|
| 5 |
import logging
|
| 6 |
import os
|
| 7 |
import re
|
| 8 |
import aiofiles
|
| 9 |
import aiohttp
|
|
|
|
| 10 |
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
|
| 11 |
from youtubesearchpython.__future__ import VideosSearch
|
|
|
|
| 12 |
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
|
|
|
|
| 109 |
|
| 110 |
async def gen_thumb(videoid: str):
|
| 111 |
try:
|
| 112 |
+
if os.path.isfile(f"cache/{videoid}_v4.png"):
|
| 113 |
+
return f"cache/{videoid}_v4.png"
|
|
|
|
| 114 |
|
| 115 |
url = f"https://www.youtube.com/watch?v={videoid}"
|
| 116 |
results = VideosSearch(url, limit=1)
|
|
|
|
| 144 |
channel = "Unknown Channel"
|
| 145 |
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
async with aiohttp.ClientSession() as session:
|
| 148 |
async with session.get(thumbnail) as resp:
|
| 149 |
+
|
| 150 |
+
content = await resp.read()
|
| 151 |
if resp.status == 200:
|
| 152 |
content_type = resp.headers.get('Content-Type')
|
| 153 |
if 'jpeg' in content_type or 'jpg' in content_type:
|
|
|
|
| 158 |
logging.error(f"Unexpected content type: {content_type}")
|
| 159 |
return None
|
| 160 |
|
| 161 |
+
filepath = f"cache/thumb{videoid}.png"
|
| 162 |
f = await aiofiles.open(filepath, mode="wb")
|
| 163 |
await f.write(await resp.read())
|
| 164 |
await f.close()
|
| 165 |
# os.system(f"file {filepath}")
|
| 166 |
|
| 167 |
|
| 168 |
+
image_path = f"cache/thumb{videoid}.png"
|
| 169 |
+
youtube = Image.open(image_path)
|
| 170 |
+
image1 = changeImageSize(1280, 720, youtube)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
image2 = image1.convert("RGBA")
|
| 173 |
background = image2.filter(filter=ImageFilter.BoxBlur(20))
|
|
|
|
| 181 |
background = Image.blend(background, gradient_image, alpha=0.2)
|
| 182 |
|
| 183 |
draw = ImageDraw.Draw(background)
|
| 184 |
+
arial = ImageFont.truetype("AviaxMusic/assets/font2.ttf", 30)
|
| 185 |
+
font = ImageFont.truetype("AviaxMusic/assets/font.ttf", 30)
|
| 186 |
+
title_font = ImageFont.truetype("AviaxMusic/assets/font3.ttf", 45)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
circle_thumbnail = crop_center_circle(youtube, 400, 20, start_gradient_color)
|
|
|
|
| 233 |
draw_text_with_shadow(background, draw, (text_x_position, 400), "00:00", arial, (255, 255, 255))
|
| 234 |
draw_text_with_shadow(background, draw, (1080, 400), duration, arial, (255, 255, 255))
|
| 235 |
|
| 236 |
+
play_icons = Image.open("AviaxMusic/assets/play_icons.png")
|
| 237 |
play_icons = play_icons.resize((580, 62))
|
| 238 |
background.paste(play_icons, (text_x_position, 450), play_icons)
|
| 239 |
|
| 240 |
+
os.remove(f"cache/thumb{videoid}.png")
|
| 241 |
|
| 242 |
+
background_path = f"cache/{videoid}_v4.png"
|
| 243 |
background.save(background_path)
|
| 244 |
|
| 245 |
return background_path
|