Created
June 11, 2025 15:23
-
-
Save pmacMaps/c4d38bc41ac8217c8246f016c2b7cdde to your computer and use it in GitHub Desktop.
Python functions to extract audio files from YouTube videos
This file contains hidden or 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
""" | |
These functions allow you to create mp3 files from YouTube videos. The scripts were created using Vibe coding. | |
You can view the AI conversation at https://chatgpt.com/share/684995e4-aa14-800b-9ce5-f8c74cd34d9a. | |
Backup of conversation located at https://github.com/pmacMaps/pmacMaps.github.io/blob/ec99d84c3c3f88eb6bb1a8c93251f95ed38f2cde/assets/documents/Download_Audio_From_YouTube_ChatGPT_Chat_History.pdf | |
download_playlist_audio: extracts audio from all videos in a playlist | |
download_audio: extracts audio from a single video | |
download_audio_segment: extracts audio from a single video using a start and end time stamp | |
Notes: you will need ffmpeg installed on the machine running this script. | |
You will also need to install the yt_dlp python module | |
""" | |
import yt_dlp as youtube_dl | |
from os import path | |
# extracts audio files (mp3) for all videos in the provided YouTube playlist | |
def download_playlist_audio(playlist_url, download_path='.', file_name_template=None): | |
""" | |
playlist_url: string, YouTube playlist URL | |
download_path: string, directory to save extracted audio files | |
file_name_template: string, template for file names for extracted audio files | |
""" | |
# handle file names for extracted files | |
if file_name_template: | |
outtmpl = path.join(download_path, file_name_template + '_%(playlist_index)s.%(ext)s') | |
else: | |
outtmpl = path.join(download_path, '%(title)s.%(ext)s') | |
# Set the download options | |
ydl_opts = { | |
'format': 'bestaudio/best', | |
'postprocessors': [{ | |
'key': 'FFmpegExtractAudio', | |
'preferredcodec': 'mp3', | |
'preferredquality': '192', | |
}], | |
'outtmpl': outtmpl, | |
'yesplaylist': True, | |
'ignoreerrors': True, | |
} | |
# extract audio from videos in playlist | |
with youtube_dl.YoutubeDL(ydl_opts) as ydl: | |
try: | |
ydl.download([playlist_url]) | |
except Exception as e: | |
print(f'error downloading song: {e}') | |
# end function to download from playlist | |
# extracts audio file (mp3) from the YouTube video provided | |
def download_audio(url, download_path='.', file_name=None): | |
""" | |
url: string, YouTube video URL | |
download_path: string, directory to save extracted audio file | |
file_name: string, file name for extracted audio file | |
""" | |
# If a custom file name is provided, set it as outtmpl | |
if file_name: | |
outtmpl = path.join(download_path, file_name + '.%(ext)s') | |
else: | |
outtmpl = path.join(download_path, '%(title)s.%(ext)s') | |
# Set the download options | |
ydl_opts = { | |
'format': 'bestaudio/best', | |
'postprocessors': [{ | |
'key': 'FFmpegExtractAudio', | |
'preferredcodec': 'mp3', | |
'preferredquality': '192', | |
}], | |
'outtmpl': outtmpl, | |
} | |
# extract audio from video | |
with youtube_dl.YoutubeDL(ydl_opts) as ydl: | |
try: | |
ydl.download([url]) | |
except Exception as e: | |
print(f'error downloading song: {e}') | |
# end function to download single song | |
# extracts audio file (mp3) from a segment of the YouTube video provided | |
def download_audio_segment(url, start_time, end_time, download_path='.', file_name=None): | |
""" | |
url: string, YouTube video URL | |
start_time: string, start segment of video to download (HH:MM:SS) | |
end_time: string, end segment of video to download (HH:MM:SS) | |
download_path: string, directory to save extracted audio file | |
file_name: string, file name for extracted audio file | |
""" | |
# Output filename | |
if file_name: | |
outtmpl = path.join(download_path, file_name + '.%(ext)s') | |
else: | |
outtmpl = path.join(download_path, '%(title)s.%(ext)s') | |
# Construct ffmpeg arguments to trim audio | |
ffmpeg_args = [ | |
'-ss', start_time, # start time | |
'-to', end_time # end time | |
] | |
# Set the download options | |
ydl_opts = { | |
'format': 'bestaudio/best', | |
'outtmpl': outtmpl, | |
'postprocessors': [{ | |
'key': 'FFmpegExtractAudio', | |
'preferredcodec': 'mp3', | |
'preferredquality': '192', | |
}], | |
'postprocessor_args': ffmpeg_args, | |
'prefer_ffmpeg': True, | |
} | |
# extract audio from video | |
with youtube_dl.YoutubeDL(ydl_opts) as ydl: | |
try: | |
ydl.download([url]) | |
except Exception as e: | |
print(f'error downloading song: {e}') | |
# end function to download audio from provided start and end time segments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment