npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

captioneer

v1.1.0

Published

YouTube Video Captions Library

Downloads

206

Readme

Captioneer

A powerful TypeScript library for fetching YouTube video transcripts with ease.

Build Status Coverage Status npm bundle size npm downloads npm version License: MIT

Features

  • 🎯 Fetch transcripts from any YouTube video
  • 🔌 Configurable HTTP clients (Fetch, Axios, Got, etc.)
  • 🌍 Support for multiple languages
  • 💪 Type-safe with full TypeScript support
  • ⚡ Modern ESM and CommonJS support
  • 🛡️ Robust error handling
  • 🧪 Well-tested and reliable

Installation

npm install captioneer
# or
yarn add captioneer
# or
pnpm add captioneer

Usage

Basic Example

import { Captioneer } from 'captioneer';

const captioneer = new Captioneer();

// Fetch captions for a video
const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ');
console.log(captions);

With Language Selection

const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ', {
  lang: 'es' // Fetch Spanish captions
});

HTTP Clients

Captioneer provides flexibility in how you make HTTP requests through its HTTP client system.

Using the Default HTTP Client

The library comes with a built-in HTTP client using the Fetch API:

import { Captioneer } from 'captioneer';

// Uses default HTTP client
const captioneer = new Captioneer();
const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ');

Using Custom HTTP Clients

You can provide your own HTTP client implementation. Here's an example using Axios:

import { Captioneer, CaptioneerHttpClient } from 'captioneer';
import axios from 'axios';

const axiosClient: CaptioneerHttpClient = {
  async get(url, options) {
    const response = await axios.get(url, options);
    return {
      ok: response.status >= 200 && response.status < 300,
      text: async () => response.data
    };
  }
};

const captioneer = new Captioneer(axiosClient);
const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ');

Advanced HTTP Client Configuration

Custom HTTP clients support additional configurations like proxies or custom headers:

const configuredClient: CaptioneerHttpClient = {
  async get(url, options) {
    const response = await axios.get(url, {
      ...options,
      proxy: {
        host: 'proxy-server',
        port: 8080,
      },
      headers: {
        ...options?.headers,
        'Custom-Header': 'value',
      },
    });

    return {
      ok: response.status >= 200 && response.status < 300,
      text: async () => response.data,
    };
  },
};

The HTTP client interface ensures compatibility while giving you full control over the request implementation.

Error Handling

import { Captioneer, isCaptioneerError, ErrorCode } from 'captioneer';

try {
  const captions = await captioneer.fetchCaptions('VIDEO_ID');
} catch (error) {
  if (isCaptioneerError(error)) {
    switch (error.code) {
      case ErrorCode.LANGUAGE_NOT_AVAILABLE:
        console.log('Language not available:', error.details.availableLanguages);
        break;
      case ErrorCode.VIDEO_UNAVAILABLE:
        console.log('Video is not available');
        break;
      // Handle other specific errors
    }
  }
}

API Reference

Captioneer

Main class for interacting with YouTube transcripts.

Methods

fetchCaptions(videoId: string, config?: CaptioneerConfig): Promise<CaptionEntry[]>

Fetches captions for a YouTube video.

  • videoId: YouTube video ID or URL
  • config: Optional configuration object
    • lang: Language code (e.g., 'en', 'es')

Returns an array of caption entries.

Types

interface CaptionEntry {
  text: string;
  duration: number;
  offset: number;
  lang: string;
}

interface CaptioneerConfig {
  lang?: string;
}

Error Handling

The library provides specific error types for different scenarios:

  • TooManyRequestsError: Rate limiting detected
  • VideoUnavailableError: Video not accessible
  • CaptionsDisabledError: Captions are disabled
  • LanguageNotAvailableError: Requested language not available
  • InvalidVideoIdError: Invalid video ID format
  • NetworkError: Network-related issues
  • ParseError: Response parsing failed

Development

# Install dependencies
pnpm install

# Run tests
pnpm test

# Build the library
pnpm build

# Run linting
pnpm lint

Contributing

Contributions are welcome! We follow a structured branching strategy and conventional commits.

Quick Start

  1. Fork the repository
  2. Create your feature branch from develop (git checkout -b feature/amazing-feature)
  3. Write meaningful commit messages following conventional commits (feat: add amazing feature)
  4. Push to your branch (git push origin feature/amazing-feature)
  5. Open a Pull Request to develop

Detailed Guidelines

Your contributions help make Captioneer better for everyone!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

Created by ItsCurstin