Skip to content

Commit

Permalink
fix InputMediaVideo thumbnail (use FSInputFile instead of URLInputFil…
Browse files Browse the repository at this point in the history
…e) (#5)
  • Loading branch information
magicxor authored Nov 17, 2024
1 parent 5638d85 commit 73a20ed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ requests = "^2.32.3"
curl-cffi = "^0.7.3"
pycryptodome = "^3.21.0"
aiogram = "^3.14.0"
aiohttp = "^3.10"

[tool.poetry.group.dev.dependencies]
mypy = "^1.13.0"
Expand Down
77 changes: 52 additions & 25 deletions ytdl_inline_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
InputMediaPhoto,
InlineKeyboardButton,
InlineKeyboardMarkup,
FSInputFile, URLInputFile,
FSInputFile,
)
from yt_dlp import YoutubeDL
import aiohttp

# Enable logging
logging.basicConfig(
Expand Down Expand Up @@ -309,31 +310,38 @@ async def download_video_and_replace(url: str, inline_message_id: str, user_id:

logger.info(f"Video uploaded. Preparing thumbnail for the video.")

video_id = extract_youtube_video_id(url)
if video_id:
thumbnail_url = get_youtube_thumbnail_url(video_id, 'mqdefault')
thumbnail_file = URLInputFile(url=thumbnail_url)
else:
thumbnail_url = None
thumbnail_file = None

logger.info(f"Replacing the placeholder with the video {video_msg.video.file_id} and thumbnail {thumbnail_url}")

await retry_operation(
bot.edit_message_media,
max_retries=2,
delay=1,
inline_message_id=inline_message_id,
media=InputMediaVideo(
media=video_msg.video.file_id,
caption=(metadata.title + " " + url),
thumbnail=thumbnail_file,
width=metadata.width,
height=metadata.height,
duration=metadata.duration,
supports_streaming=True
thumbnail_filename = None # Initialize thumbnail filename
try:
video_id = extract_youtube_video_id(url)
if video_id:
thumbnail_url = get_youtube_thumbnail_url(video_id, 'mqdefault')
thumbnail_filename = await download_file(thumbnail_url) # Download the thumbnail
thumbnail_file = FSInputFile(thumbnail_filename)
else:
thumbnail_url = None
thumbnail_file = None

logger.info(f"Replacing the placeholder with the video {video_msg.video.file_id} and thumbnail {thumbnail_url}")

await retry_operation(
bot.edit_message_media,
max_retries=2,
delay=1,
inline_message_id=inline_message_id,
media=InputMediaVideo(
media=video_msg.video.file_id,
caption=(metadata.title + " " + url),
thumbnail=thumbnail_file,
width=metadata.width,
height=metadata.height,
duration=metadata.duration,
supports_streaming=True
)
)
)
finally:
# Delete the thumbnail file from disk
if thumbnail_filename and os.path.exists(thumbnail_filename):
os.remove(thumbnail_filename)

# Update rate limit timestamp for non-VIP users only on successful download
if user_id != VIP_USER_ID:
Expand Down Expand Up @@ -362,6 +370,25 @@ async def download_video_and_replace(url: str, inline_message_id: str, user_id:
media=InputMediaVideo(media=ERR_LOADING_VIDEO_URL, caption="Failed to replace the placeholder video.", width=ERR_VIDEO_WIDTH, height=ERR_VIDEO_HEIGHT, duration=ERR_VIDEO_DURATION, supports_streaming=False)
)

async def download_file(url: str) -> str:
"""
Downloads a file from a URL to a local file with a random filename.
Returns the filename.
"""
filename = f"thumb_{uuid.uuid4().hex}.jpg" # Added .jpg extension
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
with open(filename, 'wb') as f:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
f.write(chunk)
else:
raise Exception(f"Failed to download file from {url}, status code {response.status}")
return filename

async def async_download_video(ydl_opts: Dict[str, Any], url: str) -> None:
"""Asynchronously downloads a video using yt-dlp."""
loop = asyncio.get_event_loop()
Expand Down

0 comments on commit 73a20ed

Please sign in to comment.