Skip to content

Commit

Permalink
only stream for tiktok and use specialized args
Browse files Browse the repository at this point in the history
  • Loading branch information
vaaski committed May 7, 2024
1 parent b6bfe26 commit 9fb19e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 9fb19e3

Please sign in to comment.