Yt play audio

by Herza
45 Raw
/* 
* DON'T DELETE MY WM PLEASE
@ ################
$ =====
~ type: plugins/cjs
~ description: YouTube play
~ note: must install yt-search, notmebotz-tools modules first
$ ===========
* Follow my github
https://github.com/herzonly
* Follow my Instagram
https://instagram.com/justme.herz
* Follow my channel https://whatsapp.com/channel/0029VaGVOvq1iUxY6WgHLv2R
@ ################
DONT DELETE MY WM PLEASE 
*/

const search = require("yt-search");
const { youtubev3 } = require('notmebotz-tools');
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");

let handler = async (m, { conn, text }) => {
    if (!text) throw "Masukkan Judul Lagu Yang Ingin Dicari";
    
    const look = await search(text);
    const convert = look.videos[0];
    if (!convert) throw "Video/Audio Tidak Ditemukan";
    if (convert.seconds >= 3600) return conn.reply(m.chat, "Video is longer than 1 hour!", m);

    const caption = `╭─────═[  Youtube Play  ]═─────⋆
├ ⬡ Title:  ${convert.title}
├ ⬡ Duration : ${convert.timestamp}
├ ⬡ Upload At : ${convert.ago}
├ ⬡ Viewers : ${convert.views}
├ ⬡ ID : ${convert.videoId}
├ ⬡ Description : ${convert.description}
├ ⬡ Url : ${convert.url}
╰───────────────⋆`;

    try {
        let a = await youtubev3(convert.url, "720");
        let inputPath = path.join(__dirname, `tmp/${Date.now()}.mp4`);
        let outputPath = inputPath.replace(".mp4", ".mp3");

        let download = await fetch(a.data.media);
        let buffer = await download.buffer();
        fs.writeFileSync(inputPath, buffer);

        exec(`ffmpeg -i "${inputPath}" -q:a 0 -map a "${outputPath}"`, async (err) => {
            if (err) return conn.reply(m.chat, "Error: " + err.message, m);
            fs.unlinkSync(inputPath);
            await conn.sendMessage(m.chat, {
                extendedTextMessage: {
                    text: caption,
                    contextInfo: {
                        externalAdReply: {
                            title: `Now playing... ${convert.title}`,
                            mediaType: 1,
                            body: "Enjoy your music!",
                            previewType: 0,
                            renderLargerThumbnail: true,
                            thumbnailUrl: convert.image,
                            sourceUrl: convert.url,
                        },
                    },
                    mentions: [m.sender],
                },
            }, {});
            await conn.sendMessage(m.chat, {
                audio: { url: outputPath },
                mimetype: "audio/mpeg",
                contextInfo: {
                    externalAdReply: {
                        title: convert.title,
                        body: "",
                        thumbnailUrl: convert.image,
                        sourceUrl: convert.url,
                        mediaType: 1,
                        showAdAttribution: true,
                        renderLargerThumbnail: true,
                    },
                },
            }, { quoted: m });
            fs.unlinkSync(outputPath);
        });
    } catch (error) {
        conn.reply(m.chat, "Error: " + error.message, m);
    }
};

handler.command = handler.help = ["play", "song", "ds"];
handler.tags = ["downloader"];
handler.exp = 0;
handler.limit = true;
handler.premium = false;

module.exports = handler;