Skip to content

Commit

Permalink
Fix convet media for some videos
Browse files Browse the repository at this point in the history
Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Jan 28, 2024
1 parent bdf732d commit b5f6f3f
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 164 deletions.
79 changes: 41 additions & 38 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,6 @@ async def generateSampleVideo(self, dl_path, gid, unwanted_files, ft_delete):
return dl_path

async def convertMedia(self, dl_path, gid, o_files, m_size, ft_delete):
async with task_dict_lock:
task_dict[self.mid] = MediaConvertStatus(self, gid)

fvext = []
if self.convertVideo:
vdata = self.convertVideo.split()
Expand Down Expand Up @@ -759,39 +756,45 @@ async def proceedConvert(m_path):
else:
return False

if await aiopath.isfile(dl_path):
output_file = await proceedConvert(dl_path)
if output_file:
if self.seed:
self.newDir = f"{self.dir}10000"
new_output_file = output_file.replace(self.dir, self.newDir)
await makedirs(self.newDir, exist_ok=True)
await move(output_file, new_output_file)
return new_output_file
else:
try:
await remove(dl_path)
except:
return False
return output_file
return dl_path
else:
for dirpath, _, files in await sync_to_async(walk, dl_path, topdown=False):
for file_ in files:
if self.cancelled:
return False
f_path = ospath.join(dirpath, file_)
res = await proceedConvert(f_path)
if res:
if self.seed and not self.newDir:
o_files.append(f_path)
fsize = await aiopath.getsize(f_path)
m_size.append(fsize)
ft_delete.append(res)
else:
try:
await remove(f_path)
except:
return False
async with task_dict_lock:
task_dict[self.mid] = MediaConvertStatus(self, gid)

return dl_path
async with cpu_eater_lock:
if await aiopath.isfile(dl_path):
output_file = await proceedConvert(dl_path)
if output_file:
if self.seed:
self.newDir = f"{self.dir}10000"
new_output_file = output_file.replace(self.dir, self.newDir)
await makedirs(self.newDir, exist_ok=True)
await move(output_file, new_output_file)
return new_output_file
else:
try:
await remove(dl_path)
except:
return False
return output_file
return dl_path
else:
for dirpath, _, files in await sync_to_async(
walk, dl_path, topdown=False
):
for file_ in files:
if self.cancelled:
return False
f_path = ospath.join(dirpath, file_)
res = await proceedConvert(f_path)
if res:
if self.seed and not self.newDir:
o_files.append(f_path)
fsize = await aiopath.getsize(f_path)
m_size.append(fsize)
ft_delete.append(res)
else:
try:
await remove(f_path)
except:
return False

return dl_path
50 changes: 43 additions & 7 deletions bot/helper/ext_utils/media_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,76 @@
from bot.helper.ext_utils.files_utils import ARCH_EXT, get_mime_type


async def convert_video(listener, video_file, ext):
async def convert_video(listener, video_file, ext, retry=False):
base_name = ospath.splitext(video_file)[0]
output = f"{base_name}.{ext}"
cmd = ["ffmpeg", "-i", video_file, "-c", "copy", output]
if retry:
cmd = [
"ffmpeg",
"-i",
video_file,
"-preset",
"ultrafast",
"-c:v",
"libx264",
"-c:a",
"aac",
"-map",
"0",
"-threads",
f"{cpu_count() // 2}",
output,
]
else:
cmd = ["ffmpeg", "-i", video_file, "-map", "0", "-c", "copy", output]
if listener.cancelled:
return False
async with subprocess_lock:
listener.suproc = await create_subprocess_exec(*cmd, stderr=PIPE)
listener.suproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.suproc.communicate()
if listener.cancelled:
return False
code = listener.suproc.returncode
if code == 0:
return output
elif code == -9:
listener.cancelled = True
return False
else:
stderr = stderr.decode().strip()
LOGGER.error(
f"{stderr}. Something went wrong while converting video, mostly file is corrupted. Path: {video_file}"
)
if not retry:
return await convert_video(listener, video_file, ext, True)
return False


async def convert_audio(listener, audio_file, ext):
base_name = ospath.splitext(audio_file)[0]
output = f"{base_name}.{ext}"
cmd = ["ffmpeg", "-i", audio_file, output]
cmd = [
"ffmpeg",
"-i",
audio_file,
"-preset",
"ultrafast",
"-map",
"0",
"-threads",
f"{cpu_count() // 2}",
output,
]
if listener.cancelled:
return False
async with subprocess_lock:
listener.suproc = await create_subprocess_exec(*cmd, stderr=PIPE)
listener.suproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.suproc.communicate()
if listener.cancelled:
return False
code = listener.suproc.returncode
if code == 0:
return output
elif code == -9:
listener.cancelled = True
return False
else:
stderr = stderr.decode().strip()
Expand Down Expand Up @@ -362,6 +393,7 @@ async def split_file(
return False
code = listener.suproc.returncode
if code == -9:
listener.cancelled = True
return False
elif code != 0:
stderr = stderr.decode().strip()
Expand Down Expand Up @@ -440,6 +472,7 @@ async def split_file(
return False
code = listener.suproc.returncode
if code == -9:
listener.cancelled = True
return False
elif code != 0:
stderr = stderr.decode().strip()
Expand Down Expand Up @@ -479,6 +512,8 @@ async def createSampleVideo(listener, video_file, sample_duration, part_duration
"ffmpeg",
"-i",
video_file,
"-preset",
"ultrafast",
"-filter_complex",
filter_complex,
"-map",
Expand All @@ -502,6 +537,7 @@ async def createSampleVideo(listener, video_file, sample_duration, part_duration
return False
code = listener.suproc.returncode
if code == -9:
listener.cancelled = True
return False
elif code == 0:
return output_file
Expand Down
Loading

0 comments on commit b5f6f3f

Please sign in to comment.