How can I play YouTube music with Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How can I play YouTube music with Python?

¿Cómo puedo reproducir música de YouTube con Python?

24th Dec 2021, 3:35 PM
CGO!
CGO! - avatar
17 Answers
+ 13
★«Caleb Guerra Ortega»★ you can use the python package "youtubedl" to download content (movie clips, audio ...) from YouTube. Next you can use "PyAudio" to play audio. youtube-dl and PyAudio are cros-platform powerful librairies. https://en.wikipedia.org/wiki/Youtube-dl
24th Dec 2021, 4:05 PM
Elon
Elon - avatar
+ 5
Hi there, you can use the "pytube" module for downloading the song and the "playsound" module to play it. Read through the documentation and you'll have your project up and running in no time! Both of which can be found here: https://pypi.org/project/pytube/ https://pypi.org/project/playsound/
24th Dec 2021, 4:05 PM
skaltecho
+ 3
★«Caleb Guerra Ortega»★ do you mean stream music from YouTube in your python code ?
24th Dec 2021, 8:03 PM
Elon
Elon - avatar
+ 3
Nazil_the_professor I know, but I want to play a music from YouTube since my program Python
26th Dec 2021, 1:19 PM
CGO!
CGO! - avatar
+ 2
Yes, of course
24th Dec 2021, 8:10 PM
CGO!
CGO! - avatar
+ 2
(only run on desktop no mobile) use mpv. mpv is a media player that allow you to stream medias(video/audio) from any url. Download link: https://mpv.io/ import re, requests, subprocess, urllib.parse, urllib.request from bs4 import BeautifulSoup music_name = "Linkin Park Numb" query_string = urllib.parse.urlencode({"search_query": music_name}) formatUrl = urllib.request.urlopen("https://www.youtube.com/results?" + query_string) search_results = re.findall(r"watch\?v=(\S{11})", formatUrl.read().decode()) clip = requests.get("https://www.youtube.com/watch?v=" + "{}".format(search_results[0])) clip2 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[0]) inspect = BeautifulSoup(clip.content, "html.parser") yt_title = inspect.find_all("meta", property="og:title") for concatMusic1 in yt_title: pass print(concatMusic1['content']) subprocess.Popen( "start /b " + "path\\to\\mpv.exe " + clip2 + " --no-video --loop=inf --input-ipc-server=\\\\.\\pipe\\mpv-pipe > output.txt", shell=True)
24th Dec 2021, 8:20 PM
Elon
Elon - avatar
+ 2
Hmm, say "start: must be a root", Elon Yemp #RELOADED#
24th Dec 2021, 8:26 PM
CGO!
CGO! - avatar
+ 2
★«Caleb Guerra Ortega»★ you're running on Linux ? Mac os?
24th Dec 2021, 8:34 PM
Elon
Elon - avatar
+ 2
I run it in my mobile and my computator is Window
24th Dec 2021, 8:35 PM
CGO!
CGO! - avatar
+ 2
Qufu[ISLAM-SOLDIER] Error: "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.9/site-packages/pafy/backend_youtube_dl.py", line 54, in _fetch_basic self._dislikes = self._ydl_info['dislike_count'] KeyError: 'dislike_count'
25th Dec 2021, 2:06 PM
CGO!
CGO! - avatar
+ 1
skaltecho That's I know, but I want to play a music from Internet
24th Dec 2021, 7:58 PM
CGO!
CGO! - avatar
+ 1
Well, ok, Elon Yemp #RELOADED#, that isn't I was want, but ok
24th Dec 2021, 8:00 PM
CGO!
CGO! - avatar
+ 1
What more info, Qufu[ISLAM-SOLDIER]? I need to play a music from YouTube since a Python program.
24th Dec 2021, 8:01 PM
CGO!
CGO! - avatar
+ 1
Just install pywhatkit pip install pywhatkit
26th Dec 2021, 2:05 PM
Jeet Panchal
Jeet Panchal - avatar
+ 1
26th Dec 2021, 4:30 PM
CGO!
CGO! - avatar
3rd Jan 2022, 6:32 PM
CGO!
CGO! - avatar
0
Use python package "youtubedl" And also from audio "PyAudio"
26th Dec 2021, 9:26 AM
Nazil_the_professor
Nazil_the_professor - avatar