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

substract

v1.0.1

Published

A NodeJS library for extracting hard-coded subtitles from within videos.

Downloads

210

Readme

Substract

wakatime Node.js CI GitHub License GitHub Release codecov Size typescript

Substract is a Node.js library designed to extract hard-coded subtitles from videos efficiently. Leveraging powerful tools like FFmpeg, Apple's OCR engine, and parallel processing, Substract provides a seamless way to retrieve subtitles embedded directly within video files.

Table of Contents

Features

  • Efficient Frame Extraction: Extract frames from videos at specified intervals.
  • Duplicate Frame Filtering: Remove similar or consecutive duplicate frames to optimize processing.
  • OCR Integration: Utilize Apple's OCR engine for accurate text recognition.
  • Concurrency Control: Manage the number of concurrent OCR processes to balance performance and resource usage.
  • Customizable Options: Tailor the extraction process with various configuration options.
  • Comprehensive Callbacks: Hook into different stages of the extraction process for enhanced control and monitoring.
  • 100% Test Coverage: Robust unit tests ensure reliability and stability.

Installation

Ensure you have Node.js v20.0.0 or higher installed.

npm install substract

or

pnpm install substract

or

yarn add substract

Usage

Here's a basic example of how to use Substract to extract subtitles from a video:

import { substract } from 'substract';

const videoFile = 'path/to/video.mp4';
const outputFile = 'path/to/output.json';

const options = {
    ocrOptions: {
        appleBinaryPath: '/path/to/ocr/binary', // you can get this from https://github.com/glowinthedark/macOCR
    },
    outputOptions: {
        outputFile,
    },
    callbacks: {
        onGenerateFramesStarted: async (videoFile) => {
            console.log(`Started generating frames for ${videoFile}`);
        },
        onGenerateFramesFinished: async (frames) => {
            console.log(`Finished generating ${frames.length} frames`);
        },
        onOcrFinished: (ocrResults) => {
            console.log('OCR processing completed');
        },
        onOcrProgress: (frame, index) => {
            console.log(`Processing frame ${index + 1}: ${frame.filename}`);
        },
    },
    concurrency: 5, // Optional: Limits the number of concurrent OCR processes
    duplicateTextThreshold: 0.9, // Optional: Threshold for filtering duplicate text
    frameOptions: {
        cropOptions: { top: 10, bottom: 20 }, // Optional: Crop options for frame extraction
        frequency: 5, // Optional: Extract frames every 5 seconds
    },
};

substract(videoFile, options)
    .then((outputPath) => {
        if (outputPath) {
            console.log('OCR results saved to:', outputPath);
        } else {
            console.log('No frames were generated. Aborting subtitle extraction.');
        }
    })
    .catch((error) => {
        console.error('Error extracting subtitles:', error);
    });

API Reference

substract(videoFile, options)

Extracts hard-coded subtitles from a video file.

Parameters

  • videoFile: string
    Path to the video file from which to extract subtitles.

  • options: SubstractOptions
    Configuration options for the extraction process.

Returns

  • Promise<null | string>
    Resolves to the path of the output JSON file containing OCR results if successful, or null if no frames were generated.

Options

SubstractOptions

Configuration options for the substract function.

| Property | Type | Description | | ------------------------ | ------------------------- | ----------------------------------------------------------------------------------------------- | | ocrOptions | OCROptions | Options related to the OCR engine. | | outputOptions | OutputOptions | Options related to the output file. | | callbacks | Callbacks | Callback functions to hook into various stages of the extraction process. | | concurrency | number (optional) | Limits the number of concurrent OCR processes. Default is 5. | | duplicateTextThreshold | number (optional) | Threshold for filtering duplicate text. Values range between 0 and 1. Default is undefined. | | frameOptions | FrameOptions (optional) | Options for frame extraction, such as cropping and extraction frequency. |

OCROptions

| Property | Type | Description | | ----------------- | ------------------------- | ---------------------------------------------- | | appleBinaryPath | string | Path to the Apple OCR binary executable. | | callbacks | OcrCallbacks (optional) | Callback functions specific to OCR processing. | | concurrency | number (optional) | Number of concurrent OCR processes. |

OutputOptions

| Property | Type | Description | | ------------ | -------- | -------------------------------------------- | | outputFile | string | Path to save the OCR results in JSON format. |

Callbacks

Substract provides several callbacks to monitor and control the extraction process.

Callbacks

| Callback | Description | | -------------------------- | ------------------------------------------------------------------------------- | | onGenerateFramesStarted | Called when frame generation starts. Receives the video file path. | | onGenerateFramesFinished | Called when frame generation finishes. Receives an array of generated frames. | | onOcrStarted | Called when OCR processing starts. Receives an array of frames being processed. | | onOcrFinished | Called when OCR processing finishes. Receives an array of OCR results. | | onOcrProgress | Called after each frame is processed. Receives the frame and its index. |

OcrCallbacks

| Callback | Description | | --------------- | ----------------------------------------------------------------------- | | onOcrFinished | Called when OCR processing finishes. Receives an array of OCR results. | | onOcrProgress | Called after each frame is processed. Receives the frame and its index. | | onOcrStarted | Called when OCR processing starts. Receives an array of frames. |

License

This project is licensed under the MIT License.

Acknowledgements

The Apple OCR method requires the build of the tool from here: macOCR