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

lw-ffmpeg-node

v1.0.7

Published

`lw-ffmpeg-node` is a lightweight Node.js library for manipulating video files using FFmpeg. It provides various methods to retrieve information about video files, compress videos, create video slices, crop videos, change video size and frame rate, and sa

Downloads

10

Readme

Lightweight ffmpeg node

lw-ffmpeg-node is a lightweight Node.js library for manipulating video files using FFmpeg. It provides various methods to retrieve information about video files, compress videos, create video slices, crop videos, change video size and frame rate, and save video thumbnails. This library is designed to be simple yet comprehensive for developers to integrate into their projects easily.

Installation

These libraries requires yt-dlp and ffmpeg to be installed on the system. You can install them using the following commands.

Install ffmpeg on WSL:

sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install ffmpeg

Install yt-dlp on WSL:

sudo apt-get install python3-pip
pip3 install yt-dlp

You can then install the library like so:

npm install lw-ffmpeg-node

Usage

FFMPEGManipulator

import FFMPEGManipulator from "lw-ffmpeg-node";

The FFMPEGManipulator class provides various methods for manipulating video files. This expects that the ffmpeg command is available in the system path.

All these methods are static methods on the class.

  • getInfo(filePath: string) : Retrieves detailed information about a video file, including codec name, width, height, bit rate, frame rate, filename, duration, number of streams, and size.
  • getVideoDuration(filePath: string): Gets the duration of a video file in seconds.
  • getVideoFramerate(filePath: string): Retrieves the frame rate of a video file. compress(inputPath: string, outputPath: string, options?: ProcessOptions) Compresses a video file, copying the audio track without re-encoding and encoding the video using the libx264 codec.
  • createVideoSlice(inputPath: string, outputPath: string, inpoint: number, outpoint: number, options?: ProcessOptions): Creates a slice of a video file from inpoint to outpoint.
  • cropVideo(inputPath: string, outputPath: string, x: number, y: number, width: number, height: number, options?: ProcessOptions): Crops a video file to the specified dimensions.
  • changeSize(inputPath: string, outputPath: string, width: number, height: number, options?: ProcessOptions): Changes the size of a video file to the specified width and height.
  • changeFramerate(inputPath: string, outputPath: string, frameRate: number, options?: ProcessOptions): Changes the frame rate of a video file.
  • saveThumbnail(inputPath: string, outputPath: string, time: number, options?: ProcessOptions): Saves a thumbnail image from a video file at the specified time.
async function main() {
  const filePath = "path/to/video.mp4";
  const outputPath = "path/to/output.mp4";

  // Get video information
  const info = await FFMPEGManipulator.getInfo(filePath);
  console.log(info);

  // Compress video
  await FFMPEGManipulator.compress(filePath, outputPath);

  // Create video slice
  await FFMPEGManipulator.createVideoSlice(filePath, outputPath, 3, 6);

  // Crop video
  await FFMPEGManipulator.cropVideo(filePath, outputPath, 100, 100, 640, 480);

  // Change video size
  await FFMPEGManipulator.changeSize(filePath, outputPath, 1280, 720);

  // Change video frame rate
  await FFMPEGManipulator.changeFramerate(filePath, outputPath, 30);

  // Save video thumbnail
  await FFMPEGManipulator.saveThumbnail(filePath, "path/to/thumbnail.png", 10);
}

main();

CLI

This is the basis behind how ffmpeg and ytdlp work with the command line. The CLI class is a wrapper around the child_process module in Node.js. It provides a simple way to execute commands in the terminal.

export default class CLI {
  static cmd(
    filepath: string,
    command: string,
    options?: ProcessOptions
  ): Promise<string> {
    const args = command
      .match(/(?:[^\s"]+|"[^"]*")+/g)
      ?.map((arg) => arg.replace(/"/g, ""));
    if (!args) {
      throw new Error("Invalid command");
    }
    return new Promise((resolve, reject) => {
      execFile(
        filepath,
        args,
        {
          maxBuffer: 500 * 1_000_000,
          ...options,
        },
        (error, stdout, stderr) => {
          if (error) {
            Print.yellow(`Error executing ${path.basename(filepath)}:`, error);
            reject(stderr);
          } else {
            resolve(stdout);
          }
        }
      );
    });
  }

  static linux(command: string, options?: ProcessOptions) {
    try {
      // send back stderr and stdout
      return new Promise((resolve, reject) => {
        const child = spawn(command, {
          shell: "bash",
          ...options,
        });
        let stdout = "";
        let stderr = "";

        child.stdout?.on("data", (data) => {
          options?.quiet === false && console.log(data.toString());
          stdout += data.toString();
        });

        child.stderr?.on("data", (data) => {
          options?.quiet === false && console.log(data.toString());
          stderr += data.toString();
        });

        child.on("close", (code) => {
          if (code !== 0) {
            reject(new LinuxError(command, stderr));
          } else {
            resolve(stdout);
          }
        });
      });
    } catch (e) {
      throw new LinuxError(command);
    }
  }
}

FileManager

Print

YTDLPModel

There are two ways to use YTDLPModel:

  1. Use with ytdlp sitting on the path.
  2. Point to a specific binary of ytdlp.
import { YTDLPModel } from "./src/FFMPEGPathManipulator";

const path = "C:\\Users\\Waadl\\src\\binaries\\yt-dlp.exe";

const url = "https://www.youtube.com/watch?v=_Td7JjCTfyc";

const ytdlp = new YTDLPModel(path);
async function main() {
  // downloads video to current directory
  await ytdlp.downloadVideo(url, "./", {
    // shouldOverwite enables the --force-overwrites flag to overwrite downloading same video
    shouldOverwrite: true,
    // if quality is high, ensures mp4 download
    quality: "high",
    // cliOptions to determine command line behavior
    cliOptions: {
      // quiet flag to suppress output. If false, then prints the output as it comes
      quiet: false,
    },
  });
}

main();