1. 개요
TypeScript 코드로 비디오 정보를 가져오는 기능은 ffprobe를 사용하여 짧은 코드로 구현할 수 있습니다.
TypeScript 코드에서 ffprobe를 실행하여 비디오 정보를 가져오는 코드를 소개합니다.
JavaScript 코드에 비해 TypeScript로 코드를 작성하면 잠재적인 오류를 사전에 방지할 수 있고, 결과 타입의 필드를 지정하여 편리하게 사용할 수 있습니다.
1.1. 사용 환경
사용한 환경은 다음과 같습니다.
- 운영체제 : windows10 Pro 64비트
- 비디오 파일의 정보를 가져오는 도구 : ffprobe
- 실행코드 작성 언어 : TypeScript
- Node.js 버전 : v24.3.0
2. 준비
ffprobe 실행파일이 필요합니다.
2.1. ffprobe.exe 다운로드
ffmpeg의 빌드된 바이너리의 다운로드를 제공하는 다음의 URL에서 파일을 다운로드합니다.
https://github.com/BtbN/FFmpeg-Builds/releases
Releases · BtbN/FFmpeg-Builds
Contribute to BtbN/FFmpeg-Builds development by creating an account on GitHub.
github.com
자신의 환경에 맞는 압축파일을 다운로드합니다.
보통의 윈도우 환경에서는 ffmpeg-master-latest-win64-lgpl.zip 파일을 다운로드하면 됩니다.
(참고: OOOOO-shared.zip은 연결된 dll 파일이 함께 있어야 실행됩니다)
2.2. ffprobe.exe 압축 해제하여 복사
압축파일을 열어서 bin 디렉토리로 이동하면 다음의 실행파일 중의 ffprobe.exe를 찾을 수 있습니다.
3. 코드
3.1. 파일 구성
│
├─── ffprobe.exe # ffprobe 실행파일
├─── probe.ts # 비디오 정보를 가져오기 타입스크립트 코드
└─── video_sample.mp4 # 샘플 비디오 파일
3.2. 타입스크립트 소스 코드
probe.ts:
import { spawn } from "node:child_process";
type VideoInfo = {
format: {
filename: string;
format_name: string;
duration: number;
size: number;
};
streams: {
codec_name: string;
width: number;
height: number;
duration: number;
}[];
content: any;
};
async function getVideoInfo(filePath: string): Promise<VideoInfo | undefined> {
const child = spawn("ffprobe", [
"-print_format",
"json",
"-show_streams",
"-show_format",
filePath,
]);
let out = "";
child.stdout.setEncoding("utf8");
child.stdout.on("data", (s) => (out += s));
const code = await new Promise((r) => child.on("close", r));
if (code !== 0) return undefined;
const content = JSON.parse(out);
return {
format: {
filename: content.format.filename,
format_name: content.format.format_name,
duration: content.format.duration,
size: content.format.size,
},
streams: content.streams.map((stream: any) => ({
codec_name: stream.codec_name,
width: stream.width,
height: stream.height,
duration: stream.duration,
})),
content,
};
}
async function main() {
const filePath = process.argv[2];
if (!filePath) {
console.error("Please provide a file path");
process.exit(1);
}
const result = await getVideoInfo(filePath);
if (!result) {
console.error("Failed to get video info");
process.exit(1);
}
const stream = result.streams[0];
const format = result.format;
console.log("[summary]");
console.log("format filename: ", format.filename);
console.log("format format_name: ", format.format_name);
console.log("format duration: ", format.duration);
console.log("format size: ", format.size);
console.log("");
console.log("stream 0 codec: ", stream.codec_name);
console.log("stream 0 width: ", stream.width);
console.log("stream 0 height: ", stream.height);
console.log("stream 0 duration: ", stream.duration);
console.log("");
console.log("[content]");
console.log("format: ", format);
console.log("");
console.log("stream[0]: ", stream);
}
main();
4. 실행
4.1. 코드 실행 예시
코드 실행의 예시는 다음과 같습니다.
C:\workspace_ffprobe>npx ts-node probe.ts video_sample.mp4
Need to install the following packages:
ts-node@10.9.2
Ok to proceed? (y) y
[summary]
format filename: video_sample.mp4
format format_name: mov,mp4,m4a,3gp,3g2,mj2
format duration: 14.466667
format size: 191772
stream 0 codec: h264
stream 0 width: 1222
stream 0 height: 638
stream 0 duration: 14.466667
[content]
format: {
filename: 'video_sample.mp4',
format_name: 'mov,mp4,m4a,3gp,3g2,mj2',
duration: '14.466667',
size: '191772'
}
stream[0]: { codec_name: 'h264', width: 1222, height: 638, duration: '14.466667' }
C:\workspace_ffprobe>
5. 결론
이상으로 TypeScript 코드로 비디오 정보를 가져오는 기능을 소개했습니다.
비디오의 재생시간 등을 가져오는 용도로 사용할 수 있습니다.
6. 더 보기
npm 패키지 fluent-ffmpeg를 사용하면 별도의 ffprobe 프로세스를 실행하지 않고 정보를 가져올 수 있습니다.
https://www.npmjs.com/package/fluent-ffmpeg
fluent-ffmpeg
A fluent API to FFMPEG (http://www.ffmpeg.org). Latest version: 2.1.3, last published: a year ago. Start using fluent-ffmpeg in your project by running `npm i fluent-ffmpeg`. There are 1741 other projects in the npm registry using fluent-ffmpeg.
www.npmjs.com
'Node.js' 카테고리의 다른 글
JavaScript 코드로 비디오 정보 가져오기 (0) | 2025.08.15 |
---|---|
Node.js 설치 (0) | 2022.03.08 |
puppeteer 시작하기 (0) | 2022.03.03 |
nodejs에서 json파일 읽기 (0) | 2022.03.01 |
Node.js가 설치된 경로 (0) | 2022.02.26 |