Skip to content

Commit

Permalink
Fix stuck getting screenshot on some arch using ffmpeg
Browse files Browse the repository at this point in the history
fix screenshots number

Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Jan 7, 2024
1 parent 9b671f7 commit eeb5bf2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 43 deletions.
51 changes: 38 additions & 13 deletions bot/helper/ext_utils/media_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from aiofiles.os import remove, path as aiopath, makedirs
from time import time
from re import search as re_search
from asyncio import create_subprocess_exec, gather
from asyncio import create_subprocess_exec, gather, wait_for
from asyncio.subprocess import PIPE
from PIL import Image
from aioshutil import move
Expand Down Expand Up @@ -153,21 +153,40 @@ async def take_ss(video_file, ss_nb) -> list:
name, _ = ospath.splitext(name)
dirpath = f"{dirpath}/screenshots/"
await makedirs(dirpath, exist_ok=True)
interval = duration // ss_nb + 1
interval = duration // (ss_nb + 1)
cap_time = interval
outputs = []
cmd = ""
cmds = []
for i in range(ss_nb):
output = f"{dirpath}SS.{name}_{i:02}.png"
outputs.append(output)
cmd += f'ffmpeg -hide_banner -loglevel error -ss {cap_time} -i "{video_file}" -q:v 1 -frames:v 1 "{output}"'
cmd = [
"ffmpeg",
"-hide_banner",
"-loglevel",
"error",
"-ss",
f"{cap_time}",
"-i",
video_file,
"-q:v",
"1",
"-frames:v",
"1",
output,
]
cap_time += interval
if i + 1 != ss_nb:
cmd += " && "
_, err, code = await cmd_exec(cmd, True)
if code != 0:
cmds.append(cmd_exec(cmd))
try:
resutls = await wait_for(gather(*cmds), timeout=15)
if resutls[0][2] != 0:
LOGGER.error(
f"Error while creating sreenshots from video. Path: {video_file}. stderr: {resutls[0][1]}"
)
return []
except:
LOGGER.error(
f"Error while creating sreenshots from video. Path: {video_file} stderr: {err}"
f"Error while creating sreenshots from video. Path: {video_file}. Error: Timeout some issues with ffmpeg with specific arch!"
)
return []
return outputs
Expand Down Expand Up @@ -216,7 +235,7 @@ async def create_thumbnail(video_file, duration):
"-loglevel",
"error",
"-ss",
str(duration),
f"{duration}",
"-i",
video_file,
"-vf",
Expand All @@ -225,10 +244,16 @@ async def create_thumbnail(video_file, duration):
"1",
des_dir,
]
_, err, code = await cmd_exec(cmd)
if code != 0 or not await aiopath.exists(des_dir):
try:
_, err, code = await wait_for(cmd_exec(cmd), timeout=15)
if code != 0 or not await aiopath.exists(des_dir):
LOGGER.error(
f"Error while extracting thumbnail from video. Name: {video_file} stderr: {err}"
)
return None
except:
LOGGER.error(
f"Error while extracting thumbnail from video. Name: {video_file} stderr: {err}"
f"Error while extracting thumbnail from video. Name: {video_file}. Error: Timeout some issues with ffmpeg with specific arch!"
)
return None
return des_dir
Expand Down
4 changes: 3 additions & 1 deletion bot/helper/mirror_utils/download_utils/jd_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ async def add_jd_download(listener, path):
for link in links
if link["availability"].lower() != "online"
]:
await retry_function(jdownloader.device.linkgrabber.remove_links, to_remove)
await retry_function(
jdownloader.device.linkgrabber.remove_links, to_remove
)
break

listener.name = listener.name or name
Expand Down
12 changes: 11 additions & 1 deletion bot/helper/mirror_utils/rclone_utils/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,17 @@ async def get_path(self, itype=""):
self.item_type == itype
elif self.list_status == "rcu":
self.item_type == "--dirs-only"
cmd = ["rclone", "lsjson", self.item_type, "--fast-list", "--no-mimetype", "--no-modtime", "--config", self.config_path, f"{self.remote}{self.path}"]
cmd = [
"rclone",
"lsjson",
self.item_type,
"--fast-list",
"--no-mimetype",
"--no-modtime",
"--config",
self.config_path,
f"{self.remote}{self.path}",
]
if self.is_cancelled:
return
res, err, code = await cmd_exec(cmd)
Expand Down
34 changes: 17 additions & 17 deletions bot/helper/mirror_utils/telegram_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ async def _msg_to_reply(self):
)
if self._sent_msg is None:
self._sent_msg = await user.send_message(
chat_id=self._listener.message.chat.id,
text="Deleted Cmd Message! Don't delete the cmd message again!",
disable_web_page_preview=True,
disable_notification=True,
)
chat_id=self._listener.message.chat.id,
text="Deleted Cmd Message! Don't delete the cmd message again!",
disable_web_page_preview=True,
disable_notification=True,
)
else:
self._sent_msg = self._listener.message
return True
Expand All @@ -139,7 +139,7 @@ async def _prepare_file(self, file_, dirpath):
self._up_path = new_path
else:
cap_mono = f"<code>{file_}</code>"
if len(file_) > 54:
if len(file_) > 60:
if is_archive(file_):
name = get_base_name(file_)
ext = file_.split(name, 1)[1]
Expand All @@ -153,7 +153,7 @@ async def _prepare_file(self, file_, dirpath):
name = file_
ext = ""
extn = len(ext)
remain = 54 - extn
remain = 60 - extn
name = name[:remain]
if (
self._listener.seed
Expand Down Expand Up @@ -198,16 +198,16 @@ async def _send_screenshots(self):
inputs.append(InputMediaPhoto(m, cap))
else:
outputs.remove(m)
if outputs:
self._sent_msg = (
await self._sent_msg.reply_media_group(
media=inputs,
quote=True,
disable_notification=True,
)
)[-1]
for m in outputs:
await remove(m)
if outputs:
self._sent_msg = (
await self._sent_msg.reply_media_group(
media=inputs,
quote=True,
disable_notification=True,
)
)[-1]
for m in outputs:
await remove(m)

async def _send_media_group(self, subkey, key, msgs):
msgs_list = await msgs[0].reply_to_message.reply_media_group(
Expand Down
12 changes: 3 additions & 9 deletions bot/modules/bot_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ async def get_buttons(key=None, edit_type=None):
buttons.ibutton("Back", "botset back")
buttons.ibutton("Close", "botset close")
for x in range(0, len(config_dict), 10):
buttons.ibutton(
f"{int(x/10)}", f"botset start var {x}", position="footer"
)
buttons.ibutton(f"{int(x/10)}", f"botset start var {x}", position="footer")
msg = f"Config Variables | Page: {int(START/10)} | State: {STATE}"
elif key == "private":
buttons.ibutton("Back", "botset back")
Expand All @@ -146,9 +144,7 @@ async def get_buttons(key=None, edit_type=None):
buttons.ibutton("Back", "botset back")
buttons.ibutton("Close", "botset close")
for x in range(0, len(aria2_options), 10):
buttons.ibutton(
f"{int(x/10)}", f"botset start aria {x}", position="footer"
)
buttons.ibutton(f"{int(x/10)}", f"botset start aria {x}", position="footer")
msg = f"Aria2c Options | Page: {int(START/10)} | State: {STATE}"
elif key == "qbit":
for k in list(qbit_options.keys())[START : 10 + START]:
Expand All @@ -160,9 +156,7 @@ async def get_buttons(key=None, edit_type=None):
buttons.ibutton("Back", "botset back")
buttons.ibutton("Close", "botset close")
for x in range(0, len(qbit_options), 10):
buttons.ibutton(
f"{int(x/10)}", f"botset start qbit {x}", position="footer"
)
buttons.ibutton(f"{int(x/10)}", f"botset start qbit {x}", position="footer")
msg = f"Qbittorrent Options | Page: {int(START/10)} | State: {STATE}"
button = buttons.build_menu(1) if key is None else buttons.build_menu(2)
return msg, button
Expand Down
4 changes: 3 additions & 1 deletion bot/modules/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ async def do(func, message):

try:
with redirect_stdout(stdout):
func_return = await sync_to_async(rfunc) if func == "exec" else await rfunc()
func_return = (
await sync_to_async(rfunc) if func == "exec" else await rfunc()
)
except Exception as e:
value = stdout.getvalue()
return f"{value}{format_exc()}"
Expand Down
2 changes: 1 addition & 1 deletion bot/modules/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def status_pages(_, query):
OULS: {get_readable_file_size(up_speed)}/s
OSDS: {get_readable_file_size(seed_speed)}/s
"""
await query.answer(msg, show_alert=True, cache_time=30)
await query.answer(msg, show_alert=True)


bot.add_handler(
Expand Down

0 comments on commit eeb5bf2

Please sign in to comment.