//made by zaky //dari comotan wkwk

const ytdl = require('ytdl-core'); const fs = require('fs'); const ffmpeg = require('fluent-ffmpeg'); const search = require('yt-search');

let handler = async (m, { conn, text }) => { if (!text) return m.reply('Example: .play kill bill');

conn.sendMessage(m.chat, { react: { text: '🕒', key: m.key, } });

try { let results = await search(text); let videoId = results.videos[0].videoId; let info = await ytdl.getInfo(videoId); let title = info.videoDetails.title.replace(/[^\w\s]/gi, ''); let thumbnailUrl = https://i.ytimg.com/vi/${videoId}/hqdefault.jpg; let url = info.videoDetails.video_url; let duration = parseInt(info.videoDetails.lengthSeconds); let uploadDate = new Date(info.videoDetails.publishDate).toLocaleDateString(); let views = info.videoDetails.viewCount; let minutes = Math.floor(duration / 60); let description = results.videos[0].description; let seconds = duration % 60; let durationText = ${minutes}:${seconds < 10 ? '0' : ''}${seconds};

function formatViews(views) {
  if (views >= 1000000) {
    return (views / 1000000).toFixed(1) + 'M';
  } else if (views >= 1000) {
    return (views / 1000).toFixed(1) + 'K';
  } else {
    return views.toString();
  }
}

let audio = ytdl(videoId, { quality: 'highestaudio' });
let inputFilePath = 'tmp/' + title + '.webm';
let outputFilePath = 'tmp/' + title + '.mp3';

let fileSize = 0;
audio.on('data', (chunk) => {
  fileSize += chunk.length;
  if (fileSize > 50 * 1024 * 1024) {
    audio.destroy();
    fs.unlinkSync(inputFilePath);
    return m.reply('😅Audio exceeds 50MB limit');
  }
});

let viewsFormatted = formatViews(views);
let infoText = `

${title.toUpperCase()}

╭─ • 「 YOUTUBE PLAY 」 │ ◦ Duration: ${durationText} │ ◦ Upload Date: ${uploadDate} │ ◦ Views: ${viewsFormatted} ╰──── •`;

conn.sendMessage(m.chat, {
  text: infoText,
  contextInfo: {
    externalAdReply: {
      showAdAttribution: true,
      title: `${title}`,
      body: description,
      thumbnailUrl: thumbnailUrl,
      mediaUrl: url,
      sourceUrl: url,
      mediaType: 2,
      renderLargerThumbnail: true
    }
  }
}, { quoted: m });

audio.pipe(fs.createWriteStream(inputFilePath)).on('finish', async () => {
  ffmpeg(inputFilePath)
    .toFormat('mp3')
    .on('end', async () => {
      let thumbnailData = await conn.getFile(thumbnailUrl);
      let buffer = fs.readFileSync(outputFilePath);
      conn.sendFile(m.chat, buffer, `${title}.mp3`, '', m);
      fs.unlinkSync(inputFilePath);
      fs.unlinkSync(outputFilePath);
    })
    .on('error', (err) => {
      console.log(err);
      m.reply(`🥺An error occurred while converting the audio: ${err.message}`);
      fs.unlinkSync(inputFilePath);
      fs.unlinkSync(outputFilePath);
    })
    .save(outputFilePath);
});

} catch (e) { console.log(e); m.reply(🥺An error occurred while searching for the song: ${e.message}); } };

handler.help = ['play']; handler.tags = ['downloader']; handler.command = /^play$/i; handler.limit = true;

module.exports = handler;


原文地址: http://www.cveoy.top/t/topic/jdt3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录