Skip to main content

parseMedia()

属于 @remotion/media-parser 包的一部分。 从 v4.0.190 起可用

🌐 Part of the @remotion/media-parser package. available from v4.0.190

示例

🌐 Examples

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}
Parsing a local file
import {parseMedia} from '@remotion/media-parser'; import {nodeReader} from '@remotion/media-parser/node'; const result = await parseMedia({ src: '/Users/jonnyburger/Downloads/video.mp4', fields: { durationInSeconds: true, dimensions: true, }, reader: nodeReader, });

应用编程接口

🌐 API

warning

不稳定的 API:此软件包是实验性的。API 将来可能会发生变化。
获取视频元数据的 API 是稳定的,可以在生产环境中使用。

src

可以是本地文件路径、URL,或者 FileBlob 对象。

🌐 Either a local file path, or a URL, or a File or Blob object.

如果你传递一个本地文件路径,你还必须将 nodeReader 作为 reader 参数传递。

🌐 If you pass a local file path, you must also pass nodeReader as the reader argument.

fields?

一个指定你希望接收哪些字段的对象。
如果你希望接收该字段,请将 true 作为值传入。

🌐 An object specifying which fields you'd like to receive.
If you like to receive the field, pass true as the value.

请参阅 Available Fields 以获取可用字段列表。

🌐 See Available Fields for a list of available fields.

reader?

一个阅读器界面。

🌐 A reader interface.

如果你通过 nodeReader,你必须将本地文件路径作为 src 参数传入。

🌐 If you pass nodeReader, you must pass a local file path as the src argument.

controller?

一个 controller 对象,允许你暂停、恢复和中止解析过程。

🌐 A controller object that allows you to pause, resume and abort the parsing process.

onVideoTrack?

当检测到视频轨道时调用的回调。 它接收一个包含以下内容的对象:

你必须返回 null 或者一个回调,该回调会针对每个对应于视频轨道的样本被调用。

🌐 You must return either null or a callback that is called for each sample that corresponds to the video track.

sample 的类型是 MediaParserVideoSample,它与 WebCodecs EncodedVideoChunk 构造函数的必填字段兼容。

🌐 The sample has the type MediaParserVideoSample, which is compatible with the mandatory fields for the WebCodecs EncodedVideoChunk constructor.

Reading video frames
import {parseMedia, MediaParserOnVideoTrack} from '@remotion/media-parser'; const onVideoTrack: MediaParserOnVideoTrack = ({track}) => { console.log(track); return (sample) => { console.log(new EncodedVideoChunk(sample)); }; };

onAudioTrack?

当检测到音频轨道时调用的回调函数。 它接收一个包含以下内容的对象:

你必须返回 null 或一个回调函数,该回调函数会针对与音轨对应的每个样本被调用。

🌐 You must return either null or a callback that is called for each sample that corresponds to the audio track.

sample 的类型是 MediaParserAudioSample,它与 WebCodecs EncodedAudioChunk 构造函数的必填字段兼容。

🌐 The sample has the type MediaParserAudioSample, which is compatible with the mandatory fields for the WebCodecs EncodedAudioChunk constructor.

Reading audio frames
import {parseMedia, MediaParserOnAudioTrack} from '@remotion/media-parser'; const onAudioTrack: MediaParserOnAudioTrack = ({track}) => { console.log(track); return (sample) => { console.log(new EncodedAudioChunk(sample)); }; };

selectM3uStream?

当检测到具有多个流的 .m3u8 文件时调用的回调函数。
示例见 流选择

🌐 A callback that is called when a .m3u8 file is detected which has multiple streams.
See Stream selection for an example.

selectM3uAssociatedPlaylists?

在选择 M3U 流之后调用的回调,允许你选择应该解析的辅助播放列表。通常用于选择音频轨道。 请参阅 流选择 了解示例。

🌐 A callback that is called after a M3U stream has been selected, allowing you to pick which auxiliary playlists should be parsed. Usually this is used for selecting an audio track.
See Stream selection for an example.

onParseProgress?

当从媒体中读取了字节时,会不时调用的回调函数。它包括以下数据:

🌐 A callback that is called from time to time when bytes have been read from the media.
It includes the following data:

import {ParseMediaProgress} from '@remotion/media-parser';
(alias) type ParseMediaProgress = { bytes: number; percentage: number | null; totalBytes: number | null; } import ParseMediaProgress
note

你可以将其设为异步函数,并且在它未解决时,解析过程将会暂停。 这在你想在进度步骤之间添加短暂延迟以保持界面交互时非常有用。

progressIntervalInMs?

number

以毫秒为单位的间隔,用于调用 onParseProgress 回调。
默认值为 100。设置为 0 可实现不受限制和同步更新。
请注意,更新触发的频率很高,频繁更新 DOM 可能会降低转换过程的速度。

🌐 The interval in milliseconds at which the onParseProgress callback is called.
Default 100. Set to 0 for unthrottled and synchronous updates.
Note that updates are fired very often and updating the DOM often may slow down the conversion process.

makeSamplesStartAtZero

boolean

如果设置为 true,返回样本的时间戳将被重新计算,使其从 0 开始,而不是文件中包含的时间戳。默认值为 true

🌐 If this is set to true, the timestamps of the samples returned will be recalculated so they start at 0 instead of the timestamps that are contained in the file. Default true.

seekingHints?

包含有关媒体文件结构的提示的对象。

🌐 An object that contains hints about the structure of the media file.

有关更多信息,请参阅 寻求提示

🌐 See Seeking Hints for more information.

logLevel?

"error""warn""info""debug""trace" 之一。
默认值:"info",仅记录重要信息。

🌐 One of "error", "warn", "info", "debug", "trace".
Default value: "info", which logs only important information.

acknowledgeRemotionLicense?

确认 Remotion 许可证 以使控制台消息消失。

🌐 Acknowledge the Remotion License to make the console message disappear.

回调

🌐 Callbacks

每个字段还有一个回调,允许你在获取值后立即检索,而无需等待函数解析完成。

🌐 Each field also has a callback that allows you to retrieve the value as soon as it is obtained without waiting for the function to resolve.

如果使用回调,则不必将字段添加到 fields 对象中。
但是,就像使用 fields 一样,为 慢字段 添加回调可能需要读取更多文件内容。

🌐 You do not have to add the field to the fields object if you use the callback.
However, just like with fields, adding a callback for a slow field may require reading more of the file.

Using a callback
import {parseMedia} from '@remotion/media-parser'; const result = await parseMedia({ src: 'https://remotion.media/video.mp4', onDurationInSeconds: (durationInSeconds) => { console.log(durationInSeconds); }, onDimensions: (dimensions) => { console.log(dimensions); }, });

另请参阅

🌐 See also