Code:
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=nt8b2d80kp4')
yt.streams.filter(only_audio=True)
Error:
Traceback (most recent call last):
File "/home/chris/Desktop/download-peradio-youtube.py", line 3, in <module>
yt.streams.filter(only_audio=True)
File "/home/chris/.local/lib/python3.9/site-packages/pytube/__main__.py", line 284, in streams
self.check_availability()
File "/home/chris/.local/lib/python3.9/site-packages/pytube/__main__.py", line 222, in check_availability
raise exceptions.LiveStreamError(video_id=self.video_id)
pytube.exceptions.LiveStreamError: nt8b2d80kp4 is streaming live and cannot be loaded
I am searching the mp3 of this stream (may be: https://impradio.bytemasters.gr/8002/LIVE
)
From F12 developer tool (network tab) i can't determine the url from chunk url == https://rr2---sn-vuxbavcx-n3bl.googlevideo.com/videoplayback?expire=1648612652&ei=zIBDYpuvBMKE-gb6pJKgCQ&ip=2a02%3A587%3A4b21%3Ae800%3A35f8%3A653d%3A6d21%3Ae688&id=nt8b2d80kp4.1&itag=140&source=yt_live_broadcast&requiressl=yes&mh=_r&mm=44%2C29&mn=sn-vuxbavcx-n3bl%2Csn-4g5ednsl&ms=lva%2Crdu&mv=m&mvi=2&pl=43&initcwndbps=700000&spc=4ocVC-kDs_UvdSI4lw3EA1nanCKswgqp3x4JXIvuqA&vprv=1&live=1&hang=1&noclen=1&mime=audio%2Fmp4&ns=fkRSinUFHW8LoioeBOXWBd8G&gir=yes&mt=1648590755&fvip=2&keepalive=yes&fexp=24001373%2C24007246&c=WEB&n=kSJw0LNBFe1UPg&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Clive%2Chang%2Cnoclen%2Cmime%2Cns%2Cgir&sig=AOq0QJ8wRQIhAJCiTfYpkmOOyWgzZxh5egtzOliikn1KAAKBoRYW5I-6AiBRr3W8hLKt_1DgQ_X91xXkx5SDlTi68apg5JX1iZYdOw%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIhAPCgxxKOVnY71c8a1Zx-pJh0qwo-YST4eYZ5UogPYPKnAiA0jn22b5v1Qd9ePW02fm68uDkaCOQBxhY5PpXu0d5ZiA%3D%3D&alr=yes&cpn=tHiqtO_vf7iamBU0&cver=2.20220329.01.00&sq=73251&rn=17&rbuf=10870&pot=GpIBCmW_Wf2DEiBp1s7KNz1pU8PW4RB64_buU1tbyYbk17z8ymqVCMmM9kMkTgwMuIsToHICl6x3O3i5IK0xKm8_4rUdlVWNCkdXhzsLGW6JTbOFJGdMo1_bu8l5u4IFHzEpa5TAUcdX2xIpATwYQQ4ejXizKFqJ_o12sdjuKIzYaRYlsLlvP1wIZ-NSn11RmpZtSrk=
Any help would be usefull!
I tried:
youtube-dl -gf 96 https://www.youtube.com/watch\?v\=nt8b2d80kp4
and i got an m3u8 playlist file: https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1648614747/ei/-4hDYpW_H4uw1wK5uaewCA/ip/2a02:587:4b21:e800:35f8:653d:6d21:e688/id/nt8b2d80kp4.1/itag/96/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D137/hls_chunk_host/rr2---sn-vuxbavcx-n3bl.googlevideo.com/playlist_duration/30/manifest_duration/30/spc/4ocVC5B2Kyn_91RyPOj3kFKPbQ9N/vprv/1/playlist_type/DVR/initcwndbps/5640/mh/_r/mm/44/mn/sn-vuxbavcx-n3bl/ms/lva/mv/m/mvi/2/pcm2cms/yes/pl/43/dover/11/pacing/0/keepalive/yes/fexp/24001373,24007246/mt/1648592916/sparams/expire,ei,ip,id,itag,source,requiressl,ratebypass,live,sgoap,sgovp,playlist_duration,manifest_duration,spc,vprv,playlist_type/sig/AOq0QJ8wRAIgJEXHB4L3Tt01XkMY1h10_bjmSQOtcjqz_Ngrx7r_5yMCIEMQNQiMHRMLa3wqRtCFiDHenuBM9wOzacyg8jkpXZGc/lsparams/hls_chunk_host,initcwndbps,mh,mm,mn,ms,mv,mvi,pcm2cms,pl/lsig/AG3C_xAwRgIhAKCrul2wW9VYOQV7k9adbMZNnxRuBk5kXvs4_9CF3v7ZAiEAnmAxt849-ePtaVP1YMD8rT0AdbY1rLabjQnnT9N7R0Y%3D/playlist/index.m3u8
but this file is only for 30 seconds :\
I type : http://[2a02:587:4b21:e800:35f8:653d:6d21:e688]/
in google chrome url bar and it's an ubuntu web server there...
Edit:
First i view the source code of the desired live youtube stream url (with F12 developer tools):
https://www.youtube.com/watch?v=nt8b2d80kp4
then i type this javascript code in developer tools console: ytInitialPlayerResponse.streamingData.hlsManifestUrl
After that i run this code:
from pydub import AudioSegment
import time
from io import BytesIO
import pyaudio
import subprocess
import threading
class Download_Stream(threading.Thread):
def __init__(self):
self.stdout = None
self.stderr = None
threading.Thread.__init__(self)
def run(self):
subprocess.call(['ffmpeg','-i','https://manifest.googlevideo.com/api/manifest/hls_variant/expire/1648621026/ei/gqFDYpP3AYjt1wKl646ACQ/ip/2a02%3A587%3A4b21%3Ae800%3A35f8%3A653d%3A6d21%3Ae688/id/nt8b2d80kp4.1/source/yt_live_broadcast/requiressl/yes/hfr/1/playlist_duration/30/manifest_duration/30/maxh/2160/maudio/1/spc/4ocVC0IOPvNfCvPWdJet1GiMyyw1XWw-xSQKGku_tg/vprv/1/go/1/pacing/0/nvgoi/1/keepalive/yes/fexp/24001373%2C24007246/dover/11/itag/0/playlist_type/DVR/sparams/expire%2Cei%2Cip%2Cid%2Csource%2Crequiressl%2Chfr%2Cplaylist_duration%2Cmanifest_duration%2Cmaxh%2Cmaudio%2Cspc%2Cvprv%2Cgo%2Citag%2Cplaylist_type/sig/AOq0QJ8wRAIgVev848DuWm7jaG0KWoX4Ta-yc1S6lnuPZqBiYZC3dOcCID4MWvWgxLCVFoGIseulNI_aViIWk1Xl9i3Q7-4KTTdv/file/index.m3u8','outputfile.mp3'])
d = Download_Stream()
d.start()
time.sleep(20)
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,channels=2,rate=44100,output=True,frames_per_buffer=4096)
stream.start_stream()
def read_stream(thefile):
while True:
chunk = thefile.read(4096)#change this to 32*4096
if not chunk:
print("Not chunk")
time.sleep(0.1)
continue
else:
yield chunk
mp3_stream_file = open("outputfile.mp3","rb")
chunks = read_stream(mp3_stream_file)
for chunk in chunks:
chunk = BytesIO(chunk)
slice = AudioSegment.from_mp3(chunk)
stream.write(slice.raw_data)
the script works but there is a delay between the chunks so i decided to change the chunk size from 4096 to 32*4096. Now this is better i think but it be a small freeze delay between the chunks.