1.0.1 • Published 8 months ago

substract v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

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.

PropertyTypeDescription
ocrOptionsOCROptionsOptions related to the OCR engine.
outputOptionsOutputOptionsOptions related to the output file.
callbacksCallbacksCallback functions to hook into various stages of the extraction process.
concurrencynumber (optional)Limits the number of concurrent OCR processes. Default is 5.
duplicateTextThresholdnumber (optional)Threshold for filtering duplicate text. Values range between 0 and 1. Default is undefined.
frameOptionsFrameOptions (optional)Options for frame extraction, such as cropping and extraction frequency.

OCROptions

PropertyTypeDescription
appleBinaryPathstringPath to the Apple OCR binary executable.
callbacksOcrCallbacks (optional)Callback functions specific to OCR processing.
concurrencynumber (optional)Number of concurrent OCR processes.

OutputOptions

PropertyTypeDescription
outputFilestringPath to save the OCR results in JSON format.

Callbacks

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

Callbacks

CallbackDescription
onGenerateFramesStartedCalled when frame generation starts. Receives the video file path.
onGenerateFramesFinishedCalled when frame generation finishes. Receives an array of generated frames.
onOcrStartedCalled when OCR processing starts. Receives an array of frames being processed.
onOcrFinishedCalled when OCR processing finishes. Receives an array of OCR results.
onOcrProgressCalled after each frame is processed. Receives the frame and its index.

OcrCallbacks

CallbackDescription
onOcrFinishedCalled when OCR processing finishes. Receives an array of OCR results.
onOcrProgressCalled after each frame is processed. Receives the frame and its index.
onOcrStartedCalled 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