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

@mtillmann/chapters

v0.1.2

Published

library that manages and converts chapters of multiple formats

Downloads

36

Readme

tests

npm version

chapters

This is the core library of the chaptertool project and provides the functionality to handle the formats below.

Click here to open the web GUI or go install chaptertool locally for CLI use.

Supported formats

| class | description | key | ext | info | |-------------------------------------|------------------------------|----------------|--------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ChaptersJson | Podcasting 2.0 Chapters | chaptersjson | json | spec | | FFMetadata | FFMetadata | ffmpegdata | txt | spec | | MatroskaXML | Matroska XML chapters | matroskaxml | xml | spec | | MKVMergeXML | MKVToolNix mkvmerge XML | mkvmergexml | xml | spec | | MKVMergeSimple | MKVToolNix mkvmerge simple | mkvmergesimple | txt | spec | | WebVTT | WebVTT Chapters | webvtt | vtt | spec | | Youtube | Youtube Chapter Syntax | youtube | txt | | | FFMpegInfo | FFMpegInfo | ffmpeginfo | txt | read only, used internally | | PySceneDetect | PySceneDetect | pyscenedetect | csv | project home | | VorbisComment | Vorbis Comment Format | vorbiscomment | txt | spec | | AppleChapters | "Apple Chapters" | applechapters | xml | source | | ShutterEDL | Shutter EDL | edl | edl | source | | PodloveSimpleChapters | Podlove Simple Chapters | psc | xml | spec | | PodloveJson | Podlove Simple Chapters JSON | podlovejson | json | source | | MP4Chaps | MP4Chaps | mp4chaps | txt | source | | AppleHLS | Apple HLS Chapters | applehls | json | spec, partial support | | Scenecut | Scenecut format | scenecut | json | source |

Installation

npm i @mtillmann/chapters

Usage

import { ChaptersJson } from '@mtillmann/chapters';

const chapters = new ChaptersJson()
chapters.add({ startTime: 0, title: 'Intro' })
chapters.add({ startTime: 30, title: 'Some Topic' })
const json = chapters.toString()

Autoformat

Autoformat is a helper that can be used to automatically detect the format of a given input and return the corresponding class, key or a new instance of the detected class. It also provides a method to convert the input to a specific format.

import { Autoformat } from '@mtillmann/chapters'
import { WebVTT } from '@mtillmann/chapters'

const content = '<podcasting 2.0 chapters.json>...'
const instance = Autoformat.detect(content) // returns an instance of ChaptersJson
const instance2 = Autoformat.from(content) // returns an instance of ChaptersJson

const key = Autoformat.detect(content, 'key') // returns 'chaptersjson'
const className = Autoformat.detect(content, 'class') // returns ChaptersJson

const webVttString = 'WEBVTT...';

const autodetectedWebVtt = Autoformat.detect(webVttString) // returns an instance of WebVTT

const fail = Autoformat.as('chaptersjson', webVttString) // throws an error

const vtt = Autoformat.as('webvtt', webVttString) // returns an instance of WebVTT
const vtt2 = Autoformat.as(WebVTT, webVttString) // returns an instance of WebVTT

API

All formats support the following methods:

constructor (duration: number = 3600)

Creates a new instance of the class, optionally with a duration in seconds.

const chapters = (new ChaptersJson(3600)).from(input)

the constructor will not accept any input due to javascript's order of initialization which prevents the parse method from having access to cerain locally defined properties and methods.

static create (input?: string | MediaItem): MediaItem

Creates a new media item. This is the suggested way to create a media item from a string:

const chapters = MatroskaXML.create(input)
// chapters is now an instance of MatroskaXML

const chapterString = WebVTT.create(chapters).toString()
// chapterString is now a string representation of the chapters

from (input?: string | MediaItem): MediaItem

Populates the media item's chapters from the given input.

to (className: any): MediaItem

Converts the media item to another format.

add (chapter: Chapter): void

Adds a chapter.

addChapterAt (index: number, chapter: object = {}): number

Adds a chapter at the given index.

addChapterAtTime (time: number | string, chapter: object = {}): boolean

Adds a chapter at the given time.

remove (index: number): void

Removes the chapter at the given index.

bump (keepDuration: boolean = false): void

Updates the chapters and duration. Called automatically after adding or removing chapters.

endTime (index: number): number

Returns the end time for the chapter at the given index.

expandFirstToStart (): void

Expands the first chapter to start at 0.

applyChapterMinLength (seconds: number): object

Applies a minimum length to the chapters.

chapterExistsAtStartTime (time: number): boolean

Checks if a chapter exists at the given start time.

updateChapterStartTime (index: number, startTime: number | string): number | 'timeInUse'

Updates the start time for the chapter at the given index.

chapterIndexFromStartTime (startTime: number | string): number

Returns the index of the chapter with the given start time.

chapterIndexFromTime (time: number | string): null | number

Returns the index of the chapter at the given time.

toString([pretty: boolean][, options: object]): string

Convert the chapters to a string.
If pretty is true, the output will be formatted for better readability. Only supported by json and xml formats. Some formats support additional options:

ChapterJson toString() options

| option | type | default | description | |---------------------|-----------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------| | imagePrefix | string | '' | Prefix for image URLs | | writeRedundantToc | boolean | false | Write redundant TOC attributes | | writeEndTimes | boolean | false | Write end times |

AppleChapters toString() options

| option | type | default | description | |-----------------|-----------|---------|-----------------------------------------------------------| | acUseTextAttr | boolean | false | When set, the text-attribute will be used instead of node textContent |

PySceneDetect toString() options

| option | type | default | description | |--------------------|----------|----------|------------------------------------------| | psdFramerate | number | 23.976 | Framerate of the video file | | psdOmitTimecodes | boolen | false | When set, the first line will be omitted |

Scenecut toString() options

| option | type | default | description | |--------|----------|---------|-----------------------------| | frameRate | number | 30 | Framerate of the video file | | ptsScale | number | 1 | PTS scale (See below) | | score | number | 0.5 | Score threshold |

When asked about the ptsScale-value, ChatGPT said:

The number "1001" in the context of video processing and multimedia, especially in relation to frame rates and timecodes, is often associated with the NTSC color system used in North America and Japan. In this system, the frame rate is often described as "29.97 frames per second", but it's technically 30000/1001 frames per second, which is approximately 29.97 but not exactly. This is known as a "drop frame" rate, and the "1001" comes from this fractional frame rate.

When you use 1 the output pts will be the same as the frame number. When you use 1001 the output pts will be the same as the frame number multiplied by 1001.