Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scresante/daa514a4088e71641beca1375303d78b to your computer and use it in GitHub Desktop.
Save scresante/daa514a4088e71641beca1375303d78b to your computer and use it in GitHub Desktop.
Download Youtube Live streamed video from the start or selected time
#With the help of this script you can download parts from the Youtube Video that is live streamed, from start of the stream till the end
import urllib.request
try:
from tqdm import tqdm
except ImportError:
print('consider installing tqdm for a nice progress bar')
#E.G: "https://r4---sn-gqn-p5ns.googlevideo.com/videoplayback?expire=1603041842& ..... 2.20201016.02.00&sq="
#The sound link should contain: &mime=audio in it.
#Here's an example from NASA LIVE:
#VIDEO: https://r5---sn-gqn-p5ns.googlevideo.com/videoplayback?expire=1603165657&ei=eQmOX8TeFtS07gO1xLWwDA&ip=79.115.11.159&id=DDU-rZs-Ic4.1&itag=137&aitags=133%2C134%2C135%2C136%2C137%2C160&source=yt_live_broadcast&requiressl=yes&mh=PU&mm=44%2C29&mn=sn-gqn-p5ns%2Csn-c0q7lnsl&ms=lva%2Crdu&mv=m&mvi=5&pl=20&initcwndbps=1350000&vprv=1&live=1&hang=1&noclen=1&mime=video%2Fmp4&gir=yes&mt=1603143920&fvip=5&keepalive=yes&fexp=23915654&c=WEB&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cvprv%2Clive%2Chang%2Cnoclen%2Cmime%2Cgir&sig=AOq0QJ8wRQIgQMnxy1Yk3HLTpqbOGmjZYH1CXCTNx6u6PgngAVGi4EQCIQDWyaye-u_KGyVQ0HRUsyKVaAzyXbmzDqOGVGpIyP7VtA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgR5QVZh23NcLE2nRpo5IT-axGEfUCJrXKMmJHjXQdkCYCIFLsIFacvPpy98zaNSB0RfXswacyc-Ru3sYeEjTFym43&alr=yes&cpn=LlPCcTsE_3Xao9Xh&cver=2.20201016.02.00&sq=2504043&rn=13&rbuf=21958
#AUDIO: https://r5---sn-gqn-p5ns.googlevideo.com/videoplayback?expire=1603165657&ei=eQmOX8TeFtS07gO1xLWwDA&ip=79.115.11.159&id=DDU-rZs-Ic4.1&itag=140&source=yt_live_broadcast&requiressl=yes&mh=PU&mm=44%2C29&mn=sn-gqn-p5ns%2Csn-c0q7lnsl&ms=lva%2Crdu&mv=m&mvi=5&pl=20&initcwndbps=1350000&vprv=1&live=1&hang=1&noclen=1&mime=audio%2Fmp4&gir=yes&mt=1603143920&fvip=5&keepalive=yes&fexp=23915654&c=WEB&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cvprv%2Clive%2Chang%2Cnoclen%2Cmime%2Cgir&sig=AOq0QJ8wRAIgWFTZLV1G33cKJoitlK7dUgNg1KuXyvC6F9F7Lc6x3gcCIHaGjehjvVAjUd6cqMnTLtBq9pPRfQWXM3bwI1qQYqpx&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRAIgR5QVZh23NcLE2nRpo5IT-axGEfUCJrXKMmJHjXQdkCYCIFLsIFacvPpy98zaNSB0RfXswacyc-Ru3sYeEjTFym43&alr=yes&cpn=LlPCcTsE_3Xao9Xh&cver=2.20201016.02.00&sq=2504045&rn=20&rbuf=17971
#Use VLC to play the parts. ffmpeg to re-encode / re-mux and then concatenate.
vid_link = "VIDEO LINK THE END -> &sq="
sound_link = "AUDIO LINK THE END -> &sq= "
#Each part should be equivalent to 5 seconds of video
#Please note if what you got on the your link from the sq parameter looks like this &sq=2504043 don't expect
#the script to work with 1, 2504043, because the first part probably already expired.
#Try to download apropiate parts number, like range(2501043, 2504043).
def get_full_livestream(vid_link, sound_link):
'''run this to download a livestream to files'''
# the sequence numbers to begin and end at
begin = 12000
end = 12300
video_out = 'output.mp4'
audio_out = 'output.m4a'
try:
with open(video_out, 'wb') as video, open(audio_out, 'wb') as audio:
# if you don't have tqdm, swap the comments on the next two lines
for i in tqdm(range(begin, end)):
# for i in range(begin, end):
vide_dow = f'{vid_link}{i}'
sound_dow = f'{sound_link}{i}'
with urllib.request.urlopen(vide_dow) as v_response,\
urllib.request.urlopen(sound_dow) as a_response:
video.write(v_response.read())
audio.write(a_response.read())
#print(f"Downloaded part {i}")
except urllib.error.URLError as e:
print(f'network error {e.reason}')
except (IOError) as e:
print(f'file error: {e}')
get_full_livestream(vid_link, sound_link)
##
##
####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment