manoj-chavan-13 / theta

Theta by PlayBucket is an innovative platform aimed at revolutionizing the way users download YouTube videos. Our mission is to provide an effortless, efficient, and user-friendly solution for anyone who wants to save their favorite content for offline viewing.
https://theta-bucket.vercel.app/
1 stars 1 forks source link

Theta by PlayBucket

image

Table of Contents

  1. Overview
  2. Key Features
  3. Live Demo
  4. Installation
  5. Common Issues
  6. Contribution
  7. Dependencies
  8. Functions to Download Audio and Video
  9. Express Endpoints

๐ŸŒŸ Overview

Theta is an intuitive platform designed for downloading YouTube videos seamlessly. With a user-friendly interface, Theta simplifies the process of saving your favorite videos for offline viewing, making it an essential tool for content lovers everywhere.

๐Ÿš€ Key Features

๐Ÿ–ฅ๏ธ User-Friendly Interface

Navigate easily through a clean and straightforward layout, ensuring a hassle-free experience.

๐Ÿ“ฅ High Formats

Download videos in High formats and resolutions to suit your preferences. Enjoy high quality on save content!

๐ŸŒ Open Source

Contribute and collaborate! Theta is open to contributions from anyone looking to enhance its functionality. Join the community and help us grow!

๐ŸŽฅ Live Demo

Explore Theta in action here: theta-bucket.vercel.app

Installation

To run Theta locally, follow these steps:

Prerequisites

Ensure you have the following installed on your machine:

Steps to Run Locally

  1. Clone the repository:

    git clone https://github.com/manoj-chavan-13/theta.git
    cd theta
  2. Install dependencies:

    npm install
  3. Run the project:

    npm start
  4. Open your web browser and go to http://localhost:3000 to start using the application.

Common Issues

While Theta operates smoothly on localhost, users may encounter issues when deploying on platforms like Vercel. Common errors include:

Troubleshooting Tips

If you experience any issues, consider the following:

  1. Check the Console: Look for error messages in your browser's console to diagnose problems.
  2. Network Issues: Ensure you have a stable internet connection and that there are no network restrictions affecting the application.

If issues persist, please report them on our GitHub Issues page.

Contribution

We welcome and encourage contributions to Theta! To get involved, please follow these steps:

  1. Fork the repository: Click the "Fork" button on the repository page.
  2. Create a new branch:
    git checkout -b feature/your-feature-name
  3. Make your changes: Edit the code and test your modifications locally.
  4. Commit your changes:
    git commit -m "Brief description of your changes"
  5. Push to your fork:
    git push origin feature/your-feature-name
  6. Open a Pull Request: Navigate to the original repository and submit a new Pull Request, detailing your changes.

๐Ÿ“ฆ Dependencies

Theta utilizes several key packages to enhance its functionality. Below is a list of the packages used, their purposes, and basic usage instructions:

Package List

Installation

To install the dependencies, run:

npm install @distube/ytdl-core @ffmpeg/ffmpeg body-parser cors express ffmpeg-static fluent-ffmpeg mergerino mp4box ytdl-core

This will install all the necessary packages for Theta to function properly.

Functions to Download Audio and Video

Download Audio Function
const downloadAudio = async (videoURL) => {
  const audioFilePath = path.join(__dirname, "audio.mp3");
  const audioStream = ytdl(videoURL, {
    filter: (format) => format.hasAudio,
    quality: "highest",
    requestOptions: {
      headers: {
        "User-Agent": "Mozilla/5.0 ...", // Setting a user-agent to prevent blocking
      },
    },
  });
  return new Promise((resolve, reject) => {
    audioStream
      .pipe(fs.createWriteStream(audioFilePath))
      .on("finish", () => {
        console.log("Audio download complete!");
        resolve(audioFilePath);
      })
      .on("error", (err) => {
        console.error("Error downloading audio:", err);
        reject(err);
      });
  });
};
Download Video Function
const downloadVideo = async (videoURL) => {
  const videoFilePath = path.join(__dirname, "video.mp4");
  const videoStream = ytdl(videoURL, {
    filter: (format) => format.hasVideo,
    quality: "highestvideo",
    requestOptions: {
      headers: {
        "User-Agent": "Mozilla/5.0 ...", // Setting a user-agent to prevent blocking
      },
    },
  });
  return new Promise((resolve, reject) => {
    videoStream
      .pipe(fs.createWriteStream(videoFilePath))
      .on("finish", () => {
        console.log("Video download complete!");
        resolve(videoFilePath);
      })
      .on("error", (err) => {
        console.error("Error downloading video:", err);
        reject(err);
      });
  });
};

Combine Audio and Video Function

const combineAudioVideo = async (audioFilePath, videoFilePath) => {
  const outputFilePath = path.join(__dirname, "output.mp4");
  return new Promise((resolve, reject) => {
    ffmpeg()
      .setFfmpegPath(ffmpegPath) // Setting the path for FFmpeg
      .input(videoFilePath)
      .input(audioFilePath)
      .outputOptions("-c:v copy") // Copy video codec
      .outputOptions("-c:a aac") // Use AAC audio codec
      .output(outputFilePath)
      .on("end", () => {
        console.log("Merging finished! Output saved to:", outputFilePath);
        resolve(outputFilePath);
      })
      .on("error", (err) => {
        console.error("Error during merging:", err);
        reject(err);
      })
      .run();
  });
};

Delete Files After a Delay

const deleteFilesAfterDelay = (filePaths, delay) => {
  setTimeout(() => {
    filePaths.forEach((filePath) => {
      fs.unlink(filePath, (err) => {
        if (err) {
          console.error(`Error deleting file ${filePath}:`, err);
        } else {
          console.log(`File deleted after ${delay / 1000} seconds: ${filePath}`);
        }
      });
    });
  }, delay);
};

Express Endpoints

Download Endpoint
app.post("/download", async (req, res) => {
  const { videoURL } = req.body;

  try {
    const audioFilePath = await downloadAudio(videoURL);
    const videoFilePath = await downloadVideo(videoURL);
    const outputFilePath = await combineAudioVideo(audioFilePath, videoFilePath);

    res.json({
      success: true,
      outputFile: `/download/${path.basename(outputFilePath)}`,
    });

    // Schedule deletion of the temporary files after 10 minutes
    deleteFilesAfterDelay(
      [audioFilePath, videoFilePath, outputFilePath],
      10 * 60 * 1000
    );
  } catch (error) {
    res.json({ success: false, message: error.message });
  }
});
Serve Output File for Download
app.get("/download/:filename", (req, res) => {
  const filePath = path.join(__dirname, req.params.filename);
  res.download(filePath, (err) => {
    if (err) {
      console.error("Error downloading file:", err);
      res.status(500).send("Could not download the file.");
    }
  });
});

3. Putting It All Together

To run this application:

  1. Ensure you have the necessary packages installed.
  2. Set up your Node.js server using the provided code.
  3. Make a POST request to the /download endpoint with a JSON body containing the videoURL (e.g., via Postman or a frontend application).
  4. After processing, you can download the merged video from the provided output link.

Example POST Request

Hereโ€™s an example of how to send a POST request using cURL:

curl -X POST http://localhost:3000/download -H "Content-Type: application/json" -d '{"videoURL": "https://www.youtube.com/watch?v=YOUR_VIDEO_ID"}'

Replace YOUR_VIDEO_ID with the actual ID of the YouTube video you want to download.

License

This project is licensed under the ISC License. See the LICENSE file for more details.

Contact

For questions, issues, or feedback, please feel free to raise an issue on the GitHub Repository or connect with me on LinkedIn: manoj-chavan-1311 .


Thank you for exploring Theta! We hope you find it useful for effortlessly downloading your favorite YouTube videos. Happy downloading!