Coder Social home page Coder Social logo

youtube-music-library-exporter's Introduction

YouTube Music Library Exporter

This tiny project allows you to download in a more reliable way your YouTube Music library. I tried many times to export all my uploads using Google Takeout but songs were missing and the export is unbelieviably messy.

With the help of yt-dlp, I put together several scripts that will semi-automate the export process.

Features

  • Parse and format your library's song list metadata
  • Download all songs (embeds song title, artist, album, and thumbnail)
  • Resume-able download script
  • Songs pre-clustering (by album)

Tested on macOS/PHP 8.1.

Installation

Clone this repository anywhere:

cd ~/Music
git clone [email protected]:eexit/youtube-music-library-exporter.git
cd youtube-music-library-exporter

Install dependencies (assuming you have homebrew installed):

brew install yt-dlp/taps/yt-dlp ffmpeg php composer
composer install

Build the metadata file

The metadata file is a JSON file containing all your uploaded songs basic information.

Sample:

[
    {
        "id": "iS2LWA7-hkM",
        "album": "Emotional Technology",
        "artist": "BT",
        "duration": "7:48",
        "href": "https:\/\/music.youtube.com\/watch?v=iS2LWA7-hkM",
        "is_liked": null,
        "title": "Paris"
    },
    {
        "id": "UiEfsYkr4gY",
        "album": null,
        "artist": "The Chainsmokers",
        "duration": "3:52",
        "href": "https:\/\/music.youtube.com\/watch?v=UiEfsYkr4gY",
        "is_liked": true,
        "title": "Paris (BKAYE Remix)"
    }
]

To generate it, follow these steps:

  1. Open https://music.youtube.com/library/uploaded_songs
  2. Scroll all the way to the bottom of your list (this may take a while if you have thousands of songs)
  3. Save the page (CTRL/CMD+S) as HTML-only into this project's clone directory
  4. Run the metadata-extractor script:
❯ ./metadata-extractor

YouTube Music Upload Metadata Extractor
=======================================

 Will extract ./metadata.json from ./YouTube Music.html:
 7369/7369 [============================] 100% 7 secs

 [OK] done

You have now a metadata.json file containing all the songs info of your entire library. This file is the source of truth for the next scripts. You may add manually missing data if needed.

See the script's help: ./metadata-extractor --help.

Download the songs

Before you start, you need to grab your YouTube Music's session cookies dumped by the "Get cookies.txt" extension. Save the file into this project's clone directory.

Use the downloader script to download all the songs listed in the metadata.json file:

❯ ./downloader

YouTube Music Upload Downloader
===============================

 Will now download 7368 songs:
 3697/7368 [==============>-------------]  50% 10 hrs

You can watch for the logs:

❯ tail -f downloader.log
[2022-03-14T11:34:22.799174+00:00] downloader.INFO: success {"id":"NZjN83C1K9M","song_path":"./downloads/NZjN83C1K9M.m4a"} []
[2022-03-14T11:34:30.753464+00:00] downloader.INFO: success {"id":"xIU9tu9l_co","song_path":"./downloads/xIU9tu9l_co.m4a"} []
[2022-03-14T11:34:37.341654+00:00] downloader.INFO: success {"id":"ca3w36Ua_aM","song_path":"./downloads/ca3w36Ua_aM.m4a"} []

By default, the script downloads all into the ./downloads directory and flattens all songs named by their ID. Each song has its metadata/thumbnail embedded.

See the script's help: ./downloader --help.

Songs clustering

Use the clusterer script to cluster your songs. It will use the metadata.json file and songs downloaded into the ./downloads directory.

By default, the script clusters all into the ./clusters directory and creates one sub-directory per album and leaves the songs without album in the first level:

clusters
├── Guiding Light - Gettin' Rolled Out
│   ├── Gettin' Rolled Out.m4a
│   └── Guiding Light (feat. Ali).m4a
├── Guru Josh Project - Infinity 2008 (Klaas Remix).m4a
├── Guys My Age (Prince Fox Remix)
│   └── Guys My Age (Prince Fox Remix).m4a
├── Ham Demo and Justin Time - Here I Am (Ham 2005 Remix).m4a
├── Happy morning
│   ├── Get Down On It.m4a
│   ├── High Energy.m4a
│   ├── I got you (I feel good).m4a
│   ├── Ring My Bell.m4a
│   └── We are family (live).m4a
├── Hardcore 2004 - You Ready?
│   ├── Hardcore 2004.m4a
│   └── You Ready?.m4a
├── Hardcore Classics Disc 3
│   └── Be Happy.m4a
├── Harlea - Miss Me.m4a
├── Haze and Weaver vs. Shox - Get up 2004.m4a
├── Heartburn
│   └── Heartburn.m4a

See the script's help: ./clusterer --help.

Playlists

For playlists, it's basically the same process as the metadata extractor: save the playlist HTML page locally, then run the script playlist-extrator. It will create a basic text file of this format:

[title] by [artist]
[title] by [artist]
...

See the script's help: ./playlist-extractor --help.

Adapt the format to your needs.

⚠️ Some titles in the playlist aren't part of your library so you may need to download them first.

Metadata polishing

If you like your library to be clean and accurate, you can leverage MusicBrainz Picard's Scan feature to fix and rename your songs.

Once again, this is a long and manual process but hopefully, you do only have to do it once.


Caveats

  • Song order is not restored and metadata information is at its most basic. This is still way better than the regular Takeout export. The most automated way to restore the song order would be to crawl each album/playlist page and parse it using the metadata extractor script, then update the clusterer script logic to handle order file naming.
  • Some steps are still manual and tedious and could/should be automated. I believe the songs page HTML source fetching could be done using Taiko.
  • Song downloading process is rather slow (~10s per song) BUT it could be faster if you decide to skip the song's metadata and/or thumbnail. I was able to download all my 7k+ songs library in a couple of hours vs a day when embedding all these info.

youtube-music-library-exporter's People

Contributors

eexit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

youtube-music-library-exporter's Issues

Seeming issue with php?

I'm trying to follow the guide, but something seems to go wrong when I try to run the metadata extractor or the downloader, when they try to open autoload.php. I'm not really sure what went wrong. Maybe it's just a problem with my php install? I'll attach the error messages I'm getting:

PHP Warning:  require_once(/Users/<user>/Music/youtube-music-library-exporter/vendor/autoload.php): Failed to open stream: No such file or directory in /Users/<user>/Music/youtube-music-library-exporter/metadata-extractor on line 17

Warning: require_once(/Users/<user>/Music/youtube-music-library-exporter/vendor/autoload.php): Failed to open stream: No such file or directory in /Users/<user>/Music/youtube-music-library-exporter/metadata-extractor on line 17
PHP Fatal error:  Uncaught Error: Failed opening required '/Users/<user>/Music/youtube-music-library-exporter/vendor/autoload.php' (include_path='.:/usr/local/Cellar/php/8.3.2/share/php/pear') in /Users/<user>/Music/youtube-music-library-exporter/metadata-extractor:17
Stack trace:
#0 {main}
  thrown in /Users/<user>/Music/youtube-music-library-exporter/metadata-extractor on line 17

Fatal error: Uncaught Error: Failed opening required '/Users/<user>/Music/youtube-music-library-exporter/vendor/autoload.php' (include_path='.:/usr/local/Cellar/php/8.3.2/share/php/pear') in /Users/<user>/Music/youtube-music-library-exporter/metadata-extractor:17
Stack trace:
#0 {main}
  thrown in /Users/<user>/Music/youtube-music-library-exporter/metadata-extractor on line 17

This is an awesome library btw, very useful, thanks for putting it online :)

Metadata Extractor returns empty list with no error messages in verbose mode

I successfully installed the dependencies and ran compose install on MacOS 12.6.

I followed the instructions to save as HTML only (scrolling all the way to the bottom) but I am getting stuck on the metadata export.

I'm not seeing any errors when I run in verbose mode but the result is empty.

(devel) gwtaylor@m1pro youtube-music-library-exporter % ./metadata-extractor -vvv --html-file=./YouTube\ Music.html                                                                                                     (master)youtube-music-library-exporter

YouTube Music Upload Metadata Extractor
=======================================

 Will extract ./metadata.json from ./YouTube Music.html:
    0 [->--------------------------] < 1 sec

 [OK] done

The saved HTML file is 44kb and contains some references to titles in my libraries. But if I grep for other titles I don't see them. I wonder if the HTML format has changed since the last update?

Downloader terminates if it encounters a track with no artist

My uploads music library is a bit of a hodge podge of files and includes some items like voice notes which don't have an artist.

The Downloader exits with an error on line 125 when trying to pass the artist into addcslashes:

addcslashes(): Argument #1 ($string) must be of type string, null given

To get round this, I manually edited the metadata.json file to replace "artist":null with "artist":"Not known"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.