From 6975b817d0aff6a71aba8caa6fe4a0780687f646 Mon Sep 17 00:00:00 2001 From: "Nikita Tyukalov, ASUS, Linux" Date: Mon, 24 Nov 2025 01:10:20 +0300 Subject: [PATCH] Audio update - changed thumbnail size for audio to 1000x1000 at most - .m4a and .aac now get thumbnail integrated into them - ability to set custom thumbnail for audio - changed audio caption --- mod_video_downloader.py | 78 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/mod_video_downloader.py b/mod_video_downloader.py index 84edd53..a0dd123 100644 --- a/mod_video_downloader.py +++ b/mod_video_downloader.py @@ -294,7 +294,7 @@ def _download_thumb_for_tg_raw(url: str, proxy: bool, thumb: str) -> str | None: '-i', url, '-vf', - 'thumbnail,scale=\'min(280,iw)\':\'min(280,ih)\':force_original_aspect_ratio=decrease', + 'thumbnail,scale=\'min(1000,iw)\':\'min(1000,ih)\':force_original_aspect_ratio=decrease', thumb ] if proxy: @@ -330,6 +330,38 @@ async def _download_thumb_for_tg(url: str, proxy: bool, thumb: str) -> str | Non except: return traceback.format_exc() +def _execute_args_check_file_raw(args: list[str], file_to_check: str) -> None | str: + ''' Downloads a thumbnail for Telegram ''' + try: + # start the process + cp = subprocess.run( + args, + capture_output=True, + timeout=30 + ) + # file exists? + if os.path.isfile(file_to_check): + return None + # check the result + txt = None + try: + txt = cp.stderr.decode(encoding='ascii').strip() + return txt + except: + pass + except subprocess.TimeoutExpired: + return 'Command has timed out' + except: + return traceback.format_exc() + return 'Failed to execute command' + +async def _execute_args_check_file(args: list[str], file_to_check: str) -> str | None: + ''' Async version ''' + try: + return await asyncio.to_thread(_execute_args_check_file_raw, args, file_to_check) + except: + return traceback.format_exc() + async def mod_init(config: dict) -> bool: ''' Initialize the mod ''' global _config @@ -386,7 +418,7 @@ async def mod_new_message(session, event) -> None: response_text = 'mod_audio_video_downloader:' response_text += '\n- mvdl[p] [URL] - get list of all video qualities' response_text += '\n- madl[p] [URL] - get list of all audio qualities' - response_text += '\n- mdd[p] [CODE] [TITLE] [§ PERFORMER] - download video or audio (for audio only: track title)' + response_text += '\n- mdd[p] [CODE] [TITLE] [§ PERFORMER] [§ ALBUM ART URL] - download video or audio (for audio only: track title)' response_text += '\n\nUse \'p\' letter to utilize proxy' await event.reply(message=response_text) # list video qualities @@ -489,13 +521,15 @@ async def mod_new_message(session, event) -> None: # assign track title remains = [i.strip() for i in ' '.join(args[1:]).split('§') if i.strip()] title = data['title'] - author = data['author'] + author = 'tg-utility & %s' % data['author'] thumbnail = data['thumbnail'] # use title and author from user message if remains: title = remains[0] - if len(remains) > 1: - author = remains[-1] + if len(remains) >= 2: + author = remains[1] + if len(remains) >= 3: + thumbnail = remains[2] # thumbnail exists, download it and change scale if thumbnail: thumb_path = 'mvd_temp/%s.jpg' % code @@ -505,13 +539,45 @@ async def mod_new_message(session, event) -> None: thumbnail = None else: thumbnail = thumb_path + # thumbnail really exists, apply it to track + if thumbnail: + new_file_args = None + very_new_file = None + if data['ext'] in ('m4a', 'aac'): + very_new_file = 'mvd_temp/%s.covered.%s' % (code, data['ext']) + new_file_args = [ + utils.which('ffmpeg'), + '-i', + new_name, + '-i', + thumbnail, + '-map', + '0', + '-map', + '1', + '-c', + 'copy', + '-disposition:v', + 'attached_pic', + very_new_file + ] + # CLI arguments are present + if new_file_args: + # try to execute + res = await _execute_args_check_file(new_file_args, very_new_file) + # check if success + if not res: + new_name = very_new_file + # failure + else: + await event.reply(message='Couldn\'t set album art cover. Won\'t be beautiful, but will still play.\n\n%s' % res) # send file await event.reply(message='Audio is downloaded, uploading it to Telegram...') try: await event.client.send_file( entity=peer, file=new_name, - caption='%s' % data['url'], + caption='This track is downloaded using tg-utility\n%s' % data['url'], mime_type=utils.get_mime(data['ext']), file_size=video_data['size'], thumb=thumbnail,