Skip to content

Instantly share code, notes, and snippets.

@golanlevin
Created June 22, 2024 04:24
Show Gist options
  • Save golanlevin/41d67bb78b07b8f72e47125f184ddf99 to your computer and use it in GitHub Desktop.
Save golanlevin/41d67bb78b07b8f72e47125f184ddf99 to your computer and use it in GitHub Desktop.
Download YouTube videos as MP3s from URLs
# You will need to:
# pip install yt-dlp
# Put YouTube URLs in a text file,
# optionally followed by a tab and a number for destination directories, for example:
# https://www.youtube.com/watch?v=l7A0lDnlRMQ 15
# https://www.youtube.com/watch?v=x1VMJKOz9SI 15
# Usage:
# python download_youtube_audio.py
import os
import re
from yt_dlp import YoutubeDL
# Path to your text file containing the YouTube URLs and numbers
input_file = 'youtube_urls.txt'
# Path to the file for logging failed downloads
failed_log_file = 'failed_downloads.txt'
# Base directory where MP3 files will be saved
base_output_dir = 'downloaded_mp3s'
unsorted_dir = os.path.join(base_output_dir, 'unsorted')
os.makedirs(base_output_dir, exist_ok=True)
os.makedirs(unsorted_dir, exist_ok=True)
# Characters to be removed
remove_chars = r"[ ,;:'\"`~!@#$%^&*()\[\]{}?/\\|=+<>-]"
def sanitize_filename(title):
# Replace spaces and hyphens with underscores
title = re.sub(r"[ -]", "_", title)
# Remove punctuation and non-alphanumeric characters
title = re.sub(remove_chars, "", title)
return title
def download_audio(url, output_dir):
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
try:
with YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
title = sanitize_filename(info_dict['title'])
ydl_opts['outtmpl'] = os.path.join(output_dir, f'{title}.%(ext)s')
with YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print(f'Successfully downloaded and converted: {url}')
except Exception as e:
print(f'Error downloading {url}: {e}')
with open(failed_log_file, 'a') as log_file:
log_file.write(url + '\n')
with open(input_file, 'r') as file:
lines = file.readlines()
for line in lines:
# Skip blank lines
if not line.strip():
continue
parts = line.strip().split('\t')
url = parts[0].strip()
number = parts[1].strip() if len(parts) > 1 else None
if url:
if number:
output_dir = os.path.join(base_output_dir, number)
else:
output_dir = unsorted_dir
os.makedirs(output_dir, exist_ok=True)
download_audio(url, output_dir)
else:
print(f'Invalid line: {line.strip()}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment