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)
@mrwnwttk
Copy link

mrwnwttk commented Dec 7, 2020

@scresante I've had at least two people report that exact same issue to me, but all it means is that the script can grab the web page, but is unable to extract any information about the video stream. I'll have to look into that, until then maybe try passing it a cookie file as described in the README?

@glubsy
Copy link

glubsy commented Dec 20, 2020

@Linden10 nope sadly I get the same problem with your snippets. I get a bunch of "non-monotonous DTS" errors (for each chunk it seems).

$> ffmpeg -i concat_audio.ts -c:a copy audio.m4a
[...]
[ipod @ 0x55995b908700] Non-monotonous DTS in output stream 0:0; previous: 117188020, current: 12039168; changing to 117188021. This may result in incorrect timestamps in the output file. 
$> ffmpeg -i concat_video.ts -c:a copy video.m4a

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'concat_video.ts':
  Metadata:
    creation_time   : 2020-12-20T01:01:05.000000Z
    minor_version   : 0
    major_brand     : dash
    compatible_brands: iso6avc1mp41
  Duration: 00:59:39.47, start: 1276.000000, bitrate: 770 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 854x480 [SAR 1:1 DAR 427:240], 637 kb/s, 30 fps, 30 tbr, 90k tbn, 60 tbc (default)
    Metadata:
      creation_time   : 2020-12-20T01:01:05.000000Z
      handler_name    : ISO Media file produced by Google Inc. Created on: 12/19/2020.
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x56039edd3740] Found duplicated MOOV Atom. Skipped it
    Last message repeated 3143 times
[...]
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x56039edd3740] DTS 37530000 < 114927030 out of order
More than 1000 frames duplicated       0kB time=00:00:23.10 bitrate=   0.0kbits/s dup=719 drop=28 speed=16.7x    
More than 10000 frames duplicated  12288kB time=00:37:52.03 bitrate=  44.3kbits/s dup=68129 drop=2387 speed=18.4x    
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x56039edd3740] DTS 177120000 < 320757030 out of order dup=68636 drop=32134 speed=16.9x    
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x56039edd3740] DTS 181440000 < 315717030 out of order dup=68814 drop=67655 speed=15.5x    
frame=69104 fps=437 q=-1.0 Lsize=   13449kB time=00:38:23.36 bitrate=  47.8kbits/s dup=68814 drop=92432 speed=14.6x    
video:12640kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 6.403555%

Shame, because youtube-dl(c) actually seems to download the stream, but I run into issues after the stream has ended as it does not stop looping, and killing the ffmpeg process breaks the resulting file. Perhaps I'll have to hack around @mrwnwttk's script to make it work with my files.

Edit: I believe that youtube-dl issue (ref 1, ref 2) might be related to this ffmpeg ticket and this ffmpeg ticket.

@glubsy
Copy link

glubsy commented Dec 21, 2020

@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.

@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