tombulled / innertube

Python Client for Google's Private InnerTube API. Works with YouTube, YouTube Music and more!
https://pypi.org/project/innertube/
MIT License
257 stars 18 forks source link

how to play the link got from yt music #61

Open Mrithul-E opened 7 months ago

Mrithul-E commented 7 months ago

how to play the link got from yt music

please say how to use this lib for yt music , i want to get the music url

i got this url :

"streamingData": {
    "expiresInSeconds": "21540",
    "formats": [
      {
        "itag": 18,
        "mimeType": "video/mp4; codecs=\"avc1.42001E, mp4a.40.2\"",
        "bitrate": 385054,
        "width": 640,
        "height": 360,
        "lastModified": "1679177037392938",
        "quality": "medium",
        "fps": 25,
        "qualityLabel": "360p",
        "projectionType": "RECTANGULAR",
        "audioQuality": "AUDIO_QUALITY_LOW",
        "approxDurationMs": "312099",
        "audioSampleRate": "44100",
        "audioChannels": 2,
        "signatureCipher": "s=DQng-n%3DM3r_wNoI4iPeZbVvXMDff5jLP00x-pAU1b6TrAEiAp0dlW5Qn5OKADxYz82wDiijP36LbJcmfZGSdwCK9fRIAhIgRwAgewLNAZAZ&sp=sig&url=https://rr3---sn-cnoa-jv3l.googlevideo.com/videoplayback%3Fexpire%3D1699905706%26ei%3DSixSZcruG_zd4-EPieyh4A0%26ip%3D117.230.27.134%26id%3Do-AHlq_5mSFdDDumCYuy8Xjg5LyeELW7BIiE9naezMfuvh%26itag%3D18%26source%3Dyoutube%26requiressl%3Dyes%26mh%3Drj%26mm%3D31%252C29%26mn%3Dsn-cnoa-jv3l%252Csn-h557snsl%26ms%3Dau%252Crdu%26mv%3Dm%26mvi%3D3%26pl%3D23%26initcwndbps%3D93750%26vprv%3D1%26mime%3Dvideo%252Fmp4%26ns%3DWxFiRiu_A85I_3zbvD1k-LYP%26cnr%3D14%26ratebypass%3Dyes%26dur%3D312.099%26lmt%3D1679177037392938%26mt%3D1699883593%26fvip%3D2%26fexp%3D24007246%26beids%3D24350018%26c%3DWEB_MUSIC%26txp%3D5538434%26n%3D3V3z5f-VGEaUEV6%26sparams%3Dexpire%252Cei%252Cip%252Cid%252Citag%252Csource%252Crequiressl%252Cvprv%252Cmime%252Cns%252Ccnr%252Cratebypass%252Cdur%252Clmt%26lsparams%3Dmh%252Cmm%252Cmn%252Cms%252Cmv%252Cmvi%252Cpl%252Cinitcwndbps%26lsig%3DAM8Gb2swRQIhAIHiYEYuNk0HSCdtBAY-RL5SrAMZ8MjxWoRSJ-MNoPB8AiAoAXG6AjN9pk6T93lHKIKZHAK0pZucXPSwFZLMUWEfqA%253D%253D"
      }
    ]

but the link from this is not playable andthe link after sig&url= is also not working

how to play the music

tombulled commented 4 months ago

Hi @Mrithul-E, thanks for opening this issue. I'm assuming you were using either the WEB or WEB_REMIX client to get the above streaming data? If so, YouTube employs a "bot guard" which means the stream is encoded/encrypted in such a way that it makes it hard to extract a usable URL. The community has been really good at reverse engineering the mechanism YouTube uses for this, so it is possible to decode. A few good references for this are yt-dlp and NewPipe.

The easiest solution is usually just to use a non-web client to fetch the streaming urls. For example, if you hit the /player endpoint as IOS or IOS_MUSIC (or ANDROID or ANDROID_MUSIC) you should get streamable URLs.

Hope that helps!

Mrithul-E commented 3 months ago

Very thanks!, but I am so late to see this replay from @tombulled ... and now I started using pytube for decrypting the url

we can simply pass the youtube video url to pytube's Youtube() class and access the json return from youtube using streaming_data function inside Youtube class of pytube

eg:

yt=YouTube("https://www.youtube.com/watch?v="+j)
json_respo = yt.streaming_data

The urls stored in json_respo can used straight forward

Mrithul-E commented 3 months ago

I cant open the like got from 'IOS or ANDROID_MUSIC`

tombulled commented 2 months ago

I wrote an example script a while back to demo fetching the streaming URLs, for example:

$ python examples/list-streams-for-video.py
[{'approxDurationMs': '212040',
  'averageBitrate': 3024555,
  'bitrate': 4534636,
  'contentLength': '80165852',
  'fps': 25,
  'height': 1080,
  'indexRange': {'end': '1241', 'start': '742'},
  'initRange': {'end': '741', 'start': '0'},
  'itag': 137,
  'lastModified': '1706255300792263',
  'mimeType': 'video/mp4; codecs="avc1.640028"',
  'projectionType': 'RECTANGULAR',
  'quality': 'hd1080',
  'qualityLabel': '1080p',
  'url': '<STREAMABLE URL HERE>',
  'width': 1920},
  ...
]

I've replaced the URL in the above example output with <STREAMABLE URL HERE> as the URL contains information such as the requestor's IP address. You should be able to download these URLs or view them in your browser. Importantly I believe you have to make HTTP requests for these links from the same IP address that requested them, otherwise they will not work