Skip to content

🎥 Make videos programmatically with React

@remotion/webcodecs: Add documentation for saving converted videos

copilot-swe-agent[bot]d36b19a7b030
diff --git a/packages/docs/docs/webcodecs/convert-a-video.mdx b/packages/docs/docs/webcodecs/convert-a-video.mdx
index c3ddfabab6..48ca47ed80 100644
--- a/packages/docs/docs/webcodecs/convert-a-video.mdx
+++ b/packages/docs/docs/webcodecs/convert-a-video.mdx
@@ -66,10 +66,12 @@ Install the `@remotion/webcodecs` and `@remotion/media-parser` packages:
```tsx twoslash title="Converting an MP4 to a WebM"
import {convertMedia} from '@remotion/webcodecs';
-await convertMedia({
+const result = await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
});
+
+const blob = await result.save();
```
### Converting from a [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File):
@@ -80,10 +82,12 @@ import {convertMedia} from '@remotion/webcodecs';
// Get an actual file from an <input type="file"> element
const file = new File([], 'video.mp4');
-await convertMedia({
+const result = await convertMedia({
src: file,
container: 'webm',
});
+
+const blob = await result.save();
```
## Specifying the output codec
@@ -93,12 +97,86 @@ You can specify the output codec by passing the [`videoCodec`](/docs/webcodecs/c
```tsx twoslash title="Converting to VP9"
import {convertMedia} from '@remotion/webcodecs';
-await convertMedia({
+const result = await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
videoCodec: 'vp9',
audioCodec: 'opus',
});
+
+const blob = await result.save();
+```
+
+## Saving the converted video
+
+The `convertMedia()` function returns a result object with a `save()` method that you need to call to get the converted video as a `Blob`.
+
+### Download the converted video
+
+```tsx twoslash title="Download converted video"
+import {convertMedia} from '@remotion/webcodecs';
+
+const result = await convertMedia({
+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
+ container: 'webm',
+});
+
+const blob = await result.save();
+
+// Create download link
+const url = URL.createObjectURL(blob);
+const link = document.createElement('a');
+link.href = url;
+link.download = 'converted-video.webm';
+document.body.appendChild(link);
+link.click();
+document.body.removeChild(link);
+URL.revokeObjectURL(url);
+```
+
+### Upload the converted video
+
+```tsx twoslash title="Upload converted video"
+import {convertMedia} from '@remotion/webcodecs';
+
+const result = await convertMedia({
+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
+ container: 'webm',
+});
+
+const blob = await result.save();
+
+// Upload to server
+const formData = new FormData();
+formData.append('video', blob, 'converted-video.webm');
+
+await fetch('/api/upload', {
+ method: 'POST',
+ body: formData,
+});
+```
+
+### Display the converted video
+
+```tsx twoslash title="Display converted video"
+import {convertMedia} from '@remotion/webcodecs';
+
+const result = await convertMedia({
+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
+ container: 'webm',
+});
+
+const blob = await result.save();
+
+// Display in video element
+const url = URL.createObjectURL(blob);
+const video = document.createElement('video');
+video.src = url;
+video.controls = true;
+document.body.appendChild(video);
+
+// Don't forget to clean up when done
+// URL.revokeObjectURL(url);
```
## Advanced conversions
diff --git a/packages/docs/docs/webcodecs/convert-media.mdx b/packages/docs/docs/webcodecs/convert-media.mdx
index 43e97a829b..93529bee51 100644
--- a/packages/docs/docs/webcodecs/convert-media.mdx
+++ b/packages/docs/docs/webcodecs/convert-media.mdx
@@ -27,12 +27,63 @@ Re-encodes a video using [WebCodecs](https://developer.mozilla.org/en-US/docs/We
```tsx twoslash title="Converting a video"
import {convertMedia} from '@remotion/webcodecs';
-await convertMedia({
+const result = await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
});
+
+// Save the converted video as a Blob
+const blob = await result.save();
+```
+
+## Return value
+
+`convertMedia()` returns a Promise that resolves to a `ConvertMediaResult` object with the following properties:
+
+### `save()`
+
+_Function that returns `Promise<Blob>`_
+
+Call this function to get the converted video as a `Blob`. You can then use this blob to:
+
+- Create a download link
+- Upload to a server
+- Display in a video element
+- Store in IndexedDB
+
+```tsx twoslash title="Saving and downloading a converted video"
+import {convertMedia} from '@remotion/webcodecs';
+
+const result = await convertMedia({
+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
+ container: 'webm',
+});
+
+const blob = await result.save();
+
+// Create a download link
+const url = URL.createObjectURL(blob);
+const a = document.createElement('a');
+a.href = url;
+a.download = 'converted-video.webm';
+document.body.appendChild(a);
+a.click();
+document.body.removeChild(a);
+URL.revokeObjectURL(url);
```
+### `remove()`
+
+_Function that returns `Promise<void>`_
+
+Call this function to clean up any temporary resources created during the conversion process.
+
+### `finalState`
+
+_object of type `ConvertMediaProgress`_
+
+Contains the final state of the conversion process, including statistics about the conversion.
+
## API
### `src`
@@ -276,7 +327,7 @@ This feature is inherited from [`parseMedia()`](/docs/media-parser/parse-media),
```tsx twoslash title="Converting a video"
import {convertMedia} from '@remotion/webcodecs';
-await convertMedia({
+const result = await convertMedia({
src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
container: 'webm',
videoCodec: 'vp8',
@@ -289,6 +340,8 @@ await convertMedia({
// ^?
},
});
+
+const blob = await result.save();
```
## License