Created
June 2, 2024 11:42
-
-
Save engividal/11bd9af936286a500021a593da7b4a3d to your computer and use it in GitHub Desktop.
This script allows you to download the audio from a YouTube video and convert it to MP3 format, saving the resulting file in the user's "Videos" folder on Windows. Dependencies include pytube, pydub, and ffmpeg.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from pytube import YouTube | |
from pydub import AudioSegment | |
import os | |
import sys | |
# Logger configuration | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
def download_youtube_video_as_mp3(url, output_path): | |
""" | |
Downloads the audio from a YouTube video and converts it to MP3 format. | |
Parameters: | |
url (str): The URL of the YouTube video to be downloaded. | |
output_path (str): The path where the MP3 file will be saved. | |
Steps: | |
- Start the download process and log the initial message. | |
- Create a YouTube object from the provided URL and log the video title. | |
- Filter to find the best available audio stream and log the search. | |
- Download the audio and save it in the output_path, logging the completion of the download. | |
- Define the MP3 file path based on the downloaded file. | |
- Convert the downloaded audio to MP3 using pydub and log the conversion. | |
- Remove the original audio file and log the removal. | |
- Log the completion of the entire process. | |
""" | |
try: | |
logging.info("Starting download process...") | |
# Download the video | |
yt = YouTube(url) | |
logging.info(f"Video title: {yt.title}") | |
logging.info("Looking for the best available audio stream...") | |
video = yt.streams.filter(only_audio=True).first() | |
logging.info("Downloading audio...") | |
downloaded_file = video.download(output_path=output_path) | |
logging.info(f"Audio successfully downloaded: {downloaded_file}") | |
# Define the output file path | |
base, ext = os.path.splitext(downloaded_file) | |
mp3_file = base + '.mp3' | |
logging.info("Converting audio to MP3...") | |
# Convert to MP3 using pydub | |
audio = AudioSegment.from_file(downloaded_file) | |
audio.export(mp3_file, format="mp3") | |
logging.info(f"Conversion completed: {mp3_file}") | |
# Remove the original video file | |
os.remove(downloaded_file) | |
logging.info("Original video file removed.") | |
logging.info(f"Download and conversion completed: {mp3_file}") | |
except Exception as e: | |
logging.error(f"An error occurred: {e}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
logging.error("Usage: python download_mp3.py <YouTube video URL>") | |
sys.exit(1) | |
# Get the user's "Videos" folder path | |
user_profile = os.getenv("USERPROFILE") | |
videos_folder = os.path.join(user_profile, "Videos") | |
url = sys.argv[1] | |
download_youtube_video_as_mp3(url, videos_folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment