Create Program To Download Audio from YouTube Video

thumbnail
In this tutorial, you will learn to create program to download audio from YouTube video. You need to install and import youtube_dl in order to continue this tutorial. The source code to install and import youtube_dl is detailed below.
#!pip install youtube-dl # For Google Colab
#pip3 install youtube-dl
import youtube_dl
def download_audio(video, format="mp3", bitrate="192"):
  download_option = {
      "format": "bestaudio/best",
      "postprocessors":[{
          "key": "FFmpegExtractAudio",
          "preferredcodec": format,
          "preferredquality": bitrate
      }]
  }

  with youtube_dl.YoutubeDL(download_option) as dl:
    print(f"Downloading audio for {video}")
    dl.download()

download_audio("https://www.youtube.com/watch?v=nUtgOwCh2uY")
print("Finished download audio")
Downloading audio for https://www.youtube.com/watch?v=nUtgOwCh2uY
[youtube] nUtgOwCh2uY: Downloading webpage
[youtube] nUtgOwCh2uY: Downloading video info webpage
[download] Destination: Sword Art Online - Alicization - War of Underworld Opening Full『Haruka Tomatsu - Resolution』-nUtgOwCh2uY.webm
[download] 100% of 4.51MiB in 00:00
[ffmpeg] Destination: Sword Art Online - Alicization - War of Underworld Opening Full『Haruka Tomatsu - Resolution』-nUtgOwCh2uY.mp3
Deleting original file Sword Art Online - Alicization - War of Underworld Opening Full『Haruka Tomatsu - Resolution』-nUtgOwCh2uY.webm (pass -k to keep)
Finished download audio
Back To Top