Skip to content

Commit 32972f7

Browse files
committed
Add thumbnail conversion functionality and improve download path handling
1 parent 2de3671 commit 32972f7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

bot_package/handlers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
active_downloads,
2121
)
2222
from .helpers import (
23+
convert_thumbnail_to_jpeg,
2324
count_bot_users,
2425
count_sudo_users,
2526
download_thumbnail_async,
@@ -693,9 +694,18 @@ async def upload_progress(current, total):
693694
)
694695

695696
title, thumbnail_url = await get_video_info(url) # Get thumbnail URL again
696-
print("checking thumbnail status")
697+
print("Checking thumbnail status...")
697698
thumbnail_status, thumbnail = await download_thumbnail_async(url=thumbnail_url, local_path=f"{filepath}.jpg", user_id=user_id)
699+
700+
if not thumbnail_status:
701+
pass
702+
else:
703+
print("Please wait. Converting thumbnail...")
704+
convert_thumbnail_to_jpeg(thumbnail, thumbnail)
705+
706+
# await asyncio.sleep(3)
698707

708+
print("Uploading...")
699709
send = await client.send_video(
700710
chat_id=chat_id,
701711
video=filepath,

bot_package/helpers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ async def download_thumbnail_async(url: str = None, local_path: str = None, user
238238
if not os.path.exists(work_dir):
239239
os.makedirs(work_dir, exist_ok=True)
240240
os.chdir(work_dir) # Change to the user's download directory
241-
local_path = os.path.join(work_dir, local_path)
241+
local_path = os.path.join(work_dir, local_path.replace(" ", "_"))
242242
except Exception as e:
243243
print(f"Error creating directory: {e}")
244244
pass
@@ -257,3 +257,16 @@ async def download_thumbnail_async(url: str = None, local_path: str = None, user
257257
except Exception as e:
258258
print(f"Error downloading thumbnail: {e}")
259259
return (False, None)
260+
261+
262+
# convert the downloaded thumbnail to a Jpeg image with width and height not more than 320px320px using ffmpeg
263+
def convert_thumbnail_to_jpeg(input_path: str, output_path: str) -> bool:
264+
"""Converts a thumbnail to JPEG format with a maximum size of 320x320."""
265+
try:
266+
# Use ffmpeg to convert and resize the image
267+
os.system(f"ffmpeg -i {input_path} -vf scale=320:320 -q:v 2 {output_path} -y")
268+
print(f"Thumbnail converted successfully: {output_path}")
269+
return True
270+
except Exception as e:
271+
print(f"Error converting thumbnail: {e}")
272+
return False

0 commit comments

Comments
 (0)