diff --git a/bot/helper/common.py b/bot/helper/common.py index 1dbc7771f1a..61e6a88ad9d 100644 --- a/bot/helper/common.py +++ b/bot/helper/common.py @@ -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() @@ -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 diff --git a/bot/helper/ext_utils/media_utils.py b/bot/helper/ext_utils/media_utils.py index ccd743b2d82..018022bdf42 100644 --- a/bot/helper/ext_utils/media_utils.py +++ b/bot/helper/ext_utils/media_utils.py @@ -12,14 +12,31 @@ 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 @@ -27,23 +44,36 @@ async def convert_video(listener, video_file, ext): 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 @@ -51,6 +81,7 @@ async def convert_audio(listener, audio_file, ext): if code == 0: return output elif code == -9: + listener.cancelled = True return False else: stderr = stderr.decode().strip() @@ -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() @@ -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() @@ -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", @@ -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 diff --git a/bot/helper/mirror_utils/download_utils/direct_link_generator.py b/bot/helper/mirror_utils/download_utils/direct_link_generator.py index ad13f94bbf2..657701f2ca9 100644 --- a/bot/helper/mirror_utils/download_utils/direct_link_generator.py +++ b/bot/helper/mirror_utils/download_utils/direct_link_generator.py @@ -68,74 +68,74 @@ def direct_link_generator(link): elif any(x in domain for x in ["akmfiles.com", "akmfls.xyz"]): return akmfiles(link) elif any( - x in domain - for x in [ - "dood.watch", - "doodstream.com", - "dood.to", - "dood.so", - "dood.cx", - "dood.la", - "dood.ws", - "dood.sh", - "doodstream.co", - "dood.pm", - "dood.wf", - "dood.re", - "dood.video", - "dooood.com", - "dood.yt", - "doods.yt", - "dood.stream", - "doods.pro", - "ds2play.com", - ] + x in domain + for x in [ + "dood.watch", + "doodstream.com", + "dood.to", + "dood.so", + "dood.cx", + "dood.la", + "dood.ws", + "dood.sh", + "doodstream.co", + "dood.pm", + "dood.wf", + "dood.re", + "dood.video", + "dooood.com", + "dood.yt", + "doods.yt", + "dood.stream", + "doods.pro", + "ds2play.com", + ] ): return doods(link) elif any( - x in domain - for x in [ - "streamtape.com", - "streamtape.co", - "streamtape.cc", - "streamtape.to", - "streamtape.net", - "streamta.pe", - "streamtape.xyz", - ] + x in domain + for x in [ + "streamtape.com", + "streamtape.co", + "streamtape.cc", + "streamtape.to", + "streamtape.net", + "streamta.pe", + "streamtape.xyz", + ] ): return streamtape(link) elif any(x in domain for x in ["wetransfer.com", "we.tl"]): return wetransfer(link) elif any( - x in domain - for x in [ - "terabox.com", - "nephobox.com", - "4funbox.com", - "mirrobox.com", - "momerybox.com", - "teraboxapp.com", - "1024tera.com", - "terabox.app", - ] + x in domain + for x in [ + "terabox.com", + "nephobox.com", + "4funbox.com", + "mirrobox.com", + "momerybox.com", + "teraboxapp.com", + "1024tera.com", + "terabox.app", + ] ): return terabox(link) elif any( - x in domain - for x in [ - "filelions.co", - "filelions.site", - "filelions.live", - "filelions.to", - "cabecabean.lol", - "filelions.online", - "embedwish.com", - "streamwish.com", - "kitabmarkaz.xyz", - "wishfast.top", - "streamwish.to", - ] + x in domain + for x in [ + "filelions.co", + "filelions.site", + "filelions.live", + "filelions.to", + "cabecabean.lol", + "filelions.online", + "embedwish.com", + "streamwish.com", + "kitabmarkaz.xyz", + "wishfast.top", + "streamwish.to", + ] ): return filelions_and_streamwish(link) elif any(x in domain for x in ["streamhub.ink", "streamhub.to"]): @@ -150,26 +150,26 @@ def direct_link_generator(link): else: return sharer_scraper(link) elif any( - x in domain - for x in [ - "anonfiles.com", - "zippyshare.com", - "letsupload.io", - "hotfile.io", - "bayfiles.com", - "megaupload.nz", - "letsupload.cc", - "filechan.org", - "myfile.is", - "vshare.is", - "rapidshare.nu", - "lolabits.se", - "openload.cc", - "share-online.is", - "upvid.cc", - "uptobox.com", - "uptobox.fr", - ] + x in domain + for x in [ + "anonfiles.com", + "zippyshare.com", + "letsupload.io", + "hotfile.io", + "bayfiles.com", + "megaupload.nz", + "letsupload.cc", + "filechan.org", + "myfile.is", + "vshare.is", + "rapidshare.nu", + "lolabits.se", + "openload.cc", + "share-online.is", + "upvid.cc", + "uptobox.com", + "uptobox.fr", + ] ): raise DirectDownloadLinkException(f"ERROR: R.I.P {domain}") else: @@ -193,7 +193,7 @@ def mediafire(url, session=None): if "/folder/" in url: return mediafireFolder(url) if final_link := findall( - r"https?:\/\/download\d+\.mediafire\.com\/\S+\/\S+\/\S+", url + r"https?:\/\/download\d+\.mediafire\.com\/\S+\/\S+\/\S+", url ): return final_link[0] if session is None: @@ -614,7 +614,7 @@ def gdtot(url): except Exception as e: raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") from e if ( - drive_link := findall(r"myDl\('(.*?)'\)", res.text) + drive_link := findall(r"myDl\('(.*?)'\)", res.text) ) and "drive.google.com" in drive_link[0]: return drive_link[0] else: @@ -684,7 +684,7 @@ def sharer_scraper(url): except Exception as e: raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") from e if ( - drive_link := HTML(res.text).xpath("//a[contains(@class,'btn')]/@href") + drive_link := HTML(res.text).xpath("//a[contains(@class,'btn')]/@href") ) and "drive.google.com" in drive_link[0]: return drive_link[0] else: @@ -833,7 +833,7 @@ def __fetch_links(session, _id=0, folderPath=""): folderPath = details["title"] filename = content["name"] if (sub_type := content.get("sub_type")) and not filename.endswith( - sub_type + sub_type ): filename += f".{sub_type}" item = { @@ -1269,16 +1269,16 @@ def easyupload(url): raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") first_page_html = HTML(_res.text) if ( - first_page_html.xpath("//h6[contains(text(),'Password Protected')]") - and not _password + first_page_html.xpath("//h6[contains(text(),'Password Protected')]") + and not _password ): raise DirectDownloadLinkException( f"ERROR:\n{PASSWORD_ERROR_MESSAGE.format(url)}" ) if not ( - match := search( - r"https://eu(?:[1-9][0-9]?|100)\.easyupload\.io/action\.php", _res.text - ) + match := search( + r"https://eu(?:[1-9][0-9]?|100)\.easyupload\.io/action\.php", _res.text + ) ): raise DirectDownloadLinkException( "ERROR: Failed to get server for EasyUpload Link" @@ -1323,27 +1323,27 @@ def filelions_and_streamwish(url): hostname = parsed_url.hostname scheme = parsed_url.scheme if any( - x in hostname - for x in [ - "filelions.co", - "filelions.live", - "filelions.to", - "filelions.site", - "cabecabean.lol", - "filelions.online", - ] + x in hostname + for x in [ + "filelions.co", + "filelions.live", + "filelions.to", + "filelions.site", + "cabecabean.lol", + "filelions.online", + ] ): apiKey = config_dict["FILELION_API"] apiUrl = "https://api.filelions.co" elif any( - x in hostname - for x in [ - "embedwish.com", - "streamwish.com", - "kitabmarkaz.xyz", - "wishfast.top", - "streamwish.to", - ] + x in hostname + for x in [ + "embedwish.com", + "streamwish.com", + "kitabmarkaz.xyz", + "wishfast.top", + "streamwish.to", + ] ): apiKey = config_dict["STREAMWISH_API"] apiUrl = "https://api.streamwish.com" @@ -1411,12 +1411,12 @@ def streamvid(url: str): f"ERROR: {e.__class__.__name__}" ) from e if not ( - script := html.xpath( - '//script[contains(text(),"document.location.href")]/text()' - ) + script := html.xpath( + '//script[contains(text(),"document.location.href")]/text()' + ) ): if error := html.xpath( - '//div[@class="alert alert-danger"][1]/text()[2]' + '//div[@class="alert alert-danger"][1]/text()[2]' ): raise DirectDownloadLinkException(f"ERROR: {error[0]}") raise DirectDownloadLinkException( @@ -1428,7 +1428,7 @@ def streamvid(url: str): "ERROR: direct link not found! in the script" ) elif (qualities_urls := html.xpath('//div[@id="dl_versions"]/a/@href')) and ( - qualities := html.xpath('//div[@id="dl_versions"]/a/text()[2]') + qualities := html.xpath('//div[@id="dl_versions"]/a/text()[2]') ): error = "\nProvide a quality to download the video\nAvailable Quality:" for quality_url, quality in zip(qualities_urls, qualities): @@ -1461,7 +1461,7 @@ def streamhub(url): except Exception as e: raise DirectDownloadLinkException(f"ERROR: {e.__class__.__name__}") from e if directLink := html.xpath( - '//a[@class="btn btn-primary btn-go downloadbtn"]/@href' + '//a[@class="btn btn-primary btn-go downloadbtn"]/@href' ): return directLink[0] if error := html.xpath('//div[@class="alert alert-danger"]/text()[2]'): @@ -1482,11 +1482,11 @@ def pcloud(url): def tmpsend(url): parsed_url = urlparse(url) - if any(x in parsed_url.path for x in ['thank-you', 'download']): + if any(x in parsed_url.path for x in ["thank-you", "download"]): query_params = parse_qs(parsed_url.query) - if file_id := query_params.get('d'): + if file_id := query_params.get("d"): file_id = file_id[0] - elif not (file_id := parsed_url.path.strip('/')): + elif not (file_id := parsed_url.path.strip("/")): raise DirectDownloadLinkException("ERROR: Invalid URL format") referer_url = f"https://tmpsend.com/thank-you?d={file_id}" header = f"Referer: {referer_url}" diff --git a/bot/helper/mirror_utils/status_utils/media_convert_status.py b/bot/helper/mirror_utils/status_utils/media_convert_status.py index 3c06082a3ae..23615db59c3 100644 --- a/bot/helper/mirror_utils/status_utils/media_convert_status.py +++ b/bot/helper/mirror_utils/status_utils/media_convert_status.py @@ -1,4 +1,4 @@ -from bot import LOGGER, subprocess_lock +from bot import LOGGER from bot.helper.ext_utils.status_utils import get_readable_file_size, MirrorStatus @@ -38,10 +38,6 @@ def task(self): async def cancel_task(self): LOGGER.info(f"Cancelling Converting: {self.listener.name}") self.listener.cancelled = True - async with subprocess_lock: - if ( - self.listener.suproc is not None - and self.listener.suproc.returncode is None - ): - self.listener.suproc.kill() + if self.listener.suproc is not None and self.listener.suproc.returncode is None: + self.listener.suproc.kill() await self.listener.onUploadError("Converting stopped by user!")