Spaces:
Paused
Paused
Update DragMusic/utils/thumbnails.py
Browse files
DragMusic/utils/thumbnails.py
CHANGED
@@ -151,6 +151,10 @@ async def gen_thumb(videoid: str):
|
|
151 |
channel = "Unknown Channel"
|
152 |
|
153 |
|
|
|
|
|
|
|
|
|
154 |
async with aiohttp.ClientSession() as session:
|
155 |
async with session.get(thumbnail) as resp:
|
156 |
if resp.status == 200:
|
@@ -168,10 +172,22 @@ async def gen_thumb(videoid: str):
|
|
168 |
await f.write(await resp.read())
|
169 |
await f.close()
|
170 |
# os.system(f"file {filepath}")
|
|
|
|
|
|
|
171 |
|
172 |
-
|
173 |
image_path = f"{cache_dir}/thumb{videoid}.png"
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
image1 = changeImageSize(1280, 720, youtube)
|
176 |
|
177 |
image2 = image1.convert("RGBA")
|
@@ -186,9 +202,29 @@ async def gen_thumb(videoid: str):
|
|
186 |
background = Image.blend(background, gradient_image, alpha=0.2)
|
187 |
|
188 |
draw = ImageDraw.Draw(background)
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
|
194 |
circle_thumbnail = crop_center_circle(youtube, 400, 20, start_gradient_color)
|
@@ -238,9 +274,17 @@ async def gen_thumb(videoid: str):
|
|
238 |
draw_text_with_shadow(background, draw, (text_x_position, 400), "00:00", arial, (255, 255, 255))
|
239 |
draw_text_with_shadow(background, draw, (1080, 400), duration, arial, (255, 255, 255))
|
240 |
|
241 |
-
|
242 |
-
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
|
245 |
os.remove(f"{cache_dir}/thumb{videoid}.png")
|
246 |
|
|
|
151 |
channel = "Unknown Channel"
|
152 |
|
153 |
|
154 |
+
if not thumbnail:
|
155 |
+
logging.error(f"No thumbnail URL found for video {videoid}")
|
156 |
+
return None
|
157 |
+
|
158 |
async with aiohttp.ClientSession() as session:
|
159 |
async with session.get(thumbnail) as resp:
|
160 |
if resp.status == 200:
|
|
|
172 |
await f.write(await resp.read())
|
173 |
await f.close()
|
174 |
# os.system(f"file {filepath}")
|
175 |
+
else:
|
176 |
+
logging.error(f"Failed to download thumbnail: HTTP {resp.status}")
|
177 |
+
return None
|
178 |
|
|
|
179 |
image_path = f"{cache_dir}/thumb{videoid}.png"
|
180 |
+
|
181 |
+
# Verify the downloaded file exists and has content
|
182 |
+
if not os.path.exists(image_path) or os.path.getsize(image_path) == 0:
|
183 |
+
logging.error(f"Thumbnail file not found or empty: {image_path}")
|
184 |
+
return None
|
185 |
+
|
186 |
+
try:
|
187 |
+
youtube = Image.open(image_path)
|
188 |
+
except Exception as img_error:
|
189 |
+
logging.error(f"Failed to open thumbnail image: {img_error}")
|
190 |
+
return None
|
191 |
image1 = changeImageSize(1280, 720, youtube)
|
192 |
|
193 |
image2 = image1.convert("RGBA")
|
|
|
202 |
background = Image.blend(background, gradient_image, alpha=0.2)
|
203 |
|
204 |
draw = ImageDraw.Draw(background)
|
205 |
+
|
206 |
+
# Load fonts with error handling and fallbacks
|
207 |
+
try:
|
208 |
+
# Try to use absolute paths first
|
209 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
210 |
+
assets_dir = os.path.join(current_dir, "..", "assets")
|
211 |
+
|
212 |
+
arial = ImageFont.truetype(os.path.join(assets_dir, "font2.ttf"), 30)
|
213 |
+
font = ImageFont.truetype(os.path.join(assets_dir, "font.ttf"), 30)
|
214 |
+
title_font = ImageFont.truetype(os.path.join(assets_dir, "font3.ttf"), 45)
|
215 |
+
except Exception as font_error:
|
216 |
+
logging.warning(f"Failed to load custom fonts: {font_error}, using default fonts")
|
217 |
+
try:
|
218 |
+
# Try relative paths as fallback
|
219 |
+
arial = ImageFont.truetype("DragMusic/assets/font2.ttf", 30)
|
220 |
+
font = ImageFont.truetype("DragMusic/assets/font.ttf", 30)
|
221 |
+
title_font = ImageFont.truetype("DragMusic/assets/font3.ttf", 45)
|
222 |
+
except Exception as fallback_error:
|
223 |
+
logging.warning(f"Failed to load fonts with relative paths: {fallback_error}, using system defaults")
|
224 |
+
# Use system default fonts as last resort
|
225 |
+
arial = ImageFont.load_default()
|
226 |
+
font = ImageFont.load_default()
|
227 |
+
title_font = ImageFont.load_default()
|
228 |
|
229 |
|
230 |
circle_thumbnail = crop_center_circle(youtube, 400, 20, start_gradient_color)
|
|
|
274 |
draw_text_with_shadow(background, draw, (text_x_position, 400), "00:00", arial, (255, 255, 255))
|
275 |
draw_text_with_shadow(background, draw, (1080, 400), duration, arial, (255, 255, 255))
|
276 |
|
277 |
+
# Load play icons with error handling
|
278 |
+
try:
|
279 |
+
if 'assets_dir' in locals():
|
280 |
+
play_icons = Image.open(os.path.join(assets_dir, "play_icons.png"))
|
281 |
+
else:
|
282 |
+
play_icons = Image.open("DragMusic/assets/play_icons.png")
|
283 |
+
play_icons = play_icons.resize((580, 62))
|
284 |
+
background.paste(play_icons, (text_x_position, 450), play_icons)
|
285 |
+
except Exception as icon_error:
|
286 |
+
logging.warning(f"Failed to load play icons: {icon_error}, skipping icon")
|
287 |
+
# Continue without the play icons if they can't be loaded
|
288 |
|
289 |
os.remove(f"{cache_dir}/thumb{videoid}.png")
|
290 |
|