Skip to main content

使用 @remotion/media-parser 从视频获取元数据

🌐 Not recommended anymore

我们正在逐步淘汰 Media Parser,并转向使用 Mediabunny
请参阅:使用 Mediabunny 获取元数据 以了解新的推荐方法。


使用@remotion/media-parser,你可以从各种媒体格式获取元数据。

🌐 Using @remotion/media-parser, you can get metadata from a variety of media formats.

从 URL 获取元数据

🌐 Getting metadata from a URL

指定你想要阅读的 fields 以及作为 src 的 URL。
这在浏览器以及 Node.js 和 Bun 中都有效。

🌐 Specify the fields you would like to read and the URl as src.
This works in the browser as well as in Node.js and Bun.

Parsing a hosted video
import {parseMedia} from '@remotion/media-parser'; const result = await parseMedia({ src: 'https://remotion.media/video.mp4', fields: { durationInSeconds: true, dimensions: true, }, }); console.log(result.durationInSeconds); // 10 console.log(result.dimensions); // {width: 1920, height: 1080}

从本地路径获取元数据

🌐 Getting metadata from a local path

使用 nodeReader 从文件系统读取文件。
这可以在 Node.js 和 Bun 中使用。

🌐 Use nodeReader to read a file from a filesystem.
This can be used in Node.js and Bun.

Parsing a video from a file path
import {parseMedia} from '@remotion/media-parser'; import {nodeReader} from '@remotion/media-parser/node'; const result = await parseMedia({ src: '/tmp/video.mp4', fields: { durationInSeconds: true, dimensions: true, }, reader: nodeReader, }); console.log(result.durationInSeconds); // 10 console.log(result.dimensions); // {width: 1920, height: 1080}

File 获取元数据

🌐 Getting metadata from a File

如果你在浏览器中接收用户上传,它们将以File的形式出现。

🌐 If you take user uploads in the browser, they will be in the form of a File.

Parsing a video from a file path
import {parseMedia} from '@remotion/media-parser'; // You would get this from a `<input type="file">` const file = new File([], 'video.mp4'); const result = await parseMedia({ src: file, fields: { durationInSeconds: true, dimensions: true, }, }); console.log(result.durationInSeconds); // 10 console.log(result.dimensions); // {width: 1920, height: 1080}

可用字段

🌐 Available fields

你可以读取视频文件的时长、尺寸、帧率、容器格式、编解码器、ID3标签及更多信息。

🌐 You can read the duration, the dimensions, the framerate, the container format, the codecs, the ID3 tags and more information from a video file.

请参见 可用字段 获取完整列表。

🌐 See Available Fields for a complete list.

尽快获取元数据

🌐 Getting metadata as soon as possible

Parsing a video from a File
import {parseMedia} from '@remotion/media-parser'; const result = await parseMedia({ src: 'https://remotion.media/video.mp4', fields: { durationInSeconds: true, dimensions: true, }, onDurationInSeconds: (durationInSeconds) => { console.log(durationInSeconds); // 10 }, }); console.log(result.dimensions); // {width: 1920, height: 1080}

另请参阅

🌐 See also