close

How to stream audio from a Youtube URL in Python (without download)?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to stream audio from a Youtube URL in Python (without download) in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to stream audio from a Youtube URL in Python (without download)?

  1. How to stream audio from a Youtube URL in Python (without download)?

    However we can use it to get correct url, and then use player such as vlc to play directly without downloading.

  2. stream audio from a Youtube URL in Python (without download)

    However we can use it to get correct url, and then use player such as vlc to play directly without downloading.

Method 1

pafy according to its documentation do not list playing media directly (at least I didn’t find any).

However we can use it to get correct url, and then use player such as vlc to play directly without downloading.

You can download vlc from here

First we get correct / best URL from youtube using pafy

import pafy
import vlc

url = "https://www.youtube.com/watch?v=bMt47wvK6u0"
video = pafy.new(url)
best = video.getbest()
playurl = best.url

Over here playurl is best URL to play. Then we use VLC to play it.

Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new(playurl)
Media.get_mrl()
player.set_media(Media)
player.play()

This will open a window with no controls (play/pause/stop etc). You can run these command on the repr window or at python prompt (depending upon how you are using it)
You will need to build one accordingly using vlc commands such as

>>> player.pause() #-- to pause video
>>> player.play()  #-- resume paused video. On older versions, 
                   #     this function was called resume
>>> player.stop()  #-- to stop/end video

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read