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
This commit is contained in:
Nikita Tyukalov, ASUS, Linux
2025-11-24 01:10:20 +03:00
parent 0bcd47ab4c
commit 6975b817d0

View File

@@ -294,7 +294,7 @@ def _download_thumb_for_tg_raw(url: str, proxy: bool, thumb: str) -> str | None:
'-i', '-i',
url, url,
'-vf', '-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 thumb
] ]
if proxy: if proxy:
@@ -330,6 +330,38 @@ async def _download_thumb_for_tg(url: str, proxy: bool, thumb: str) -> str | Non
except: except:
return traceback.format_exc() 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: async def mod_init(config: dict) -> bool:
''' Initialize the mod ''' ''' Initialize the mod '''
global _config global _config
@@ -386,7 +418,7 @@ async def mod_new_message(session, event) -> None:
response_text = 'mod_audio_video_downloader:' response_text = 'mod_audio_video_downloader:'
response_text += '\n- mvdl[p] [URL] - get list of all video qualities' 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- 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' response_text += '\n\nUse \'p\' letter to utilize proxy'
await event.reply(message=response_text) await event.reply(message=response_text)
# list video qualities # list video qualities
@@ -489,13 +521,15 @@ async def mod_new_message(session, event) -> None:
# assign track title # assign track title
remains = [i.strip() for i in ' '.join(args[1:]).split('§') if i.strip()] remains = [i.strip() for i in ' '.join(args[1:]).split('§') if i.strip()]
title = data['title'] title = data['title']
author = data['author'] author = 'tg-utility & %s' % data['author']
thumbnail = data['thumbnail'] thumbnail = data['thumbnail']
# use title and author from user message # use title and author from user message
if remains: if remains:
title = remains[0] title = remains[0]
if len(remains) > 1: if len(remains) >= 2:
author = remains[-1] author = remains[1]
if len(remains) >= 3:
thumbnail = remains[2]
# thumbnail exists, download it and change scale # thumbnail exists, download it and change scale
if thumbnail: if thumbnail:
thumb_path = 'mvd_temp/%s.jpg' % code thumb_path = 'mvd_temp/%s.jpg' % code
@@ -505,13 +539,45 @@ async def mod_new_message(session, event) -> None:
thumbnail = None thumbnail = None
else: else:
thumbnail = thumb_path 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 # send file
await event.reply(message='Audio is downloaded, uploading it to Telegram...') await event.reply(message='Audio is downloaded, uploading it to Telegram...')
try: try:
await event.client.send_file( await event.client.send_file(
entity=peer, entity=peer,
file=new_name, 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']), mime_type=utils.get_mime(data['ext']),
file_size=video_data['size'], file_size=video_data['size'],
thumb=thumbnail, thumb=thumbnail,