audio only format

fix #59
This commit is contained in:
BennyThink 2022-02-09 11:35:18 +08:00
parent ff3eaeaea9
commit f0c7a60081
No known key found for this signature in database
GPG key ID: 6CD0DBDA5235D481
4 changed files with 29 additions and 15 deletions

View file

@ -24,7 +24,7 @@ import yt_dlp as ytdl
from tqdm import tqdm
from yt_dlp import DownloadError
from config import ENABLE_VIP, MAX_DURATION, TG_MAX_SIZE
from config import AUDIO_FORMAT, ENABLE_VIP, MAX_DURATION, TG_MAX_SIZE
from db import Redis
from limit import VIP
from utils import (adjust_formats, apply_log_formatter, current_time,
@ -139,10 +139,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
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]
edit_text(bot_msg, f"{current_time()}: Converting {path.name} to mp4. Please wait.")
new_file_path = path.with_suffix(".mp4")
cmd = ["ffmpeg", "-y", "-i", path, new_file_path]
logging.info("Detected %s, converting to mp4...", mime)
subprocess.check_output(cmd)
index = resp["filepath"].index(path)
@ -183,7 +182,6 @@ def ytdl_download(url, tempdir, bm) -> dict:
]
adjust_formats(chat_id, url, formats)
add_instagram_cookies(url, ydl_opts)
# TODO it appears twitter download on macOS will fail. Don't know why...Linux's fine.
for f in formats:
if f:
ydl_opts["format"] = f
@ -207,7 +205,7 @@ def ytdl_download(url, tempdir, bm) -> dict:
return response
for i in os.listdir(tempdir):
p: "str" = os.path.join(tempdir, i)
p = pathlib.Path(tempdir, i)
file_size = os.stat(p).st_size
if ENABLE_VIP:
remain, _, ttl = VIP().check_remaining_quota(chat_id)
@ -227,11 +225,28 @@ def ytdl_download(url, tempdir, bm) -> dict:
if settings[2] == "video" or isinstance(settings[2], MagicMock):
# only convert if send type is video
convert_to_mp4(response, bm)
if settings[2] == "audio":
check_audio_format(response)
# disable it for now
# split_large_video(response)
return response
def check_audio_format(resp: "dict"):
if resp["status"]:
# all_converted = []
path: pathlib.PosixPath
for path in resp["filepath"]:
# if we can't guess file type, we assume it's video/mp4
if path.suffix != f".{AUDIO_FORMAT}":
new_path = path.with_suffix(f".{AUDIO_FORMAT}")
cmd = 'ffmpeg -y -i "{}" "{}"'.format(path, new_path)
subprocess.check_output(cmd, shell=True)
path.unlink()
index = resp["filepath"].index(path)
resp["filepath"][index] = new_path
def add_instagram_cookies(url: "str", opt: "dict"):
if url.startswith("https://www.instagram.com"):
opt["cookiefi22"] = pathlib.Path(__file__).parent.joinpath("instagram.com_cookies.txt").as_posix()

View file

@ -295,6 +295,3 @@ def subscribe_query():
print(f"{has} - {uid}")
if __name__ == '__main__':
a = VIP.extract_canonical_link("https://www.youtube.com/shorts/YrnvPPGznXM")
print(a)

View file

@ -12,12 +12,12 @@ import os
import pathlib
import re
import subprocess
import psutil
import tempfile
import threading
import time
from urllib.parse import quote_plus
import psutil
import requests
from apscheduler.schedulers.background import BackgroundScheduler
from celery import Celery
@ -223,9 +223,11 @@ def normal_audio(bot_msg, client):
Redis().update_metrics("audio_success")
def get_worker_status():
def get_dl_source():
worker_name = os.getenv("WORKER_NAME")
return f"Downloaded by {worker_name}"
if worker_name:
return f"Downloaded by {worker_name}"
return ""
def upload_transfer_sh(video_paths) -> "str":
@ -273,7 +275,7 @@ def ytdl_normal_download(bot_msg, client, url):
return
meta = get_metadata(video_path)
worker = "Downloaded by {}".format(os.getenv("WORKER_NAME", "Unknown"))
worker = get_dl_source()
cap = f"`{filename}`\n\n{url}\n\nInfo: {meta['width']}x{meta['height']} {size} {meta['duration']}s" \
f"\n{remain}\n{worker}"
settings = get_user_settings(str(chat_id))

View file

@ -249,7 +249,7 @@ def download_handler(client: "Client", message: "types.Message"):
red.update_metrics("video_request")
text = bot_text.get_receive_link_text()
time.sleep(random.random() * 2)
time.sleep(random.random() / 2)
try:
# raise pyrogram.errors.exceptions.FloodWait(10)
bot_msg: typing.Union["types.Message", "typing.Any"] = message.reply_text(text, quote=True)