Deno.js FFMPEG Library Tutorial to Build Video Converter Using TypeScript

Hello friend you may have many doubts about Deno.js FFMPEG Library Tutorial to Build Video Converter Using TypeScript We hope this article fills in all the doubts in the article. Let’s get back to the article.

Deno.js FFMPEG Library Tutorial to Build Video Converter Using TypeScript

Deno.js FFMPEG Library Tutorial to Build Video Converter Using TypeScript

An easy to use ffmpeg module for Deno.

Installation

This is a Deno module and can be imported directly from the repo and from following registries.

Deno Registry

import { ffmpeg } from "https://deno.land/x/fast_forward@<version>/mod.ts";

Nest Registry

import { ffmpeg } from "https://x.nest.land/fast_forward@<version>/mod.ts";

Github

import { ffmpeg } from "https://raw.githubusercontent.com/c4spar/deno-fast-forward/<version>/mod.ts";

Usage

await ffmpeg("https://www.w3schools.com/html/mov_bbb.mp4")
  // Global encoding options (applied to all outputs).
  .audioBitrate("192k")
  .videoBitrate("1M")
  .width(480)
  .height(640)
  // Ouput 1.
  .output("output.mp4")
  .audioCodec("aac")
  .videoCodec("libx264")
  // Ouput 2.
  .output("output.webm")
  .audioCodec("libvorbis")
  .videoCodec("libvpx-vp9")
  // Start encoding.
  .encode();

console.log("All encodings done!");

Getting Started

First create an instance of FFmpeg.

const encoder = ffmpeg("https://www.w3schools.com/html/mov_bbb.mp4");
// or using the constructor
const encoder = new FFmpeg("https://www.w3schools.com/html/mov_bbb.mp4");

Then you can define global options and events which will be applied to all defined outputs.

encoder
  .audioBitrate("192k")
  .videoBitrate("1M")
  .addEventListener(
    "progress",
    (event) => console.log("Progress: %s", event.progress),
  )
  .addEventListener("error", (event) => console.log(event.error));

Examples

await ffmpeg("https://www.w3schools.com/html/mov_bbb.mp4")
  .audioBitrate("192k")
  .videoBitrate("1M")
  .width(480)
  .height(640)
  .addEventListener("start", (event) => console.log("Event: %s", event.type))
  .addEventListener("info", (event) => console.log("Event: %s", event.type))
  .addEventListener("progress", (event) => console.log("Event: %s", event.type))
  .addEventListener("end", (event) => console.log("Event: %s", event.type))
  .addEventListener("error", (error) => console.log("Event: %s", error.type))
  .output("output.mp4")
  .output("output.webm")
  .encode();

console.log("All encodings done!");
$ deno run --allow-read --allow-run https://deno.land/x/fast_forward/examples/events.ts

Process handling

const encoder = ffmpeg("https://www.w3schools.com/html/mov_bbb.mp4")
  .audioBitrate("192k")
  .videoBitrate("1M")
  .width(480)
  .height(640)
  .output("output.mp4")
  .output("output.mkv");

for await (const process: EncodingProcess of encoder) {
  process.run();
  const status: EncodingStatus = await process.status();
  process.close();
  if (!status.success) {
    throw new Error(
      `Encoding failed: ${process.encoding.output}\n${
        new TextDecoder().decode(await process.stderrOutput())
      }`,
    );
  }
  console.log("Encoding of %s done!", process.encoding.output);
}

console.log("All encodings done!");
$ deno run --allow-read --allow-run https://deno.land/x/fast_forward/examples/process-handling.ts

Event Stream

const spinner = wait({ text: "" });

const encoder = ffmpeg("https://www.w3schools.com/html/mov_bbb.mp4")
  .audioBitrate("192k")
  .videoBitrate("1M")
  .width(480)
  .height(640)
  .output("output.mp4")
  .output("output.webm");

for await (const process: EncodingProcess of encoder) {
  process.run();
  spinner.start();
  for await (const event: EncodingEvent of process) {
    switch (event.type) {
      case "start":
        spinner.text = `Loading meta data: ${event.encoding.output} ...`;
        break;
      case "info":
        spinner.text = `Start encoding: ${event.encoding.output} ...`;
        break;
      case "progress":
        spinner.text = `Encode: ${event.encoding.output} - ${event.progress}%`;
        break;
      case "end":
        spinner.stop();
        process.close();
        console.log(`<img alt="✔" src="https://s.w.org/images/core/emoji/13.1.0/svg/2714.svg" style="width: 20px;"> Encode: ${process.encoding.output} - 100%`);
        break;
      case "error":
        spinner.stop();
        process.close();
        console.log(`✘ Encode: ${process.encoding.output} - failed!`);
        throw event.error;
    }
  }
}

console.log("All encodings done!");
$ deno run --allow-read --allow-run --unstable https://deno.land/x/fast_forward/examples/event-stream.ts

Output Stream

const encoder = ffmpeg("https://www.w3schools.com/html/mov_bbb.mp4")
  .output("pipe:1")
  .format("mp4")
  .videoBitrate("933k")
  .audioBitrate("128k")
  .args(["-movflags", "frag_keyframe+empty_moov"]);

for await (const process: EncodingProcess of encoder) {
  process.run();
  if (process.stdout) {
    const outputFile: Deno.File = await Deno.open("output.mp4", {
      create: true,
      write: true,
    });
    const [status] = await Promise.all([
      process.status(),
      Deno.copy(process.stdout, outputFile),
    ]);
    console.log({ status });
  }
  process.close();
}

console.log("Encoding done!");
$ deno run --allow-read --allow-write --allow-run https://deno.land/x/fast_forward/examples/output-s

Read Also: Deno.js Tutorial to Get Module or Package Information Using moduleInfo Library in TypeScript

Final Words

Deno.js FFMPEG Library Tutorial to Build Video Converter Using TypeScriptthe article will satisfy all your doubts.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment