2

I get youtube video download link from youtube-dl.

Example Link: https://r4---sn-hgn7rne7.googlevideo.com/videoplayback?id=o-AFAhZy_ewhPuT9l2wWK4OXaCBsUmBIWX0EqumOSUHzBT&itag=140&source=youtube&requiressl=yes&pl=22&ei=zirdXPuFL6uM8gSPj7ygCg&mime=audio%2Fmp4&gir=yes&clen=4081843&dur=252.168&lmt=1557823006263504&fvip=6&keepalive=yes&c=WEB&txp=5531432&ip=2604%3Aa880%3A800%3Ac1%3A%3Afd%3A0&ipbits=0&expire=1558019887&sparams=clen,dur,ei,expire,gir,id,ip,ipbits,itag,lmt,mime,mip,mm,mn,ms,mv,pl,requiressl,source&key=cms1&signature=1F0A1261C8B008BB96D33F3268C673FD171790CA.7F63590873FF47EC4E05D28F74D6684CCC1154C1&ratebypass=yes&utmg=ytap1&title=Sezen_Aksu_-_Aktan_Ne_Haber_Official_Audio.m4a&cms_redirect=yes&mip=46.196.21.103&mm=31&mn=sn-hgn7rne7&ms=au&mt=1557998299&mv=m

When I download it from any browser it downloads in 5 seconds but when I try to download it from python, the link downloads in 1 minute. I tried same size in different networks but I got same result.

For Python I use this:

   import urllib
   urllib.urlretrieve(url,"file.m4a")

I don't know google expects me any header or something else to make download faster.

hinciler
  • 161
  • 1
  • 11
  • 1
    Side Note: If you want to publish this app to the PlayStore it will likely get rejected because of a violation of the YouTube API. [link](https://developers.google.com/youtube/terms/api-services-terms-of-service#agreement) – bautista May 16 '19 at 11:21
  • @bautista it was just example. I removed android code to prevent misunderstanding. – hinciler May 16 '19 at 11:43

1 Answers1

0

I suspect your browser has the file cached, or is using a proxy which caches the file. I tried downloading a file from youtube and got a similar duration in three different ways.

# Note this is how to get the youtube url: `youtube-dl -f 22 -g https://www.youtube.com/watch?v=qxlVTsFbyKs`

url = 'https://r3---sn-tt1e7n7e.googlevideo.com/videoplayback?id=o-APMpsV_ubZwYnre71FtKIY7rTKTd1HmAGqjFS7D_W9vO&itag=22&source=youtube&requiressl=yes&mm=31%2C29&mn=sn-tt1e7n7e%2Csn-tt1eln7s&ms=au%2Crdu&mv=m&pl=25&ei=g-LdXNT1H-ODir4P0Jeb0Ag&initcwndbps=2695000&mime=video%2Fmp4&ratebypass=yes&dur=91.649&lmt=1556128078557211&mt=1558045252&fvip=3&c=WEB&txp=2216222&ip=184.75.215.122&ipbits=0&expire=1558066915&sparams=ip%2Cipbits%2Cexpire%2Cid%2Citag%2Csource%2Crequiressl%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cei%2Cinitcwndbps%2Cmime%2Cratebypass%2Cdur%2Clmt&signature=481807C3CAD81BC6CFA6E4131D5E734BC2CB63FC.2EBF166C597725BC02E9EE28227B9A9C815E224C&key=yt8'

# 60 seconds
import urllib.request
urllib.request.urlretrieve(url,"file.mp4")

# 50 seconds
import urllib.request
data = urllib.request.urlopen(url).read()
open('out.mp4', 'wb').write(data)

# 50 seconds
import subprocess
subprocess.check_call(["curl", "-L", url, "--output", "out4.mp4"])

To disable your caches on the browser, e.g. chrome, you can try Disabling Chrome cache for website development

ubershmekel
  • 9,570
  • 6
  • 62
  • 78
  • When I try unique video to download which is not on cache, it still downloads in few seconds. – hinciler May 16 '19 at 22:54
  • Can you check how long it takes to download when you use `curl`? – ubershmekel May 17 '19 at 07:03
  • Same as python. It is about between 1 and 2 minutes for 4mb m4a file. I don't have any problem with videos but all audio files have this problem. – hinciler May 17 '19 at 08:34
  • To check if it might have to do with headers - you can open Chrome's developer tools, click the network tab, and look at the specific request that seems fast to you. From there you right-click the request -> `copy as cURL` then paste that in a terminal. This will let you use the exact same headers as Chrome did. – ubershmekel May 17 '19 at 19:23