Download YouTube Playlist
Download all YouTube videos (or audio only) from every video in a playlist!
Prerequisites
You'll need a few things to use this project:
- This project uses Bun! Get that installed or feel free to edit the source code to use Node instead (it'll only be a few adjustments).
- You'll need a YouTube Data API key. Set that to the
API_KEY
env variable. - The yt-dlp command line tool needs to be present on your system. You can easily install it with a tool like Brew.
Usage
Say you have this code in a file named download.ts
:
import {downloadYoutubePlaylist} from 'dl-yt-playlist'
const {playlistId, apiKey} = process.env
const resultsMetadata = await downloadYoutubePlaylist({
playlistId: <string>,
apiKey: <string>,
audioOnly: <boolean>,
getFullData: <boolean>,
jsonOnly: <boolean>,
maxLengthInSeconds: <number>,
})
Now you can use Bun to run the file:
bun run download.ts
Folder Structure
Downloads will be organized into the following folder structure:
data
/<playlist name>
/video
/audio
responses.json
videoMetadata.json
/video | This folder will contain all the mp4 video files |
/audio | This folder will contain all the mp3 audio files |
responses.json | This file will contain all the responses from the YouTube api. This is useful for understanding the shape of the data. |
videoMetadata.json | This file will contain an array metadata on each video. See shape below |
Video Metadata Shape
Each video will have metadata stored in the videoMetadata.json
with the following shape:
{
id: string
title: string
channel: string
publishedAt: string
dateAddedToPlaylist: string
url: string
lengthInSeconds: number
}