A subset of the YouTube Downloader script. This Python script allows you to download audio from YouTube videos and convert them to MP3 format in 32 kbit/s, 96 kbit/s, 128 kbit/s, 192 kbit/s, 256 kbit/s and 320 kbit/s
Features:
- Validates YouTube video URLs.
- Extracts video title and removes non-alphabetical characters.
- Checks for video availability.
- Downloads the audio stream in MP4 format.
- Converts the MP4 audio to MP3 format.
- Cleans up by removing the downloaded MP4 file.
Usage:
- Ensure you have the following libraries installed:
requests
,pytube
,os
,pydub
,re
,urllib.parse
. You can install them usingpip install requests pytube os pydub re urllib.parse
. - Make sure you have FFmpeg installed on your system (https://github.com/BtbN/FFmpeg-Builds/releases) and the ffmpeg.exe, ffplay.exe, ffprobe.exe files in your project folder.
- Save the code as
youtube_mp3.py
. - Run the script:
python youtube_mp3.py
. - Paste the YouTube video URL when prompted.
- The script will download the audio, convert it to MP3, and save it in the same directory as the script.
Note:
- This script is for educational purposes only and should not be used for downloading copyrighted material without permission.
- This is a work in progress and may have bugs or limitations.
Updates:
-
Direct MP4 to MP3 conversion has been achieved.
-
Both www.youtube.com or youtu.be are accepted.
-
Added new standard sample rate 48 khz.
-
Added new bitrate options 32 kbit/s – generally acceptable only for speech, 96 kbit/s – generally used for speech or low-quality streaming, 128 kbit/s – mid-range bitrate quality, 192 kbit/s – medium-quality bitrate, 256 kbit/s – a commonly used high-quality bitrate and 320 kbit/s – highest level supported by the MP3 standard.
-
Currently the script relies on an external executable, ffmpeg-master-latest-win64-gpl.zip, downloaded from GitHub. This zip file contains three applications: ffmpeg, ffprobe, and ffplay.
-
Currently at the user is needs to manually download ffmpeg-master-latest-win64-gpl.zip, unzip it and place the three applications in the same directory as my Python script for it to function. Therefore the solution I need to implement is to create a script that automatically downloads the three applications from ffmpeg-master-latest-win64-gpl.zip, and push down into the same directory. This way, the script could download or pre-install the executable within itself, eliminating the need for manual download and external file searching.
-
The directory structure of the extracted zip file has an extra nested folder (ffmpeg-master-latest-win64-gpl) inside the ffmpeg-master-latest-win64-gpl folder, which is causing the FileNotFoundError. This issue arises because the zip file may contain multiple levels of directories. The path it's looking for doesn't match the actual path of the extracted files due to these additional nested directories.
-
The solution that was implemented was we can dynamically handle the folder structure to ensure the correct bin directory is found, regardless of how deep it is nested.
-
Next implementation is to create a Docker container to prevent the constant need to update pytube.