gamdl (Glomatico's Apple Music Downloader)

repository·main·Indexed 23 days ago

https://github.com/glomatico/gamdl

A command-line application and Python library for downloading high-quality Apple Music content, including AAC/ALAC songs, 4K music videos, and synced lyrics. It supports downloading songs, albums, playlists, artists, and post videos. The tool features integration with Wrapper v2 for ALAC and advanced requests, and supports N_m3u8DL-RE for faster video downloads. It requires an active Apple Music subscription and Netscape format cookies for authentication.

Tokens
8.3K
Snippets
7
Records
34
Agent score
82%

What's inside gamdl

  1. Configure file naming with Template Variables

    main

    Gamdl uses template variables to dynamically name folders and files. These can be used in various --template options and --exclude-tags.

    Available Tags for Templates:

    • album, album_artist, album_id
    • artist, artist_id
    • composer, composer_id
    • date (supports strftime format: {date:%Y})
    • disc, disc_total
    • media_type
    • playlist_artist, playlist_id, playlist_title, playlist_track
    • title, title_id
    • track, track_total

    Available Tags for exclude-tags only:

    • album_sort, artist_sort, composer_sort, title_sort
    • comment, compilation, copyright, cover, gapless, genre, genre_id, lyrics, rating, storefront, xid
    • all (special: skip all tagging)
  2. Use the Wrapper for ALAC and advanced requests

    main

    The Wrapper v2 server is recommended when downloading songs using the alac codec, as standard API limitations may prevent it from working without the wrapper. Using the wrapper also allows you to skip providing cookies, as it handles account and playback requests.

    Configuration Options:

    • --use-wrapper or use_wrapper = true: Enables the wrapper.
    • --wrapper-url or wrapper_url: Configures the HTTP endpoint for account/playback calls.
    • --wrapper-decrypt-host / --wrapper-decrypt-port: Configures WV2D batch TCP decryption.

    Note: Newer wrapper-v2 builds use HTTP JSON for account/playback and WV2D batch TCP on port 10020 for decryption.

  3. Use N_m3u8DL-RE for faster video downloads

    main

    You can use N_m3u8DL-RE as a faster alternative to the default yt-dlp download mode.

    Configuration Options:

    • --download-mode nm3u8dlre or download_mode = nm3u8dlre: Enables N_m3u8DL-RE mode.
    • --nm3u8dlre-path or nm3u8dlre_path: Specifies the path to the N_m3u8DL-RE executable if it is not in your system PATH.
    • --ffmpeg-path or ffmpeg_path: Specifies the path to the FFmpeg executable if it is not in your system PATH.
  4. Configure Apple Music cookies

    main

    Gamdl requires an active Apple Music subscription and exported browser cookies in Netscape format to authenticate.

    To export cookies while logged in at Apple Music:

    Once exported, you can use them by placing the file in your working directory as cookies.txt or by specifying the path via --cookies-path or the cookies_path config key.

  5. Configure Gamdl using a config file

    main

    Gamdl can be configured via a config.ini file. The file is automatically created on the first run. Command-line arguments will override any values set in the config file.

    Default config file locations:

    • Linux: ~/.gamdl/config.ini
    • Windows: %USERPROFILE%\ .gamdl\config.ini

    You can specify a custom location using the --config-path flag.

  6. Download songs, albums, and artists with Gamdl

    main

    Gamdl is a CLI tool for downloading various Apple Music content types including songs, albums, playlists, music videos, artists, post videos, and Apple Music Classical content.

    Basic Syntax:

    gamdl [OPTIONS] URLS...

    Examples:

    Download a song:

    gamdl "https://music.apple.com/us/album/never-gonna-give-you-up-2022-remaster/1624945511?i=1624945512"

    Download an album:

    gamdl "https://music.apple.com/us/album/whenever-you-need-somebody-2022-remaster/1624945511"

    Download from an artist:

    gamdl "https://music.apple.com/us/artist/rick-astley/669771"
  7. Embed Gamdl as a Python library

    main

    You can integrate Gamdl into your own Python projects by using the gamdl.api and gamdl.downloader modules. The workflow involves:

    1. Creating an AppleMusicApi instance (e.g., using create_from_netscape_cookies).
    2. Initializing an AppleMusicBaseInterface.
    3. Composing specialized interfaces (AppleMusicSongInterface, AppleMusicMusicVideoInterface, AppleMusicUploadedVideoInterface) into a main AppleMusicInterface.
    4. Initializing an AppleMusicBaseDownloader using that interface.
    5. Composing specialized downloaders into a main AppleMusicDownloader.
    6. Using downloader.get_download_item_from_url(url) to resolve media items and downloader.download(download_item) to perform the download.
    import asyncio
    
    from gamdl.api import AppleMusicApi
    from gamdl.downloader import (
        AppleMusicBaseDownloader,
        AppleMusicDownloader,
        AppleMusicMusicVideoDownloader,
        AppleMusicSongDownloader,
        AppleMusicUploadedVideoDownloader,
    )
    from gamdl.interface import (
        AppleMusicBaseInterface,
        AppleMusicInterface,
        AppleMusicMusicVideoInterface,
        AppleMusicSongInterface,
        AppleMusicUploadedVideoInterface,
    )
    
    
    async def main():
        # Create AppleMusicApi instance from cookies
        apple_music_api = await AppleMusicApi.create_from_netscape_cookies(
            cookies_path="cookies.txt",
        )
    
        # Check subscription
        if not apple_music_api.active_subscription:
            print("No active Apple Music subscription")
            return
    
        # Create base interface
        base_interface = await AppleMusicBaseInterface.create(
            apple_music_api=apple_music_api,
        )
    
        # Create specialized interfaces
        song_interface = AppleMusicSongInterface(
            base=base_interface,
        )
        music_video_interface = AppleMusicMusicVideoInterface(
            base=base_interface,
        )
        uploaded_video_interface = AppleMusicUploadedVideoInterface(
            base=base_interface,
        )
    
        # Create main interface
        interface = AppleMusicInterface(
            song=song_interface,
            music_video=music_video_interface,
            uploaded_video=uploaded_video_interface,
        )
    
        # Create base downloader
        base_downloader = AppleMusicBaseDownloader(
            interface=interface,
        )
    
        # Create specialized downloaders
        song_downloader = AppleMusicSongDownloader(base=base_downloader)
        music_video_downloader = AppleMusicMusicVideoDownloader(
            base=base_downloader,
        )
        uploaded_video_downloader = AppleMusicUploadedVideoDownloader(base=base_downloader)
    
        # Create main downloader
        downloader = AppleMusicDownloader(
            song=song_downloader,
            music_video=music_video_downloader,
            uploaded_video=uploaded_video_downloader,
        )
    
        # Download from URL
        url = "https://music.apple.com/us/album/never-gonna-give-you-up-2022-remaster/1624945511?i=1624945512"
    
        download_queue = []
        async for media in downloader.get_download_item_from_url(url):
            download_queue.append(media)
    
        for download_item in download_queue:
            try:
                await downloader.download(download_item)
            except Exception as e:
                print(f"Error downloading: {e}")
    
    
    if __name__ == "__main__":
        asyncio.run(main())
  8. Install Gamdl via pip

    main

    Install the Gamdl command-line application using pip:

    pip install gamdl

    After installation, you must provide Apple Music cookies in Netscape format. You can either:

    1. Place a file named cookies.txt in your current working directory.
    2. Specify the path to your cookies file using the --cookies-path flag or the cookies_path configuration option.
  9. Manage gamdl configuration via ConfigFile

    main

    The gamdl CLI uses a configuration file (typically an INI-style file) to persist settings. The ConfigFile class manages the lifecycle of these settings, including loading them from disk, merging them with command-line arguments, and saving default values for available parameters.

    Key Behaviors

    • Automatic Sectioning: Settings are stored under a specific section, which defaults to [gamdl].
    • Parameter Merging: When running a command, the CLI follows a priority order:
      1. Command-line arguments (highest priority)
      2. Configuration file values
      3. Default parameter values (lowest priority)
    • Persistence: The CLI can automatically write default values for all supported parameters into your configuration file if they are missing, ensuring your config file stays up-to-date with the current version of the tool.
    • Cleanup: The load() process automatically removes keys from your configuration file that are no longer recognized by the current version of the CLI command, preventing clutter from deprecated options.
  10. Use the Gamdl CLI to download Apple Music content

    main

    Gamdl is a command-line tool used to download songs, music videos, and uploaded videos from Apple Music. The CLI supports downloading from direct URLs or from text files containing multiple URLs. It can be configured via command-line arguments or a configuration file.

    Key features include:

    • Support for multiple media types (songs, music videos, uploaded videos).
    • Integration with a WrapperApi for enhanced decryption capabilities.
    • Support for Netscape-format cookies for authentication.
    • Database integration to track downloaded items and apply filters.
    • Customizable output paths and folder templates.

    To view available options, use the --help flag.

  11. Configure gamdl using a configuration file

    main

    You can provide a custom path to a configuration file using the --config-path flag (implied by the loader decorator logic). To ignore the configuration file and use only command-line arguments and defaults, use the --no-config-file flag.

    Configuration File Format

    The configuration file uses the standard INI format. Settings are grouped under the [gamdl] section.

    [gamdl]
    param_name = value
    csv_param = item1,item2,item3
    boolean_param = true

    Supported Data Types in Config

    • Booleans: Represented as true or false.
    • CSV/Lists: Represented as comma-separated values (e.g., val1,val2).
    • Nulls: Represented by the literal string null to indicate a None value.
    • Strings/Ints/Paths: Standard string representations.