← back to browse
goat command

Mehedi

Convert Mehedi video to audio

0
views
0
likes
0
installs

Aliases: mehedi

raw source
const axios = require("axios");
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const ffmpeg = require("ffmpeg-static");

module.exports = {
  config: {
    name: "Mehedi",
    aliases: ["mehedi"],
    version: "1.0",
    author: "badhon-bbz",
    countDown: 5,
    role: 0,
    category: "media",
    guide: {
      en: "{pn}"
    },
    description: "Convert Mehedi video to audio"
  },

  onStart: async function ({ message }) {
    const videoUrl = "https://i.imgur.com/rj7c0GE.mp4";

    const cacheDir = path.resolve(process.cwd(), "cache");

    if (!fs.existsSync(cacheDir)) {
      fs.mkdirSync(cacheDir, { recursive: true });
    }

    const id = Date.now();

    const videoPath = path.resolve(
      cacheDir,
      "Mehedi_" + id + ".mp4"
    );

    const audioPath = path.resolve(
      cacheDir,
      "Mehedi_" + id + ".mp3"
    );

    try {
      if (!ffmpeg || typeof ffmpeg !== "string") {
        throw new Error("FFmpeg path not found");
      }

      const response = await axios.get(videoUrl, {
        responseType: "arraybuffer",
        timeout: 60000,
        headers: {
          "User-Agent": "Mozilla/5.0"
        }
      });

      fs.writeFileSync(videoPath, response.data);

      if (!fs.existsSync(videoPath)) {
        throw new Error("Video download failed");
      }

      await new Promise((resolve, reject) => {
        const process = spawn(ffmpeg, [
          "-y",
          "-i",
          videoPath,
          "-vn",
          "-codec:a",
          "libmp3lame",
          "-q:a",
          "2",
          audioPath
        ]);

        let errorText = "";

        process.stderr.on("data", function (data) {
          errorText += data.toString();
        });

        process.on("error", function (error) {
          reject(error);
        });

        process.on("close", function (code) {
          if (code === 0) {
            resolve();
          } else {
            reject(
              new Error(
                "FFmpeg failed: " + errorText
              )
            );
          }
        });
      });

      if (!fs.existsSync(audioPath)) {
        throw new Error("MP3 file was not created");
      }

      await message.reply({
        attachment: fs.createReadStream(audioPath)
      });

      setTimeout(function () {
        try {
          if (fs.existsSync(videoPath)) {
            fs.unlinkSync(videoPath);
          }

          if (fs.existsSync(audioPath)) {
            fs.unlinkSync(audioPath);
          }
        } catch (e) {
          console.log("Cleanup error:", e.message);
        }
      }, 10000);

    } catch (error) {
      console.error("MEHEDI ERROR:", error);

      try {
        if (fs.existsSync(videoPath)) {
          fs.unlinkSync(videoPath);
        }

        if (fs.existsSync(audioPath)) {
          fs.unlinkSync(audioPath);
        }
      } catch (e) {}

      return message.reply(
        "❌ Mehedi audio তৈরি করা যায়নি।"
      );
    }
  }
};

View raw file