概要
- Node.jsからffmpegを叩きたくなった
- いいラッパーないかなと探していたが、よさそうなのがアーカイブされていた
- というわけでうすーいラッパーを用意した。その時のメモ
前提
ffmpegのバージョン
ffmpeg version 5.1.6-0+deb12u1 Copyright (c) 2000-2024 the FFmpeg developers
コード
import { execFile } from "child_process";
import { promisify } from "util";
const execFileAsync = promisify(execFile);
async function extractVideoThumbnail(
inputPath: string,
outputPath: string,
timeout: number,
maxBuffer: number
): Promise<void> {
const args = [
"-threads",
"1",
"-i",
inputPath,
"-vf",
"thumbnail",
"-frames:v",
"1",
"-vcodec",
"mjpeg",
"-f",
"image2",
"-y",
outputPath,
];
try {
await execFileAsync("ffmpeg", args, {
timeout: timeout,
maxBuffer: maxBuffer,
});
} catch (error: any) {
throw new Error(`FFmpeg thumbnail extraction failed: ${error.message}`);
}
}
確認してみる
-threads 1
CPUスレッド数を指定できる. 何も指定しない場合は自動的に選択する(多分)
-i ${入力ソース}
インプットするファイル名
-vf thumbnail -frames:v 1
これがよくわからなかったが、調べていると下記記事に出会う
先頭から100フレーム内で、もっともサムネイルにふさわしい絵
すげー!知らなかった
-vcodec mjpeg
コーデック指定
-f image2
よくわからなかったのだが
This will extract one video frame per second from the video and will output them in files named foo-001.jpeg, foo-002.jpeg, etc. Images will be rescaled to fit the new WxH values.
複数ファイルが作成されれる場合は、foo-001.jpeg, foo-002.jpeg,...
みたいなファイルが作成されるとのことだが、今回1枚想定なので多分よしっ
-y
出力ファイル名がすでに作成されている場合は、No確認で上書きする apt update -y
みたいなイメージ