Skip to content

Instantly share code, notes, and snippets.

@cheadrian
Last active August 13, 2024 16:03
Show Gist options
  • Save cheadrian/b661fb68a6a87ea64069e641cef68c3e to your computer and use it in GitHub Desktop.
Save cheadrian/b661fb68a6a87ea64069e641cef68c3e 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
import os
#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).
#TO JOIN PARTS with ffmpeg, check this comment
#https://gist.github.com/cheadrian/b661fb68a6a87ea64069e641cef68c3e#gistcomment-3495351
os.mkdir("vid", 0o666)
os.mkdir("aud", 0o666)
for i in range(1, 5000): #Change 5000 according to video lenght and 1 to where you want to start
vide_dow = vid_link + str(i)
sound_dow = sound_link + str(i)
name_vid = "vid/" + str(i) + ".mp4"
name_sound = "aud/" + str(i) + ".m4a"
urllib.request.urlretrieve(vide_dow, name_vid)
urllib.request.urlretrieve(sound_dow, name_sound)
print("Downloaded part ", i)
@ekardon
Copy link

ekardon commented May 9, 2021

@mrwnwttk you saved me, and now I owe you a meal or two. Your merge.py script actually worked just fine on the chunks I had downloaded with this here script. Thank you for sharing! 👍

This is what I did:

  1. Create directory mkdir segments_AAAAAAAAAAA
  2. Make symbolic links to chunks in that directory:
    cp -s /path/to/vid/* segments_AAAAAAAAAAA
    cp -s /path/to/aud/* segments_AAAAAAAAAAA
  3. Rename the symbolic links to a format expected by the merge script:
    12345.mp4 -> 12345_AAAAAAAAAAA_video.ts
    12345.m4a -> 12345_AAAAAAAAAAA_audio.ts
    with these commands:
perl-rename 's/(\d*)\.m4a/$1_AAAAAAAAAAA_audio\.ts/' *
perl-rename 's/(\d*)\.mp4/$1_AAAAAAAAAAA_video\.ts/' *
  1. Call the merge.py script like this:
$> merge.py https://www.youtube.com/watch\?v\=AAAAAAAAAAA
[...]
[WARNING] File of method 1 broken.
[INFO] using method 2 for this livestream. This process might take a while...
Output file: AAAAAAAAAAA.mp4

And the file works great! No messy timestamp shenanigans anymore!
Added a small shell script to automate the process described above.

it doesn't work for me. neither vlc player nor gomplayer is able to play output file. vlc player reports 2400 hours video but its only 2 hours and 40 minutes.

@glubsy
Copy link

glubsy commented May 9, 2021

it doesn't work for me. neither vlc player nor gomplayer is able to play output file. vlc player reports 2400 hours video but its only 2 hours and 40 minutes.

I observed this problem too before, as I mentioned in a previous message. Not sure why it wouldn't work for you. Report issues related to the program you used in the proper issue tracker, and don't forget to include details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment