remove os.path, fix long filename in overlay2

This commit is contained in:
BennyThink 2022-02-06 16:19:40 +08:00
parent 0b86b5b1c7
commit e5b92287c3
No known key found for this signature in database
GPG Key ID: 6CD0DBDA5235D481
2 changed files with 13 additions and 19 deletions

View File

@ -44,9 +44,9 @@ def sizeof_fmt(num: int, suffix='B'):
def edit_text(bot_msg, text):
key = f"{bot_msg.chat.id}-{bot_msg.message_id}"
time.sleep(random.random())
# if the key exists, we shouldn't send edit message
if not r.exists(key):
time.sleep(random.random())
r.set(key, "ok", ex=3)
bot_msg.edit_text(text)
@ -145,10 +145,9 @@ def convert_to_mp4(resp: dict, bot_msg):
bot_msg.chat.id,
"You're not VIP, so you can't convert longer video to streaming formats.")
break
edit_text(bot_msg, f"{current_time()}: Converting {os.path.basename(path)} to mp4. Please wait.")
new_name = os.path.basename(path).split(".")[0] + ".mp4"
new_file_path = os.path.join(os.path.dirname(path), new_name)
pobj = pathlib.Path(path)
edit_text(bot_msg, f"{current_time()}: Converting {pobj.name} to mp4. Please wait.")
new_file_path = pobj.with_suffix(".mp4")
cmd = ["ffmpeg", "-i", path, new_file_path]
logging.info("Detected %s, converting to mp4...", mime)
subprocess.check_output(cmd)
@ -174,7 +173,7 @@ def can_convert_mp4(video_path, uid):
def ytdl_download(url, tempdir, bm) -> dict:
chat_id = bm.chat.id
response = {"status": True, "error": "", "filepath": []}
output = os.path.join(tempdir, '%(title).50s.%(ext)s')
output = pathlib.Path(tempdir, "%(title).70s.%(ext)s").as_posix()
ydl_opts = {
'progress_hooks': [lambda d: download_hook(d, bm)],
'outtmpl': output,
@ -200,15 +199,10 @@ def ytdl_download(url, tempdir, bm) -> dict:
response["error"] = ""
break
except DownloadError as e:
err = str(e)
except (ValueError, DownloadError) as e:
logging.error("Download failed for %s ", url)
response["status"] = False
response["error"] = err
# can't return here
except ValueError as e:
response["status"] = False
response["error"] = str(e)
response["error"] = e
except Exception as e:
logging.error("UNKNOWN EXCEPTION: %s", e)
@ -244,7 +238,7 @@ def ytdl_download(url, tempdir, bm) -> dict:
def add_instagram_cookies(url: "str", opt: "dict"):
if url.startswith("https://www.instagram.com"):
opt["cookiefile"] = os.path.join(os.path.dirname(__file__), "instagram.com_cookies.txt")
opt["cookiefi22"] = pathlib.Path(__file__).parent.joinpath("instagram.com_cookies.txt").as_posix()
def run_splitter(video_path: "str"):

View File

@ -149,7 +149,7 @@ def direct_normal_download(bot_msg, client, url):
try:
head_req = requests.head(url, headers=headers)
length = int(head_req.headers.get("content-length"))
except:
except (TypeError, requests.exceptions.RequestException):
length = 0
if remain < length:
bot_msg.reply_text(f"Sorry, you have reached your quota.\n")
@ -197,16 +197,16 @@ def normal_audio(bot_msg, client):
with tempfile.TemporaryDirectory() as tmp:
logging.info("downloading to %s", tmp)
base_path = pathlib.Path(tmp)
video_path = base_path.joinpath(fn).as_posix()
video_path = base_path.joinpath(fn)
audio = base_path.joinpath(fn).with_suffix(".m4a")
client.send_chat_action(chat_id, 'record_video_note')
client.download_media(bot_msg, video_path)
logging.info("downloading complete %s", video_path)
# execute ffmpeg
client.send_chat_action(chat_id, 'record_audio')
subprocess.check_output(f"ffmpeg -y -i '{video_path}' -vn -acodec copy '{audio.as_posix()}'", shell=True)
subprocess.check_output(f"ffmpeg -y -i '{video_path}' -vn -acodec copy '{audio}'", shell=True)
client.send_chat_action(chat_id, 'upload_audio')
client.send_audio(chat_id, audio.as_posix())
client.send_audio(chat_id, audio)
Redis().update_metrics("audio_success")
@ -277,7 +277,7 @@ def ytdl_normal_download(bot_msg, client, url):
@Panel.register
def hot_patch(*args):
git_path = pathlib.Path().cwd().parent.as_posix()
git_path = pathlib.Path().cwd().parent
logging.info("Hot patching on path %s...", git_path)
pip_install = "pip install -r requirements.txt"