1. 개요
JavaScript 코드로 비디오 정보를 가져오는 기능은 ffprobe를 사용하여 짧은 코드로 구현할 수 있습니다.
JavaScript 코드에서 ffprobe를 실행하여 비디오 정보를 가져오는 코드를 소개합니다.
1.1. 사용 환경
사용한 환경은 다음과 같습니다.
- 운영체제 : windows10 Pro 64비트
- 비디오 파일의 정보를 가져오는 도구 : ffprobe
- 실행코드 작성 언어 : JavaScript
- 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.js # 비디오 정보를 가져오기 자바스크립트 코드
└─── video_sample.mp4 # 샘플 비디오 파일
3.2. 자바스크립트 소스 코드
probe.js:
import { spawn } from "node:child_process";
async function getVideoInfo(filePath) {
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;
return out;
}
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);
const videoInfo = JSON.parse(result);
const stream = videoInfo.streams[0];
const format = videoInfo.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>node probe.js video_sample.mp4
[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',
nb_streams: 1,
nb_programs: 0,
nb_stream_groups: 0,
format_name: 'mov,mp4,m4a,3gp,3g2,mj2',
format_long_name: 'QuickTime / MOV',
start_time: '0.000000',
duration: '14.466667',
size: '191772',
bit_rate: '106049',
probe_score: 100,
tags: {
major_brand: 'isom',
minor_version: '512',
compatible_brands: 'isomiso2avc1mp41',
encoder: 'Lavf61.7.100'
}
}
stream[0]: {
index: 0,
codec_name: 'h264',
codec_long_name: 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
profile: 'Constrained Baseline',
codec_type: 'video',
codec_tag_string: 'avc1',
codec_tag: '0x31637661',
width: 1222,
height: 638,
coded_width: 1222,
coded_height: 638,
has_b_frames: 0,
pix_fmt: 'yuv420p',
level: 31,
chroma_location: 'left',
field_order: 'progressive',
refs: 1,
is_avc: 'true',
nal_length_size: '4',
id: '0x1',
r_frame_rate: '30/1',
avg_frame_rate: '30/1',
time_base: '1/15360',
start_pts: 0,
start_time: '0.000000',
duration_ts: 222208,
duration: '14.466667',
bit_rate: '104625',
bits_per_raw_sample: '8',
nb_frames: '434',
extradata_size: 39,
disposition: {
default: 1,
dub: 0,
original: 0,
comment: 0,
lyrics: 0,
karaoke: 0,
forced: 0,
hearing_impaired: 0,
visual_impaired: 0,
clean_effects: 0,
attached_pic: 0,
timed_thumbnails: 0,
non_diegetic: 0,
captions: 0,
descriptions: 0,
metadata: 0,
dependent: 0,
still_image: 0,
multilayer: 0
},
tags: {
language: 'und',
handler_name: 'VideoHandler',
vendor_id: '[0][0][0][0]',
encoder: 'Lavc61.19.101 libx264'
}
}
C:\workspace_ffprobe>
5. 결론
이상으로 JavaScript 코드로 비디오 정보를 가져오는 기능을 소개했습니다.
비디오의 재생시간 등을 가져오는 용도로 사용할 수 있습니다.
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' 카테고리의 다른 글
TypeScript 코드로 비디오 정보 가져오기 (8) | 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 |