Skip to content

Commit 2b89955

Browse files
committed
feat: improve play command by extracting the m4a audio instead of re-encoding it into mp3 & and also both play and video command now cache the files and if the user requested for the audio or video id it'll use that instead of downloading it over again
1 parent e2fa323 commit 2b89955

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

src/commands/play.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ export const info = {
1818
cooldown: 5000,
1919
};
2020

21+
const fileExists = async (filePath: string) => {
22+
try {
23+
await fs.promises.access(filePath, fs.constants.F_OK);
24+
return true;
25+
} catch {
26+
return false;
27+
}
28+
};
29+
2130
async function search(yt: Innertube, query: string) {
2231
log.info("Play", `Searching for ${query}`);
2332
const results = await yt.music.search(query, { type: "song" });
@@ -65,6 +74,17 @@ export default async function play(msg: Message) {
6574

6675
await msg.react("🔍");
6776

77+
const tempDir = "./.temp";
78+
await fs.promises.mkdir(tempDir, { recursive: true });
79+
const tempPath = path.join(tempDir, `${audio.id}.mp3`);
80+
const savePath = path.join(tempDir, `${audio.id}.m4a`);
81+
82+
if (await fileExists(savePath)) {
83+
const media = MessageMedia.fromFilePath(savePath);
84+
await msg.reply(media);
85+
return;
86+
}
87+
6888
const stream = await yt.download(audio.id, {
6989
type: "video+audio",
7090
quality: "best",
@@ -81,9 +101,6 @@ export default async function play(msg: Message) {
81101

82102
await msg.react("⬇️");
83103

84-
const tempDir = "./.temp";
85-
await fs.promises.mkdir(tempDir, { recursive: true });
86-
const tempPath = path.join(tempDir, `${audio.id}.mp3`);
87104
let writeStream = fs.createWriteStream(tempPath);
88105

89106
for await (const chunk of Utils.streamToIterable(stream)) {
@@ -100,17 +117,7 @@ export default async function play(msg: Message) {
100117
const chat = await msg.getChat();
101118
chat.sendStateRecording();
102119

103-
await execPromise(
104-
`ffmpeg -y -i "${tempPath}" -vn -ar 44100 -ac 2 -b:a 192k "${tempPath}.mp3"`,
105-
);
106-
107-
const media = MessageMedia.fromFilePath(`${tempPath}.mp3`);
108-
await msg.reply(media, undefined, {
109-
caption: audio.title,
110-
});
111-
112-
Promise.all([
113-
fs.promises.unlink(tempPath),
114-
fs.promises.unlink(`${tempPath}.mp3`),
115-
]);
120+
await execPromise(`ffmpeg -i "${tempPath} -vn -c:a copy "${savePath}"`);
121+
const media = MessageMedia.fromFilePath(savePath);
122+
Promise.all([msg.reply(media), fs.promises.unlink(tempPath)]);
116123
}

src/commands/video.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ export const info = {
1414
cooldown: 5000,
1515
};
1616

17+
const fileExists = async (filePath: string) => {
18+
try {
19+
await fs.promises.access(filePath, fs.constants.F_OK);
20+
return true;
21+
} catch {
22+
return false;
23+
}
24+
};
25+
1726
async function search(yt: Innertube, query: string) {
1827
log.info("Video", `Searching for ${query}`);
1928
const results = await yt.search(query, { type: "video" });
@@ -57,6 +66,18 @@ export default async function (msg: Message) {
5766

5867
await msg.react("🔍");
5968

69+
const tempDir = "./.temp";
70+
await fs.promises.mkdir(tempDir, { recursive: true });
71+
const tempPath = path.join(tempDir, `${video.video_id}.mp4`);
72+
73+
if (await fileExists(tempPath)) {
74+
const media = MessageMedia.fromFilePath(tempPath);
75+
await msg.reply(media, undefined, {
76+
caption: video.title.text,
77+
});
78+
return;
79+
}
80+
6081
const stream = await yt.download(video.video_id, {
6182
type: "video+audio",
6283
quality: "best",
@@ -73,9 +94,6 @@ export default async function (msg: Message) {
7394

7495
await msg.react("⬇️");
7596

76-
const tempDir = "./.temp";
77-
await fs.promises.mkdir(tempDir, { recursive: true });
78-
const tempPath = path.join(tempDir, `${video.video_id}.mp4`);
7997
let writeStream = fs.createWriteStream(tempPath);
8098

8199
for await (const chunk of Utils.streamToIterable(stream)) {
@@ -92,6 +110,4 @@ export default async function (msg: Message) {
92110
await msg.reply(media, undefined, {
93111
caption: video.title.text,
94112
});
95-
96-
fs.promises.unlink(tempPath);
97113
}

0 commit comments

Comments
 (0)