downloadAndParseMedia()
与parseMedia()相同,但也将媒体文件下载到磁盘上。
🌐 Same as parseMedia(), but also downloads the media file to disk.
打算用于 Node.js 和 Bun。
🌐 Meant to be used in Node.js and Bun.
Download a fileimport {downloadAndParseMedia } from '@remotion/media-parser'; import {nodeWriter } from '@remotion/media-parser/node-writer'; awaitdownloadAndParseMedia ({src : 'https://www.w3schools.com/html/mov_bbb.mp4',writer :nodeWriter ('output.mp4'), });
你可以通过将它们传递给 fields 对象来获取像 tracks 和 duration 这样的字段。
🌐 You can obtain fields like tracks and duration by passing them to the fields object.
Download a file and get metadataimport {downloadAndParseMedia } from '@remotion/media-parser'; import {nodeWriter } from '@remotion/media-parser/node-writer'; const {durationInSeconds ,tracks } = awaitdownloadAndParseMedia ({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 longimport {downloadAndParseMedia } from '@remotion/media-parser'; import {nodeWriter } from '@remotion/media-parser/node-writer'; awaitdownloadAndParseMedia ({src : 'https://s3.amazonaws.com/bucket/uploaded-asset.mp4',writer :nodeWriter ('output.mp4'),onDurationInSeconds : (duration ) => { if (duration &&duration > 600) { throw newError ('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 errorimport {downloadAndParseMedia } from '@remotion/media-parser'; import {nodeWriter } from '@remotion/media-parser/node-writer'; awaitdownloadAndParseMedia ({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'}; }, });
应用编程接口
🌐 API
[parseMedia()](/docs/media-parser/parse-media) 的所有相同参数均可用,外加:
🌐 All of the same parameters for parseMedia() are available, plus:
writer
用于将下载的文件写入磁盘的写入器。目前可用:
🌐 The writer to use to write the downloaded file to disk. Currently available:
nodeWriter来自@remotion/media-parser/node-writer:使用 Node.js 的fs模块写入磁盘。
onError
当发生错误时调用的函数。它接收错误作为参数。 你必须返回以下之一:
🌐 A function that is called when an error occurs. It receives the error as an argument.
You must return one of the following:
{action: 'download'}:尽管出现错误,仍然继续下载文件。{action: 'fail'}:中止下载,删除文件并立即抛出错误。
另请参见上面的示例。
该函数可能是异步的,解析会暂停,直到它完成。
🌐 See also the example above.
The function may be async, parsing is paused until it resolves.
另请参阅
🌐 See also