DEV Community

Cover image for Solved: Exporting YouTube Playlist Metadata to CSV for Content Creators
Darian Vance
Darian Vance

Posted on • Originally published at wp.me

Solved: Exporting YouTube Playlist Metadata to CSV for Content Creators

🚀 Executive Summary

TL;DR: Content creators often struggle with manually exporting YouTube playlist metadata for analytics. This guide provides a Python script utilizing the YouTube Data API v3 to automate the extraction of video titles, IDs, and publish dates, exporting them to a clean CSV file. This automation saves significant time and reduces errors in content planning workflows.

🎯 Key Takeaways

  • The script requires Python 3, a Google Account, a YouTube Playlist ID, and a YouTube Data API v3 Key obtained from the Google Cloud Console.
  • Securely manage your YouTube API key using python-dotenv and a config.env file to prevent accidental exposure in version control.
  • The google-api-python-client is used to interact with the YouTube Data API, and a while True loop with nextPageToken is essential for handling API pagination to retrieve all videos in playlists exceeding 50 items.
  • Robust error handling with try…except HttpError is crucial for gracefully managing issues like invalid API keys, private playlists, or exceeding daily API quotas.
  • Pandas is leveraged to create a DataFrame from the fetched video data, allowing for easy data manipulation (e.g., timestamp formatting, sorting) before exporting to a CSV file.
  • The script can be scheduled using tools like cron jobs on Linux systems to provide automated, regular reports of playlist metadata with zero manual effort.

Exporting YouTube Playlist Metadata to CSV for Content Creators

Darian Vance here. At TechResolve, my day-to-day involves building robust, automated pipelines. A few years ago, I started a small YouTube channel on the side to document some home lab projects, and I quickly realized a huge time sink: content planning. I was manually copy-pasting video titles, IDs, and publish dates into a spreadsheet to analyze what was working. It was tedious and prone to error. That’s when I decided to apply my DevOps mindset to my creator workflow. This script is the result—it pulls all that metadata automatically, saving me a solid hour every week. Now, I just run it, and a clean CSV is ready for my analytics tools.

Today, I’m going to walk you through how to build it yourself. Let’s get that time back.

Prerequisites

  • Python 3 installed on your machine.
  • A Google Account to access the Google Cloud Console.
  • A YouTube Playlist ID. You can find this in the URL of the playlist you want to track (e.g., in youtube.com/playlist?list=PL…, the ID is PL…).
  • A YouTube Data API v3 Key. You can get this for free from the Google Cloud Console. Just create a new project, enable the “YouTube Data API v3,” and generate credentials for an API key. Keep this key safe!

The Guide: Step-by-Step

Step 1: Project Setup

First things first, let’s get our project environment ready. I’ll skip the standard virtual environment setup commands since you likely have your own workflow for that. Just make sure you’re working in an isolated environment.

Once your environment is active, you’ll need a few Python libraries. You can install them by running the standard pip install command for google-api-python-client, python-dotenv, and pandas. These handle the API connection, managing our secret key, and handling the data, respectively.

Next, create two files in your project directory: get\_playlist\_data.py for our script and a config.env file to securely store our API key.

In your config.env file, add your API key like this:

YOUTUBE_API_KEY="YOUR_API_KEY_HERE"
Enter fullscreen mode Exit fullscreen mode

This approach prevents you from accidentally committing your secret key to a git repository—a mistake I’ve seen take down production systems.

Step 2: The Python Script – Imports and Setup

Let’s open get\_playlist\_data.py and start coding. We’ll begin by importing the necessary libraries and loading our environment variables.

import os
import pandas as pd
from dotenv import load_dotenv
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

def main():
    # Load environment variables from config.env
    load_dotenv('config.env')
    api_key = os.getenv('YOUTUBE_API_KEY')

    # The ID of the playlist you want to fetch
    playlist_id = 'YOUR_PLAYLIST_ID_HERE' 

    if not api_key:
        print("Error: YOUTUBE_API_KEY not found. Please check your config.env file.")
        return

    # The rest of our logic will go here
    print("Starting script...")

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Here, we import everything we need. The load\_dotenv() function reads our config.env file, and os.getenv() securely fetches the API key. I’ve added a simple check to ensure the key was loaded correctly. Remember to replace ’YOUR\_PLAYLIST\_ID\_HERE’ with your actual playlist ID.

Step 3: Fetching the Data from the API

Now for the core logic. We need to initialize the YouTube API service and then repeatedly call it to get all the videos in the playlist. The API returns data in “pages” of up to 50 items at a time, so we need to handle pagination.

Let’s add the fetching logic inside our main function.

    try:
        # Initialize the YouTube API client
        youtube = build('youtube', 'v3', developerKey=api_key)

        all_videos = []
        next_page_token = None

        while True:
            request = youtube.playlistItems().list(
                part='snippet,contentDetails',
                playlistId=playlist_id,
                maxResults=50,
                pageToken=next_page_token
            )
            response = request.execute()

            for item in response.get('items', []):
                snippet = item.get('snippet', {})
                video_id = snippet.get('resourceId', {}).get('videoId')
                video_title = snippet.get('title')
                published_date = snippet.get('publishedAt')

                # We only process valid video entries
                if video_id and video_title != 'Private video' and video_title != 'Deleted video':
                    all_videos.append({
                        'published_at': published_date,
                        'video_title': video_title,
                        'video_id': video_id,
                        'video_url': f'https://www.youtube.com/watch?v={video_id}'
                    })

            next_page_token = response.get('nextPageToken')
            if not next_page_token:
                break # Exit loop if no more pages

        print(f"Successfully fetched {len(all_videos)} videos.")

    except HttpError as e:
        print(f"An HTTP error {e.resp.status} occurred: {e.content}")
        return
Enter fullscreen mode Exit fullscreen mode

What’s happening here?

  1. We initialize the API service with build().

  2. We start a while True loop to handle pagination.

  3. Inside the loop, youtube.playlistItems().list(…) makes the API request. We ask for the snippet (which contains title, publish date) and contentDetails (which confirms the video ID).

  4. The pageToken parameter tells the API which page of results to give us. For the first request, it’s None.

  5. We loop through the returned items, pull out the data we need, and add it to our all\_videos list. I’ve included a check to skip private or deleted videos, which can otherwise cause issues.

  6. We check for a nextPageToken in the response. If it exists, the loop continues with that token. If not, we’ve reached the last page, and we break.

Pro Tip: I’ve wrapped the API call in a try…except HttpError block. This is crucial for production setups. If your API key is invalid, the playlist is private, or you exceed your daily quota, the script will fail gracefully instead of crashing.

Step 4: Exporting to CSV

The final step is to take our list of video data and use the Pandas library to export it to a clean CSV file. Add this code to the end of your main function, right after the try…except block.

    if not all_videos:
        print("No videos found or an error occurred.")
        return

    # Create a Pandas DataFrame
    df = pd.DataFrame(all_videos)

    # Optional: Convert 'published_at' to a more readable format
    df['published_at'] = pd.to_datetime(df['published_at']).dt.strftime('%Y-%m-%d %H:%M:%S')

    # Sort by publish date, oldest first
    df = df.sort_values(by='published_at', ascending=True)

    # Export to CSV
    output_filename = f'playlist_{playlist_id}_export.csv'
    df.to_csv(output_filename, index=False, encoding='utf-8')

    print(f"Data successfully exported to {output_filename}")
Enter fullscreen mode Exit fullscreen mode

This part is straightforward. We load our all\_videos list into a Pandas DataFrame, which is essentially a powerful in-memory spreadsheet. I like to convert the timestamp to a cleaner format and sort the data by publish date. Finally, df.to\_csv() does the heavy lifting, writing everything to a file named after the playlist ID. Done!

Pro Tip: You can schedule this script to run automatically. On a Linux system, a simple cron job would do the trick. For instance, to run it every Monday at 2 AM, your cron entry might look like this: 0 2 \* \* 1 python3 get\_playlist\_data.py. This gives you a fresh report at the start of each week with zero effort.

Common Pitfalls

Here are a few places where I’ve stumbled in the past, so you can avoid them:

  • API Quota Exceeded: The YouTube Data API has a generous free daily quota, but it’s not unlimited. If you run the script too many times while debugging, you might hit it. The error message is usually a 403 Forbidden. Just wait 24 hours for it to reset.
  • Incorrect Playlist ID: Double-check you’ve copied the full ID from the URL. A common mistake is grabbing the video ID instead of the playlist ID if you’re on a page watching a video that’s part of a playlist.
  • Forgetting Pagination: The maxResults is capped at 50. If you have more than 50 videos and forget the while loop with the nextPageToken, you’ll only get the first batch of results. I made this mistake and for a week I thought my older videos had vanished!

Conclusion

And that’s it. You now have a powerful, automated script for exporting your YouTube playlist metadata. From here, you can load the CSV into Google Sheets, Excel, or a data visualization tool to track your content’s performance, look for patterns, and plan your next big video. It’s a simple tool, but in my experience, these are the kinds of small automations that free up significant time and mental energy, letting you focus on what you do best: creating.


Darian Vance

👉 Read the original article on TechResolve.blog


Support my work

If this article helped you, you can buy me a coffee:

👉 https://buymeacoffee.com/darianvance

Top comments (0)