Extract audio only from YouTube videos using youtube-dl Python library

To extract just the audio from YouTube videos with youtube-dl, set the format to ‘bestaudio’ and add a postprocessor for your preferred audio format:

import youtube_dl

config = {
    'format': 'bestaudio',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}

You’ll need ffmpeg installed for the audio conversion to work.