get dfpwm's from youtube in HQ

This commit is contained in:
Soph :3 2025-07-11 21:43:06 +03:00
parent 60ecf8a1a2
commit e1c81afc0c
3 changed files with 165 additions and 2 deletions

View file

@ -1,10 +1,47 @@
import { Hono } from 'hono'
const app = new Hono()
const joypixels = require("emoji-toolkit");
import { mkdirSync, writeFileSync, existsSync, exists, readFileSync } from 'fs'
import { mkdirSync, writeFileSync, existsSync, rmSync, readFileSync, readFile } from 'fs'
import { $ } from 'bun';
import ytSearch from 'yt-search';
let ytdlpPath = "yt-dlp"
if(existsSync("./yt-dlp")) {
ytdlpPath = "./yt-dlp"
}
app.post("/youtube", async (c) => {
const auth = c.req.header('Authorization')
if (auth !== process.env.PASSWORD) {
return c.text('Unauthorized', 401)
}
const body = await c.req.json().catch(() => null);
if (!body || typeof body.title !== 'string' || typeof body.artist !== 'string') {
return c.text('Missing or invalid title/artist', 400);
}
let title = body.title;
let artist = body.artist;
if(!existsSync("cache/" + title + " - " + artist + ".dfpwm")) {
const searchResult = await ytSearch(`${artist} ${title}`);
const video = searchResult.videos[0];
if (!video) throw new Error("No video found");
console.log(video)
await $`${ytdlpPath} --audio-format opus -x ${video.url} -o test`;
await $`ffmpeg -i test.opus -ar 48000 -ac 1 -f dfpwm "cache/${title} - ${artist}.dfpwm"`;
rmSync("test.opus")
}
return c.body(readFileSync(`cache/${title} - ${artist}.dfpwm`), {
headers: {
'Content-Type': 'audio/vnd.dfpwm',
'Content-Disposition': `attachment; filename="${title} - ${artist}.dfpwm"`,
},
});
})
app.post('/render', async (c) => {
const auth = c.req.header('Authorization')
if (auth !== process.env.PASSWORD) {