Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Lyrics w. Timestamps #662 #693

Merged
merged 16 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added Lyrics w. Timestamps
Added `get_lyrics_with_timestamps` to get lyrics with timestamps.
The Method doesn't try to parse the response as normal lyrics, if no lyrics with timestamps are returned. (could be changed to do so tho, the format is the same)
heinrich26 authored and sigma67 committed Dec 17, 2024
commit 9cfc67c59f29ca8207ba153b4336b5c522755ab0
59 changes: 59 additions & 0 deletions ytmusicapi/mixins/browsing.py
Original file line number Diff line number Diff line change
@@ -865,6 +865,65 @@ def get_lyrics(self, browseId: str) -> dict:

return lyrics

def get_lyrics_with_timestamps(self, browseId: str) -> dict:
"""
Returns lyrics of a song with timestamps, if available.
If no timestaps are given, this method won't replicate the behavior of `get_lyrics`!

:param browseId: Lyrics browse id obtained from `get_watch_playlist` (startswith `MPLYt`)
:return: Dictionary with song lyrics.

Example::

{
"lyrics": [
{
"lyricLine": "I was a liar",
"cueRange": {
"startTimeMilliseconds": "9200",
"endTimeMilliseconds": "10630",
"metadata": {
"id": "1"
}
}
},
{
"lyricLine": "I gave in to the fire",
"cueRange": {
"startTimeMilliseconds": "10680",
"endTimeMilliseconds": "12540",
"metadata": {
"id": "2"
}
}
},
],
"source": "Source: LyricFind"
}

"""
lyrics = {}
if not browseId:
raise YTMusicUserError(
"Invalid browseId provided. This song might not have lyrics.")

# change the Client to get lyrics with timestamps (mobile only)
copied_context_client = self.context["context"]["client"].copy()
self.context["context"]["client"].update({
"clientName": "ANDROID_MUSIC",
"clientVersion": "7.21.50"
})
response = self._send_request("browse", {"browseId": browseId})
# restore the old context
self.context["context"]["client"] = copied_context_client

base_path = ["contents", "elementRenderer", "newElement", "type",
"componentType", "model", "timedLyricsModel", "lyricsData"]
lyrics["lyrics"] = nav(response, [*base_path, "timedLyricsData"], True)
lyrics["source"] = nav(response, [*base_path, "sourceMessage"], True)

return lyrics

def get_basejs_url(self):
"""
Extract the URL for the `base.js` script from YouTube Music.