From 9fb19e3605246cac654083c9193fe825fffd91f0 Mon Sep 17 00:00:00 2001 From: vaaski Date: Tue, 7 May 2024 12:17:31 +0200 Subject: [PATCH] only stream for tiktok and use specialized args --- src/constants.ts | 11 +++++++++++ src/index.ts | 25 ++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 92e6e6d..34e58a2 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -14,3 +14,14 @@ export const deniedMessage = [ "", `${bold("Do not")} try to contact me to get whitelisted, I will no longer accept anyone I don't know personally.`, ].join("\n") + +export const tiktokMatcher = (url: string) => { + const parsed = new URL(url) + return parsed.hostname.endsWith("tiktok.com") +} + +// https://github.com/yt-dlp/yt-dlp/issues/9506#issuecomment-2053987537 +export const tiktokArgs = [ + "--extractor-args", + "tiktok:api_hostname=api16-normal-c-useast1a.tiktokv.com;app_info=7355728856979392262", +] diff --git a/src/index.ts b/src/index.ts index fa938de..2168592 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,11 @@ +import { getInfo, streamFromInfo } from "@resync-tv/yt-dlp" import { InputFile } from "grammy" import { deleteMessage, errorMessage } from "./botutil" -import { deniedMessage } from "./constants" +import { deniedMessage, tiktokArgs, tiktokMatcher } from "./constants" import { ADMIN_ID, WHITELISTED_IDS } from "./environment" import { Queue } from "./queue" import { bot } from "./setup" import { removeHashtagsMentions } from "./textutil" -import { getInfo, streamFromInfo } from "@resync-tv/yt-dlp" const queue = new Queue() @@ -47,15 +47,30 @@ bot.on("message:text").on("::url", async (ctx, next) => { queue.add(async () => { try { - const info = await getInfo(url.text, ["-f", "b", "--no-playlist"]) + const isTiktok = tiktokMatcher(url.text) + const additionalArgs = isTiktok ? tiktokArgs : [] + + const info = await getInfo(url.text, [ + "-f", + "b", + "--no-playlist", + ...additionalArgs, + ]) const [download] = info.requested_downloads ?? [] if (!download || !download.url) throw new Error("No download available") if (download.vcodec || download.ext === "mp4") { - const stream = streamFromInfo(info) + let video: InputFile | string + + if (isTiktok) { + const stream = streamFromInfo(info) + video = new InputFile(stream.stdout) + } else { + video = new InputFile({ url: download.url }) + } - await ctx.replyWithVideo(new InputFile(stream.stdout), { + await ctx.replyWithVideo(video, { caption: removeHashtagsMentions(info.title), supports_streaming: true, reply_parameters: {