Skip to main content

同时下载和解析视频或音频

@remotion/media-parser 允许你将远程视频下载到磁盘,并且在媒体下载的同时,一旦元数据可用就可以立即获取它。

这是一个使用 downloadAndParseMedia() API 的示例:

🌐 Here is an example using the downloadAndParseMedia() API:

Download a file and get metadata
import {downloadAndParseMedia} from '@remotion/media-parser'; import {nodeWriter} from '@remotion/media-parser/node-writer'; const {durationInSeconds, tracks} = await downloadAndParseMedia({ src: 'https://s3.amazonaws.com/bucket/uploaded-asset.mp4', writer: nodeWriter('output.mp4'), fields: { durationInSeconds: true, tracks: true, }, }); // If here was reached, file is downloaded! console.log(durationInSeconds); console.log(tracks);

你可以使用回调函数在信息一旦可用时立即获取。
抛出错误以停止下载。

🌐 You can use callback functions to retrieve information as soon as it is available.
Throw an error to stop the download.

Stop the download if the video is too long
import {downloadAndParseMedia} from '@remotion/media-parser'; import {nodeWriter} from '@remotion/media-parser/node-writer'; await downloadAndParseMedia({ src: 'https://s3.amazonaws.com/bucket/uploaded-asset.mp4', writer: nodeWriter('output.mp4'), onDurationInSeconds: (duration) => { if (duration && duration > 600) { throw new Error('Video is too long'); } }, });

如果发生错误(包括你自己抛出的错误),你可以使用 onError 决定如何处理。

🌐 If an error occurs (including one you've thrown yourself), you can decide what to do using onError.

Continue download despite error
import {downloadAndParseMedia} from '@remotion/media-parser'; import {nodeWriter} from '@remotion/media-parser/node-writer'; await downloadAndParseMedia({ src: 'https://s3.amazonaws.com/bucket/uploaded-asset.mp4', writer: nodeWriter('output.mp4'), onError: (error) => { // Force the file to be downloaded despite parsing error. // Note: At the end, the error will be thrown nonetheless. return {action: 'download'}; // Default behavior: // Abort the download, delete the file and throw the error immediately. // return {action: 'fail'}; }, });

另请参阅

🌐 See also