[ PROMPT_NODE_23408 ]
get-video-dimensions
[ SKILL_DOCUMENTATION ]
# 使用 Mediabunny 获取视频尺寸
Mediabunny 可以提取视频文件的宽度和高度。它适用于浏览器、Node.js 和 Bun 环境。
## 获取视频尺寸
tsx
import { Input, ALL_FORMATS, UrlSource } from "mediabunny";
export const getVideoDimensions = async (src: string) => {
const input = new Input({
formats: ALL_FORMATS,
source: new UrlSource(src, {
getRetryDelay: () => null,
}),
});
const videoTrack = await input.getPrimaryVideoTrack();
if (!videoTrack) {
throw new Error("未找到视频轨道");
}
return {
width: videoTrack.displayWidth,
height: videoTrack.displayHeight,
};
};
## 用法
tsx
const dimensions = await getVideoDimensions("https://remotion.media/video.mp4");
console.log(dimensions.width); // 例如 1920
console.log(dimensions.height); // 例如 1080
## 使用本地文件
对于本地文件,请使用 `FileSource` 代替 `UrlSource`:
tsx
import { Input, ALL_FORMATS, FileSource } from "mediabunny";
const input = new Input({
formats: ALL_FORMATS,
source: new FileSource(file), // 来自 input 或拖拽的 File 对象
});
const videoTrack = await input.getPrimaryVideoTrack();
const width = videoTrack.displayWidth;
const height = videoTrack.displayHeight;
## 在 Remotion 中结合 staticFile 使用
tsx
import { staticFile } from "remotion";
const dimensions = await getVideoDimensions(staticFile("video.mp4"));