mdfpwm support

This commit is contained in:
Soph :3 2025-07-12 15:11:46 +03:00
parent 745b92660c
commit 55840b54e5
2 changed files with 89 additions and 12 deletions

View file

@ -1,9 +1,10 @@
import { Hono } from 'hono'
const app = new Hono()
const joypixels = require("emoji-toolkit");
import { mkdirSync, writeFileSync, existsSync, rmSync, readFileSync, readFile } from 'fs'
import { mkdirSync, writeFileSync, existsSync, rmSync, readFileSync, readFile, writeFile } from 'fs'
import { $ } from 'bun';
import ytSearch from 'yt-search';
import { writeMDFPWMv3 } from './mdfpwmWriter';
let ytdlpPath = "yt-dlp"
if(existsSync("./yt-dlp")) {
ytdlpPath = "./yt-dlp"
@ -22,22 +23,49 @@ app.post("/youtube", async (c) => {
let title = body.title;
let artist = body.artist;
let filename;
if(body.mdfpwm) {
filename = "cache/"+Bun.hash(title+"-"+artist)+".mdfpwm"
let filename = "cache/"+Bun.hash(title+"-"+artist)+".dfpwm"
if(!existsSync(filename)) {
const searchResult = await ytSearch(`${artist} ${title}`);
const video = searchResult.videos[0];
if (!video) throw new Error("No video found");
console.log(video)
if(!existsSync(filename)) {
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 -af "highpass=f=60, lowpass=f=15000, dynaudnorm" -ar 48000 -ac 1 -f dfpwm "${filename}"`;
rmSync("test.opus")
await $`${ytdlpPath} --audio-format opus -x ${video.url} -o test`;
console.timeEnd("yt-dlp")
// this makes left.dfpwm & right.dfpwm
await $`ffmpeg -i test.opus -filter_complex "[0:a]channelsplit=channel_layout=stereo[left][right]; [left]highpass=f=60,lowpass=f=15000,dynaudnorm[leftf]; [right]highpass=f=60,lowpass=f=15000,dynaudnorm[rightf]" -map "[leftf]" -ar 48000 -f dfpwm left.dfpwm -map "[rightf]" -ar 48000 -f dfpwm right.dfpwm`
rmSync("test.opus")
writeFileSync(filename, writeMDFPWMv3(
readFileSync("left.dfpwm"),
readFileSync("right.dfpwm"),
{ title: title, artist: artist, album: title }
))
rmSync("right.dfpwm")
rmSync("left.dfpwm")
}
} else {
filename = "cache/"+Bun.hash(title+"-"+artist)+".dfpwm"
if(!existsSync(filename)) {
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 -af "highpass=f=60, lowpass=f=15000, dynaudnorm" -ar 48000 -ac 1 -f dfpwm "${filename}"`;
rmSync("test.opus")
}
}
return c.body(readFileSync(filename), {
headers: {
'Content-Type': 'audio/vnd.dfpwm',
'Content-Type': `audio/vnd.${body.mdfpwm ? "m" : ""}dfpwm`,
'Content-Disposition': `attachment; filename="${filename}"`,
},
});