I’m trying to make a telegram bot with a pyrogram for downloading music from YouTube. its succes but im getting problem the audio thumbnail getting so blur. I tried this code to adjust the blur but still not reduce the blur.:
def resize_image_from_url(url, output_path, name):
try:
thumb = url
req = requests.get(thumb)
file = BytesIO(req.content)
img = Image.open(file)
print(f"Image size: {len(req.content)} bytes width: {img.width} height: {img.height}")
img.thumbnail((320, 320))
print(f"Resized image size: {img.size} bytes width: {img.width} height: {img.height}")
# Include the name in the output file path
output_file_path = os.path.join(output_path, f"{name}.png")
img.save(output_file_path, "PNG", optimize=True)
return output_file_path
except Exception as e:
print(f"Error processing image: {e}")
return None
And the yt downloading code
@app.on_callback_query(filters.create(lambda _, __, query: query.data.startswith("mytb")))
async def download_mp3(client, callback_query):
with ThreadPoolExecutor() as executor:
# Extract the URL from the callback data
await app.send_message(callback_query.message.chat.id, "Downloading Your Audio...")
video_id = callback_query.data.split('.', 1)[1]
url = f"https://www.youtube.com/watch?v={video_id}"
# Download the audio
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': f'{downdir}/%(title)s-%(uploader)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '320',
}],
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
title1 = info_dict['title']
author = info_dict['uploader']
duration = info_dict['duration']
thumbnail = info_dict['thumbnail']
title = f"{title1}-{author}"
mp3_path = os.path.join("bot/music/youtube", f"{title}.mp3")
# Prepare the thumbnail
song_name = title1.replace(" ", "_")
song_name = re.sub(r'[\\/*?:"<>|]', "", song_name) # remove invalid characters
thumbnail_name = f"{song_name}_thumbnail"
output_path = "bot/music/youtube"
thumbnail_file_path = resize_image_from_url(thumbnail, output_path, thumbnail_name)
# Send the audio file
await app.send_chat_action(callback_query.message.chat.id, enums.ChatAction.UPLOAD_AUDIO)
await app.send_audio(callback_query.message.chat.id, audio=mp3_path, title=title, performer=author, duration=duration, thumb=thumbnail_file_path)
executor.shutdown(wait=True)
await app.send_message(callback_query.message.chat.id, f'Audio downloaded')
# Delete the audio files
if os.path.exists(mp3_path):
os.remove(mp3_path)
If there my code have a problem? or telegram thumbnail audio is not supported with a size of 320 x 320, and telegram audio only uses blur for audio thumbnails?