Skip to content

Commit

Permalink
support music downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
vaaski committed May 7, 2024
1 parent 666cfb9 commit b77eba6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RUN pnpm install --frozen-lockfile

COPY src ./src

RUN apk add python3
RUN apk add python3 ffmpeg
ADD https://github.com/yt-dlp/yt-dlp-nightly-builds/releases/latest/download/yt-dlp /bin/yt-dlp
RUN chmod +x /bin/yt-dlp

Expand Down
26 changes: 26 additions & 0 deletions src/botutil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Thumbnail } from "@resync-tv/yt-dlp"
import type { Chat, Message } from "grammy/types"

import { InputFile } from "grammy/types"
import { ADMIN_ID } from "./environment"
import { bot } from "./setup"

Expand Down Expand Up @@ -84,3 +87,26 @@ export const link = (text: string, url: string) =>
export const quote = (text: string) => `<blockquote>${text}</blockquote>`
export const mention = (text: string, user_id: number) =>
`<a href="tg://user?id=${user_id}">${text}</a>`

/**
* Gets a fitting thumbnail for the `sendAudio` method.
*
* https://core.telegram.org/bots/api#sendaudio
*/
export const getThumbnail = (thumbnails?: Thumbnail[]) => {
if (!thumbnails) return undefined

const MAX_SIZE = 320

// Thumbnail sizes go from smallest to largest
const reversed = [...thumbnails].reverse()

const match = reversed.find((thumbnail) => {
const { width, height } = thumbnail
if (!width || !height) return false

return width <= MAX_SIZE && height <= MAX_SIZE
})

if (match) return new InputFile({ url: match.url })
}
20 changes: 17 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getInfo, streamFromInfo } from "@resync-tv/yt-dlp"
import { InputFile } from "grammy"
import { deleteMessage, errorMessage } from "./botutil"
import { deleteMessage, errorMessage, getThumbnail } from "./botutil"
import { deniedMessage, tiktokArgs, tiktokMatcher } from "./constants"
import { ADMIN_ID, WHITELISTED_IDS } from "./environment"
import { Queue } from "./queue"
Expand Down Expand Up @@ -64,7 +64,7 @@ bot.on("message:text").on("::url", async (ctx, next) => {
const [download] = info.requested_downloads ?? []
if (!download || !download.url) throw new Error("No download available")

if (download.vcodec || download.ext === "mp4") {
if (download.vcodec !== "none") {
let video: InputFile | string

if (isTiktok) {
Expand All @@ -82,8 +82,22 @@ bot.on("message:text").on("::url", async (ctx, next) => {
allow_sending_without_reply: true,
},
})
} else if (download.acodec !== "none") {
const stream = streamFromInfo(info, ["-x", "--audio-format", "mp3"])
const audio = new InputFile(stream.stdout)

await ctx.replyWithAudio(audio, {
caption: removeHashtagsMentions(info.title),
performer: info.uploader,
title: info.title,
thumbnail: getThumbnail(info.thumbnails),
reply_parameters: {
message_id: ctx.message?.message_id,
allow_sending_without_reply: true,
},
})
} else {
throw new Error("No video available")
throw new Error("No download available")
}
} catch (error) {
return error instanceof Error
Expand Down

0 comments on commit b77eba6

Please sign in to comment.