Skip to content

🎥 Make videos programmatically with React

finish documentation

Jonny Burger4d0657e8f148
diff --git a/packages/docs/docs/media-parser/download-and-parse-media.mdx b/packages/docs/docs/media-parser/download-and-parse-media.mdx
index 34a216a5c2..c57ccc6ab1 100644
--- a/packages/docs/docs/media-parser/download-and-parse-media.mdx
+++ b/packages/docs/docs/media-parser/download-and-parse-media.mdx
@@ -6,7 +6,8 @@ slug: /media-parser/download-and-parse-media
crumb: '@remotion/media-parser'
---
-Same as [`parseMedia()`](/docs/media-parser/parse-media), but also downloads the media file to disk.
+Same as [`parseMedia()`](/docs/media-parser/parse-media), but also downloads the media file to disk.
+
Meant to be used in Node.js and Bun.
```tsx title="Download a file"
@@ -21,15 +22,31 @@ await downloadAndParseMedia({
You can obtain [fields](/docs/media-parser/parse-media#fields) like `tracks` and `duration` by passing them to the `fields` object.
-```tsx title="Download a file and get the duration"
+```tsx title="Download a file and get metadata"
import {downloadAndParseMedia} from '@remotion/media-parser';
-const {duration} = await downloadAndParseMedia({
+const {durationInSeconds, tracks} = await downloadAndParseMedia({
src: 'https://s3.amazonaws.com/bucket/uploaded-asset.mp4',
writer: nodeWriter('output.mp4'),
- onDimensions: () => {
- console.log('Got dimensions!');
+ fields: {
+ durationInSeconds: true,
+ tracks: true,
},
+});
+// If here was reached, file is downloaded!
+console.log(durationInSeconds);
+console.log(tracks);
+```
+
+You can use callback functions to listen when new fields are available.
+Throw an error to stop the download.
+
+```tsx title="Stop the download if the video is too long"
+import {downloadAndParseMedia} from '@remotion/media-parser';
+
+const {duration} = await downloadAndParseMedia({
+ src: 'https://s3.amazonaws.com/bucket/uploaded-asset.mp4',
+ writer: nodeWriter('output.mp4'),
onDuration: (duration) => {
if (duration && duration > 600) {
throw new Error('Video is too long');
@@ -37,3 +54,49 @@ const {duration} = await downloadAndParseMedia({
},
});
```
+
+If an error occurs (including one you've thrown yourself), you can decide what to do using [`onError`](#onerror).
+
+```tsx title="Continue download despite error"
+import {downloadAndParseMedia} from '@remotion/media-parser';
+
+const {duration} = 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'};
+ },
+});
+```
+
+## API
+
+All of the same parameters for [`parseMedia()`](/docs/media-parser/parse-media) are available, plus:
+
+### `writer`
+
+The writer to use to write the downloaded file to disk. Currently available:
+
+- `nodeWriter` from `@remotion/media-parser/node-writer`: Writes to disk using Node.js's `fs` module.
+
+### `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'}`: Continue downloading the file despite the error.
+- `{action: 'fail'}`: Abort the download, delete the file and throw the error immediately.
+
+See also the example above.
+The function may be async, parsing is paused until it resolves.
+
+## See also
+
+- [`parseMedia()`](/docs/media-parser/parse-media)
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/media-parser/src/download-and-parse-media.ts)