Balasundar
Balasundar's Tech Blog

Balasundar's Tech Blog

Extract audio from YouTube video using Python

Extract audio from YouTube video using Python

Balasundar's photo
Balasundar

Published on Dec 7, 2020

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

In this article, we will see the implementation of audio extraction from YouTube video in Python.

Introduction

One day I wanted to download just the audio of a YouTube video. I searched for applications which can do it for me. But I ended up with the applications filled with annoying ads. So I started searching for the possibilities to achieve this in Python. Off-course I found the solution and it is very simpler and easier than you think.

Dependencies:

Pytube

Pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos.

Why Pytube

  1. Support for Both Progressive & DASH Streams
  2. Support for downloading complete playlist
  3. Easily Register on_download_progress & on_download_complete callbacks
  4. Command-line Interfaced Included
  5. Caption Track Support
  6. Outputs Caption Tracks to .srt format (SubRip Subtitle)
  7. Ability to Capture Thumbnail URL.
  8. Extensively Documented Source Code
  9. No Third-Party Dependencies

Installation of Pytube

Pytube can be installed by executing the following command on your terminal

 pip install pytube3

Installed! Now we can move forward to the coding part.

>>> from pytube import YouTube
>>> selected_video = YouTube('https://www.youtube.com/watch?v=xWOoBJUqlbI')
>>> print(selected_video.streams)
[<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">, 
 <Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">, 
 <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028" progressive="False" type="video">, 
 <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f" progressive="False" type="video">, 
 <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401f" progressive="False" type="video">,
 <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
 <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015" progressive="False" type="video">,
 <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9" progressive="False" type="video">, 
 <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c" progressive="False" type="video">, 
 <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">,
 <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus" progressive="False" type="audio">, 
 <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus" progressive="False" type="audio">,
 <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus" progressive="False" type="audio">]

"selected_video.streams" will return list of available video and audio streams. For now we are only focusing on audio streams. The audio streams can be filtered from the streams like shown in the below code block.

>>> print(selected_video.streams.filter(only_audio=True))
[<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">,
 <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus" progressive="False" type="audio">,
 <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus" progressive="False" type="audio">,
 <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus" progressive="False" type="audio">]

You can download any type of audio from the list. In this article we are going to download mp4 format. It can be done by just passing the file_extension parameter to the filter() method.

>>> print(selected_video.streams.filter(only_audio=True, file_extension='mp4'))
[<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">]

Let's download the audio now!. By using the download method.

>>> audio = selected_video.streams.filter(only_audio=True, file_extension='mp4').first()
or
>>> audio = selected_video.streams.filter(only_audio=True, file_extension='mp4')[0]
>>> audio.download()
'current_working_directory/video_title.mp4'
or
>>> audio.download('path_to_save_the_file')
'given_path/video_title.mp4'

You made it!. You downloaded the audio of the YouTube video. By default the audio file will be saved in the name of the video title with selected extension in the same directory where your python script resides.

Final Python file

from pytube import YouTube

# where to save. 
# replce /home/balasundar/Downloads/ with the path where you want to store the dowload file
destination = "/home/balasundar/Downloads/"
# link of the video to be downloaded
# Replace with the Youtube video link you want to download.
video_link = "https://www.youtube.com/watch?v=xWOoBJUqlbI"

try:
    video = YouTube(video_link)
    # filtering the audio. File extension can be mp4/webm
    # You can see all the available streams by print(video.streams)
    audio = video.streams.filter(only_audio=True, file_extension='mp4').first()
    audio.download()
    print('Download Completed!')

except:
    print("Connection Error")  # to handle exception

Also you can download the code from my GitHub Repository. Drop a start if you found it useful.
Want to convert the downloaded audio to mp3? Yes!, Off course

Do share your valuable feedback and suggestions!
Thank you for reading, I would love to connect with you at LinkedIn.

 
Share this

Impressum

Do connect with me on LinkedIn | Twitter | Github | Instagram |
Please share your feedback and suggestions.

Proudly part of