Skip to content

Commit

Permalink
audio only format
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Feb 9, 2022
1 parent ff3eaea commit f0c7a60
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
29 changes: 22 additions & 7 deletions ytdlbot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand Down
3 changes: 0 additions & 3 deletions ytdlbot/limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 6 additions & 4 deletions ytdlbot/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
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
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion ytdlbot/ytdl_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f0c7a60

Please sign in to comment.