Skip to content

Commit

Permalink
feat: auto generate thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Gorn committed Jun 9, 2024
1 parent 67f0210 commit c531385
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Bot/functions/dl_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import time
import re
import random
from datetime import datetime
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
Expand All @@ -15,6 +16,7 @@
from pyrogram.types import CallbackQuery
from .display_progress import progress_for_pyrogram
from .download import download_coroutine
from .helper import run_cmd, ffmpeg_supported_video_mimetypes
from .. import client


Expand Down Expand Up @@ -122,6 +124,10 @@ async def ddl_call_back(bot: Client, update: CallbackQuery):
if metadata is not None:
if metadata.has("duration"):
duration = metadata.get('duration').seconds
# auto generate thumbnail if not available
if not os.path.exists(thumb_image_path):
if client.guess_mime_type(download_directory) in ffmpeg_supported_video_mimetypes:
await run_cmd('ffmpeg -ss {} -i "{}" -vframes 1 "{}"'.format(random.randint(0, duration), download_directory, thumb_image_path))
# get the correct width, height, and duration for videos greater than 10MB
if os.path.exists(thumb_image_path):
width = 0
Expand Down
18 changes: 18 additions & 0 deletions Bot/functions/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
import re
from typing import Tuple

ffmpeg_supported_video_mimetypes = [
'video/mp4',
'video/x-matroska',
'video/webm',
'video/mpeg',
'video/ogg',
'video/x-nut',
'video/MP2T',
'video/x-mjpeg',
'video/x-m4v',
'video/x-h261',
'video/x-h263',
'video/x-flv',
'video/x-msvideo',
'video/x-ms-asf',
'video/3gpp2',
'video/3gpp'
]

# Detect URLS using Regex. https://stackoverflow.com/a/3809435/15561455
URL_REGEX = re.compile(
Expand Down
10 changes: 7 additions & 3 deletions Bot/functions/youtube_dl_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@


import time
import shutil
import os
import json
import random
import re
import asyncio
from PIL import Image
Expand All @@ -18,6 +18,7 @@
from pyrogram.types import CallbackQuery
from .display_progress import progress_for_pyrogram, humanbytes
from .help_Nekmo_ffmpeg import generate_screen_shots
from .helper import run_cmd, ffmpeg_supported_video_mimetypes
from .. import client


Expand Down Expand Up @@ -203,6 +204,10 @@ async def youtube_dl_call_back(bot: Client, update: CallbackQuery):
if metadata is not None:
if metadata.has("duration"):
duration = metadata.get('duration').seconds
# auto generate thumbnail if not available
if not os.path.exists(thumb_image_path):
if client.guess_mime_type(download_directory) in ffmpeg_supported_video_mimetypes:
await run_cmd('ffmpeg -ss {} -i "{}" -vframes 1 "{}"'.format(random.randint(0, duration), download_directory, thumb_image_path))
# get the correct width, height, and duration for videos greater than 10MB
if os.path.exists(thumb_image_path):
width = 0
Expand All @@ -228,7 +233,6 @@ async def youtube_dl_call_back(bot: Client, update: CallbackQuery):
img.resize((90, height))
img.save(thumb_image_path, "JPEG")
# https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#create-thumbnails

else:
thumb_image_path = None
start_time = time.time()
Expand Down Expand Up @@ -307,7 +311,7 @@ async def youtube_dl_call_back(bot: Client, update: CallbackQuery):
end_two = datetime.now()
time_taken_for_upload = (end_two - end_one).seconds
media_album_p: list[InputMediaPhoto] = []
if images is not None:
if images:
i = 0
caption = "@xurluploaderbot"
for image in images:
Expand Down

0 comments on commit c531385

Please sign in to comment.