Skip to content

Commit

Permalink
sync mltb
Browse files Browse the repository at this point in the history
  • Loading branch information
5hojib committed Jan 1, 2025
1 parent 60c71e6 commit 80436fb
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 171 deletions.
1 change: 0 additions & 1 deletion bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
queue_dict_lock = Lock()
qb_listener_lock = Lock()
cpu_eater_lock = Lock()
subprocess_lock = Lock()
same_directory_lock = Lock()
extension_filter = ["aria2", "!qB"]
drives_names = []
Expand Down
2 changes: 1 addition & 1 deletion bot/core/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Config:
DELETE_LINKS = False
EXTENSION_FILTER = ""
FSUB_IDS = ""
FFMPEG_CMDS: ClassVar[list[str]] = []
FFMPEG_CMDS = {}
FILELION_API = ""
GDRIVE_ID = ""
INCOMPLETE_TASK_NOTIFIER = False
Expand Down
126 changes: 88 additions & 38 deletions bot/helper/common.py

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ def process_argument_with_values(start_index):
values = []
for j in range(start_index + 1, total):
if items[j] in arg_base:
break
check = " ".join(values).strip()
if check.startswith("[") and check.endswith("]"):
break
elif check.startswith("["):
pass
else:
break
values.append(items[j])
return values

Expand All @@ -141,16 +147,6 @@ def process_argument_with_values(start_index):
"-med",
]:
arg_base[part] = True
elif part == "-ff":
i += 1
if i < total:
values = []
while i < total:
values.append(items[i])
if items[i].endswith("]"):
break
i += 1
arg_base[part] = " ".join(values)
else:
sub_list = process_argument_with_values(i)
if sub_list:
Expand Down
6 changes: 2 additions & 4 deletions bot/helper/ext_utils/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ async def update_qbittorrent(self, key, value):
async def save_qbit_settings(self):
if self._return:
return
await self.db.settings.qbittorrent.replace_one(
{"_id": TgClient.ID},
qbit_options,
upsert=True,
await self.db.settings.qbittorrent.update_one(
{"_id": TgClient.ID}, {"$set": qbit_options}, upsert=True
)

async def update_private_file(self, path):
Expand Down
2 changes: 1 addition & 1 deletion bot/helper/ext_utils/files_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def exit_clean_up(_, __):
LOGGER.info("Please wait, while we clean up and stop the running downloads")
clean_all()
srun(
["pkill", "-9", "-f", "gunicorn|xria|xnox|xtra|xone"],
["pkill", "-9", "-f", "gunicorn|xria|xnox|xtra|xone|7z"],
check=False,
)
exit(0)
Expand Down
1 change: 1 addition & 0 deletions bot/helper/ext_utils/help_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
Notes:
1. Add <code>-del</code> to the list(s) which you want from the bot to delete the original files after command run complete!
2. Seed will get disbaled while using this option
3. To execute one of pre-added lists in bot like: ({"subtitle": ["-i mltb.mkv -c copy -c:s srt mltb.mkv"]}), you must use -ff subtitle (list key)
Examples: ["-i mltb.mkv -c copy -c:s srt mltb.mkv", "-i mltb.video -c copy -c:s srt mltb", "-i mltb.m4a -c:a libmp3lame -q:a 2 mltb.mp3", "-i mltb.audio -c:a libmp3lame -q:a 2 mltb.mp3"]
Here I will explain how to use mltb.* which is reference to files you want to work on.
1. First cmd: the input is mltb.mkv so this cmd will work only on mkv videos and the output is mltb.mkv also so all outputs is mkv. -del will delete the original media after complete run of the cmd.
Expand Down
112 changes: 65 additions & 47 deletions bot/helper/ext_utils/media_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from aioshutil import rmtree
from PIL import Image

from bot import LOGGER, subprocess_lock
from bot import LOGGER
from bot.core.config_manager import Config

from .bot_utils import cmd_exec, sync_to_async
Expand All @@ -28,8 +28,14 @@ async def convert_video(listener, video_file, ext, retry=False):
"-hide_banner",
"-loglevel",
"error",
"-progress",
"pipe:1",
"-i",
video_file,
"-map",
"0:v",
"-map",
"0:a",
"-c:v",
"libx264",
"-c:a",
Expand All @@ -39,17 +45,19 @@ async def convert_video(listener, video_file, ext, retry=False):
output,
]
if ext == "mp4":
cmd[10:10] = ["-c:s", "mov_text"]
cmd[16:16] = ["-c:s", "mov_text"]
elif ext == "mkv":
cmd[10:10] = ["-c:s", "ass"]
cmd[16:16] = ["-c:s", "ass"]
else:
cmd[10:10] = ["-c:s", "copy"]
cmd[16:16] = ["-c:s", "copy"]
else:
cmd = [
"xtra",
"-hide_banner",
"-loglevel",
"error",
"-progress",
"pipe:1",
"-i",
video_file,
"-map",
Expand All @@ -60,12 +68,12 @@ async def convert_video(listener, video_file, ext, retry=False):
]
if listener.is_cancelled:
return False
async with subprocess_lock:
listener.subproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.subproc.communicate()
if listener.is_cancelled:
return False
code = listener.subproc.returncode
async with listener.subprocess_lock:
listener.subproc = await create_subprocess_exec(*cmd, stdout=PIPE, stderr=PIPE)
code = await listener.subproc.wait()
async with listener.subprocess_lock:
if listener.is_cancelled:
return False
if code == 0:
return output
if code == -9:
Expand All @@ -76,7 +84,7 @@ async def convert_video(listener, video_file, ext, retry=False):
await remove(output)
return await convert_video(listener, video_file, ext, True)
try:
stderr = stderr.decode().strip()
stderr = (await listener.subproc.stderr.read()).decode().strip()
except Exception:
stderr = "Unable to decode the error!"
LOGGER.error(
Expand All @@ -93,6 +101,8 @@ async def convert_audio(listener, audio_file, ext):
"-hide_banner",
"-loglevel",
"error",
"-progress",
"pipe:1",
"-i",
audio_file,
"-threads",
Expand All @@ -101,19 +111,19 @@ async def convert_audio(listener, audio_file, ext):
]
if listener.is_cancelled:
return False
async with subprocess_lock:
listener.subproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.subproc.communicate()
if listener.is_cancelled:
return False
code = listener.subproc.returncode
async with listener.subprocess_lock:
listener.subproc = await create_subprocess_exec(*cmd, stdout=PIPE, stderr=PIPE)
code = await listener.subproc.wait()
async with listener.subprocess_lock:
if listener.is_cancelled:
return False
if code == 0:
return output
if code == -9:
listener.is_cancelled = True
return False
try:
stderr = stderr.decode().strip()
stderr = (await listener.subproc.stderr.read()).decode().strip()
except Exception:
stderr = "Unable to decode the error!"
LOGGER.error(
Expand Down Expand Up @@ -451,6 +461,8 @@ async def split_file(
"-hide_banner",
"-loglevel",
"error",
"-progress",
"pipe:1",
"-ss",
str(start_time),
"-i",
Expand All @@ -470,22 +482,25 @@ async def split_file(
out_path,
]
if not multi_streams:
del cmd[10]
del cmd[10]
if listener.is_cancelled:
return False
async with subprocess_lock:
listener.subproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.subproc.communicate()
del cmd[12]
del cmd[12]
if listener.is_cancelled:
return False
code = listener.subproc.returncode
async with listener.subprocess_lock:
listener.subproc = await create_subprocess_exec(
*cmd, stdout=PIPE, stderr=PIPE
)
code = await listener.subproc.wait()
async with listener.subprocess_lock:
if listener.is_cancelled:
return False
code = listener.subproc.returncode
if code == -9:
listener.is_cancelled = True
return False
if code != 0:
try:
stderr = stderr.decode().strip()
stderr = (await listener.subproc.stderr.read()).decode().strip()
except Exception:
stderr = "Unable to decode the error!"
with contextlib.suppress(Exception):
Expand Down Expand Up @@ -542,8 +557,9 @@ async def split_file(
start_time += lpd - 3
i += 1
else:
listener.subsize = 0
out_path = f"{dirpath}/{file_}."
async with subprocess_lock:
async with listener.subprocess_lock:
if listener.is_cancelled:
return False
listener.subproc = await create_subprocess_exec(
Expand All @@ -555,16 +571,16 @@ async def split_file(
out_path,
stderr=PIPE,
)
_, stderr = await listener.subproc.communicate()
if listener.is_cancelled:
return False
code = listener.subproc.returncode
code = await listener.subproc.wait()
async with listener.subprocess_lock:
if listener.is_cancelled:
return False
if code == -9:
listener.is_cancelled = True
return False
if code != 0:
try:
stderr = stderr.decode().strip()
stderr = (await listener.subproc.stderr.read()).decode().strip()
except Exception:
stderr = "Unable to decode the error!"
LOGGER.error(f"{stderr}. Split Document: {path}")
Expand Down Expand Up @@ -604,6 +620,8 @@ async def create_sample_video(listener, video_file, sample_duration, part_durati
"-hide_banner",
"-loglevel",
"error",
"-progress",
"pipe:1",
"-i",
video_file,
"-filter_complex",
Expand All @@ -623,19 +641,19 @@ async def create_sample_video(listener, video_file, sample_duration, part_durati

if listener.is_cancelled:
return False
async with subprocess_lock:
listener.subproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.subproc.communicate()
if listener.is_cancelled:
return False
code = listener.subproc.returncode
async with listener.subprocess_lock:
listener.subproc = await create_subprocess_exec(*cmd, stdout=PIPE, stderr=PIPE)
code = await listener.subproc.wait()
async with listener.subprocess_lock:
if listener.is_cancelled:
return False
if code == -9:
listener.is_cancelled = True
return False
if code == 0:
return output_file
try:
stderr = stderr.decode().strip()
stderr = (await listener.subproc.stderr.read()).decode().strip()
except Exception:
stderr = "Unable to decode the error!"
LOGGER.error(
Expand Down Expand Up @@ -756,19 +774,19 @@ async def run_ffmpeg_cmd(listener, ffmpeg, path):
ffmpeg[-1] = output
if listener.is_cancelled:
return False
async with subprocess_lock:
listener.subproc = await create_subprocess_exec(*ffmpeg, stderr=PIPE)
_, stderr = await listener.subproc.communicate()
if listener.is_cancelled:
return False
code = listener.subproc.returncode
async with listener.subprocess_lock:
listener.subproc = await create_subprocess_exec(*ffmpeg, stdout=PIPE, stderr=PIPE)
code = await listener.subproc.wait()
async with listener.subprocess_lock:
if listener.is_cancelled:
return False
if code == 0:
return output
if code == -9:
listener.is_cancelled = True
return False
try:
stderr = stderr.decode().strip()
stderr = (await listener.subproc.stderr.read()).decode().strip()
except Exception:
stderr = "Unable to decode the error!"
LOGGER.error(
Expand Down
28 changes: 19 additions & 9 deletions bot/helper/ext_utils/status_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,31 @@ async def get_readable_message(sid, is_user, page_no=1, status="All", page_step=
else:
msg += f"<b>{index + start_position}.{tstatus}: </b>"
msg += f"<code>{escape(f'{task.name()}')}</code>"
if tstatus not in [
MirrorStatus.STATUS_SPLIT,
MirrorStatus.STATUS_SEED,
MirrorStatus.STATUS_SAMVID,
MirrorStatus.STATUS_CONVERT,
MirrorStatus.STATUS_FFMPEG,
MirrorStatus.STATUS_QUEUEUP,
]:
if (
tstatus
not in [
MirrorStatus.STATUS_SEED,
MirrorStatus.STATUS_QUEUEUP,
MirrorStatus.STATUS_SPLIT,
]
or (MirrorStatus.STATUS_SPLIT
and task.listener.subsize)
):
progress = (
await task.progress()
if iscoroutinefunction(task.progress)
else task.progress()
)
if task.listener.subname:
msg += f"\n<i>{task.listener.subname[:35]}</i>"
msg += f"\n{get_progress_bar_string(progress)} {progress}"
msg += f"\n<b>Processed:</b> {task.processed_bytes()} of {task.size()}"
if task.listener.subname:
size = (
f"{get_readable_file_size(task.listener.subsize)} ({task.size()})"
)
else:
size = task.size()
msg += f"\n<b>Processed:</b> {task.processed_bytes()} of {size}"
msg += f"\n<b>Speed:</b> {task.speed()} | <b>ETA:</b> {task.eta()}"
if hasattr(task, "seeders_num"):
with contextlib.suppress(Exception):
Expand Down
Loading

0 comments on commit 80436fb

Please sign in to comment.