Download music from youtube with python script

webbackendpython

source code available on github

YouTube to MP3 Downloader

This project provides a script to download YouTube videos and convert them to MP3 format using multiple CPU cores for better performance. It also monitors CPU and memory usage during the download process.

Features

  • Download audio from YouTube videos and convert them to MP3.
  • Process multiple URLs concurrently using multiple CPU cores.
  • Monitor CPU and memory usage during the process.

Requirements

  • Python 3.6+
  • pytube for downloading YouTube videos.
  • pydub for audio conversion.
  • psutil for monitoring system usage.
  • ffmpeg or libav for audio processing (required by pydub).

Installation

  1. Install Python packages:

    pip install pytube pydub psutil
  2. Install ffmpeg:

    On macOS using Homebrew:

    brew install ffmpeg

    On Ubuntu:

    sudo apt update
    sudo apt install ffmpeg

    On Windows, download the binaries from ffmpeg.org and add them to your PATH.

  3. Clone my github repository:

    git clone https://github.com/MetythornPenn/youtube2mp3.git
  4. Install dependency:

    # go to dir 
    cd youtube2mp3
    # create python env with conda
    conda create -n env python=3.12 -y  # can be any python3 verson 
    # activate env 
    conda activate env 
    # install dependency
    pip install -r requirements.txt 
     

Usage

  1. Prepare a text file with YouTube URLs:

    Create a text file (urls.txt) with each line containing a YouTube video URL you want to download.

     # example urls.txt file 
     https://www.youtube.com/watch?v=MFtfIpt7DfY&list=PLCl5gq1pTcnXSGkvV1-QemxiEPYDz6GnT&index=2
     
     https://www.youtube.com/watch?v=aNk0DIWFw8w
     
     https://www.youtube.com/watch?v=ZLiGQsPm1gg
     
     https://www.youtube.com/watch?v=D7F_rGhB7hE
  2. Run the script:

    python main.py