Skip to content

Commit

Permalink
Merge pull request Ankit404butfound#318 from DebadritoCG/master
Browse files Browse the repository at this point in the history
Play Youtube Video/Music in python console(without opening browser and without downloading).
  • Loading branch information
Ankit404butfound authored Feb 21, 2024
2 parents 0162f9f + 760ba5d commit 3d8f63b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
80 changes: 80 additions & 0 deletions playonyt_stream
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import requests #pip install requests
import zipfile
import os
import yt_dlp as youtube_dl # pip install yt-dlp
import time

def playonyt_stream(search):
'''
Used to play music in youtube without opening the browser(plays song in python console) and without downloading the song/ytvideo(streaming).
Automatically install vlc media player in current working directory if it is not present in C:\Program Files\VideoLAN\VLC.

Ignore the libvlc errors if it occurs
'''
try:
os.add_dll_directory(r"C:\Program Files\VideoLAN\VLC")
import vlc #pip install python-vlc
except:
if "VLC" in os.listdir():
os.add_dll_directory(f'{os.getcwd()}\\VLC')
import vlc
else:
print('VLC media player not installed, Installing It.....')
param_dict = {
"id": "19mwPKY0LTkJ4I_p6j__cijSIcppzPDLX",
"export": "download",
"authuser": "1",
"confirm": "t",
"uuid": "aedd8891-820d-4990-a09d-dc4f055505ff",
"at": "APZUnTUN6UOl55PNEU-BKds63wLx:1708498961088",
}
r_obj = requests.get('https://drive.usercontent.google.com/download', params=param_dict)
with open('VLC.zip', 'wb') as f:
f.write(r_obj.content)
f.close()

with zipfile.ZipFile("VLC.zip", "r") as zip_ref:
zip_ref.extractall()

os.remove('VLC.zip')

os.add_dll_directory(f'{os.getcwd()}\\VLC')
import vlc

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

s = str(search)
url = f"https://www.youtube.com/results?q={s}"
count = 0
cont = requests.get(url)
data = cont.content
data = str(data)
lst = data.split('"')
for i in lst:
count += 1
if i == "WEB_PAGE_TYPE_WATCH":
break
if lst[count - 5] == "/results":
return None

link = f"https://www.youtube.com{lst[count - 5]}"

ydl = youtube_dl.YoutubeDL(ydl_opts)
info = ydl.extract_info(link, download=False)
url = info['url']
duration = info['duration']

Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new(url)
Media.get_mrl()
player.set_media(Media)
player.play()
time.sleep(duration)
1 change: 1 addition & 0 deletions pywhatkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
sendwhatdoc_immediately,
sendimg_or_video_immediately
)
from pywhatkit.playonyt_stream import playonyt_stream

_system = system().lower()
if _system in ("darwin", "windows"):
Expand Down

0 comments on commit 3d8f63b

Please sign in to comment.