syncedlyrics

repository·main·Indexed 19 days ago

https://github.com/moehmeni/syncedlyrics

A tool and Python library for retrieving LRC format (synchronized) lyrics for music. It supports multiple providers including Musixmatch, Lrclib, NetEase, Megalobiz, and Genius. Features include a CLI for direct searching, a Python API via syncedlyrics.search(), translation support, and word-level (karaoke) enhanced lyrics.

Tokens
3.5K
Snippets
16
Records
19
Agent score
60%

What's inside syncedlyrics

  1. Use syncedlyrics.search() in Python

    main

    The primary entry point for the library is the syncedlyrics.search() function. It accepts a search term (e.g., "[TRACK_NAME] [ARTIST_NAME]") and several optional parameters to control behavior such as language, providers, and output format.

    import syncedlyrics
    
    # Basic search
    lrc = syncedlyrics.search("[TRACK_NAME] [ARTIST_NAME]")
    
    # Search with specific options
    syncedlyrics.search("search term", plain_only=True, save_path="{search_term}_1234.lrc", providers=["NetEase"])
    
    # Get translation (original and translation separated by \n)
    syncedlyrics.search("search term", lang="de")
    
    # Get word-by-word (karaoke) enhanced lyrics if available
    syncedlyrics.search("search term", enhanced=True)
  2. Reference the syncedlyrics CLI options

    main

    The following flags are available for the syncedlyrics command line interface:

    FlagDescription
    -oPath to save .lrc lyrics, default="{search_term}.lrc"
    -pSpace-separated list of providers to include in searching
    -lLanguage code of the translation (ISO 639-1 format)
    -vUse this flag to show the logs
    --plain-onlyOnly look for plain text (not synced) lyrics
    --synced-onlyOnly look for synced lyrics
    --enhancedSearches for an Enhanced (word-level karaoke) format. If it isn't available, search for regular synced lyrics.
  3. Save lyrics to a file using save_path

    main

    You can instruct search() to automatically save the retrieved lyrics to a file by providing a save_path. The path supports a {search_term} placeholder which will be replaced by the actual search string used in the function call.

    Example usage with a placeholder:

    import syncedlyrics
    # This will save to a file named 'Bohemian_Rhapsody_Queen.lrc'
    syncedlyrics.search("Bohemian Rhapsody Queen", save_path="{search_term}.lrc")
    import syncedlyrics
    syncedlyrics.search("Bohemian Rhapsody Queen", save_path="{search_term}.lrc")
  4. Use the syncedlyrics CLI

    main

    You can search for lyrics directly from the command line. By default, the CLI prefers time-synced lyrics but falls back to plaintext if no synced lyrics are found. Use --plain-only or --synced-only to restrict the search type.

    syncedlyrics "SEARCH_TERM"
  5. Use the search() function in Python

    main

    The search function is the core API for finding lyrics programmatically. It returns the lyrics string if found, or None if no results match the criteria.

    from syncedlyrics import search
    
    lrc = search(
        "track name",
        plain_only=False,
        synced_only=False,
        output="track.lrc",
        p="lrclib musixmatch",
        lang="en",
        enhanced=True
    )
    
    if lrc:
        print(lrc)
  6. Search for synced lyrics with search()

    main

    The search() function is the primary entrypoint for retrieving lyrics in LRC (synchronized) format. It searches through available providers and returns the lyrics as a string. If a save_path is provided, the lyrics will be saved to a .lrc file.

    Arguments:

    • search_term (str): The track name and artist name used to find the song.
    • plain_only (bool): If True, the function only looks for plain text lyrics (not synced).
    • synced_only (bool): If True, the function only looks for synced lyrics.
    • save_path (Optional[str]): The file path where the .lrc file should be saved. You can use {search_term} as a placeholder in the path string. If None, no file is saved.
    • providers (List[str]): A list of provider names to restrict the search to. If empty, all providers are used.
    • lang (Optional[str]): The language for translation. Note: Only supported by Musixmatch.
    • enhanced (bool): If True, returns word-by-word (karaoke) synced lyrics if available. Note: Only supported by Musixmatch.

    Returns:

    • The lyrics as a string in LRC format, or None if no suitable lyrics are found.
    import syncedlyrics
    lrc_text = syncedlyrics.search("[TRACK_NAME] [ARTIST_NAME]")
  7. Configure Musixmatch-specific options in search()

    main

    When using the search() function, certain parameters are exclusive to the Musixmatch provider. If you provide these parameters but the search does not use Musixmatch (or if Musixmatch fails), the behavior may be limited:

    • lang: Used to request a specific language for translation along with the original lyrics.
    • enhanced: Used to request word-by-word (karaoke) synced lyrics.

    If lang is provided and an error occurs during the search process, the function may abort and return None because the requested language feature is provider-specific.

  8. Convert synced lyrics to plaintext with synced_to_plaintext

    main

    The synced_to_plaintext function removes LRC timestamps (e.g., [00:12.34] ) from a string, leaving only the raw text.

    from syncedlyrics.utils import synced_to_plaintext
    
    synced = "[00:12.00] Hello world"
    plain = synced_to_plaintext(synced)
    # plain == "Hello world"
  9. Get or create a cache directory with get_cache_path

    main

    Use get_cache_path to retrieve a platform-specific directory for storing cached data. It automatically handles paths for Windows (LOCALAPPDATA), macOS (~/Library/Caches), and Linux (~/.cache).

    • lib_name: The name of the subdirectory to create (defaults to "syncedlyrics").
    • auto_create: If True, the directory will be created if it doesn't exist.
    from pathlib import Path
    from syncedlyrics.utils import get_cache_path
    
    cache_dir = get_cache_path(lib_name="my_app", auto_create=True)
    print(cache_dir)  # Returns a Path object