转换视频
你可以使用 @remotion/webcodecs 的 convertMedia() 功能,在浏览器中将视频从一种格式转换为另一种格式。
🌐 You can convert a video in the browser from one format to another using the convertMedia() function from @remotion/webcodecs.
💼 重要许可免责声明
We consider a team of 4 or more people a "company".
In a future version of
@remotion/webcodecs, this package will also require the purchase of a newly created "WebCodecs Conversion Seat". Get in touch with us if you are planning to use this package.支持以下输入格式:
🌐 The following input formats are supported:
- ISO 基础媒体(
.mp4,.mov,.m4a) - Matroska(
.mkv,.webm) .avi- MPEG传输流(
.ts) .wav,.mp3.flac.aac- HLS(
.m3u8)
支持以下输出格式:
🌐 The following output formats are supported:
- MP4
- webm
- WAV
支持以下输出视频编解码器:
🌐 The following output video codecs are supported:
- VP8(仅限 WebM)
- VP9(仅限 WebM)
- H.264(仅限 MP4)
支持以下输出音频编解码器:
🌐 The following output audio codecs are supported:
- Opus(仅限 WebM)
- AAC(仅限 MP4)
- PCM(仅限 WAV)
安装
🌐 Installation
安装 @remotion/webcodecs 和 @remotion/media-parser 包:
🌐 Install the @remotion/webcodecs and @remotion/media-parser packages:
- Remotion CLI
- npm
- bun
- pnpm
- yarn
npx remotion add @remotion/webcodecs @remotion/media-parser
This assumes you are currently using v4.0.431 of Remotion.npm i --save-exact @remotion/webcodecs@4.0.431 @remotion/media-parser@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.431 of Remotion.pnpm i @remotion/webcodecs@4.0.431 @remotion/media-parser@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.431 of Remotion.bun i @remotion/webcodecs@4.0.431 @remotion/media-parser@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.This assumes you are currently using v4.0.431 of Remotion.yarn --exact add @remotion/webcodecs@4.0.431 @remotion/media-parser@4.0.431
Also update
remotion and all `@remotion/*` packages to the same version.Remove all
^ character in front of the version numbers of it as it can lead to a version conflict.🚧 不稳定的 API
We might change the API at any time, until we remove this notice.
基本转换
🌐 Basic conversions
从 URL 转换
🌐 Converting from an URL
(需要启用 CORS)
Converting an MP4 to a WebMimport {convertMedia } from '@remotion/webcodecs'; constresult = awaitconvertMedia ({src : 'https://remotion.media/BigBuckBunny.mp4',container : 'webm', }); constblob = awaitresult .save ();
从 File 转换:
🌐 Converting from a File:
Converting a fileimport {convertMedia } from '@remotion/webcodecs'; // Get an actual file from an <input type="file"> element constfile = newFile ([], 'video.mp4'); constresult = awaitconvertMedia ({src :file ,container : 'webm', }); constblob = awaitresult .save ();
指定输出编解码器
🌐 Specifying the output codec
你可以通过传递 videoCodec 和 audioCodec 选项来指定输出编解码器:
🌐 You can specify the output codec by passing the videoCodec and audioCodec options:
Converting to VP9import {convertMedia } from '@remotion/webcodecs'; constresult = awaitconvertMedia ({src : 'https://remotion.media/BigBuckBunny.mp4',container : 'webm',videoCodec : 'vp9',audioCodec : 'opus', }); constblob = awaitresult .save ();
正在保存已转换的视频
🌐 Saving the converted video
convertMedia() 函数返回一个结果对象,该对象具有一个 save() 方法,你需要调用该方法以获取转换后的视频,格式为 Blob。
🌐 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
Download converted videoimport {convertMedia } from '@remotion/webcodecs'; constresult = awaitconvertMedia ({src : 'https://remotion.media/BigBuckBunny.mp4',container : 'webm', }); constblob = awaitresult .save (); // Create download link consturl =URL .createObjectURL (blob ); constlink =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
Upload converted videoimport {convertMedia } from '@remotion/webcodecs'; constresult = awaitconvertMedia ({src : 'https://remotion.media/BigBuckBunny.mp4',container : 'webm', }); constblob = awaitresult .save (); // Upload to server constformData = newFormData ();formData .append ('video',blob , 'converted-video.webm'); awaitfetch ('/api/upload', {method : 'POST',body :formData , });
显示已转换的视频
🌐 Display the converted video
Display converted videoimport {convertMedia } from '@remotion/webcodecs'; constresult = awaitconvertMedia ({src : 'https://remotion.media/BigBuckBunny.mp4',container : 'webm', }); constblob = awaitresult .save (); // Display in video element consturl =URL .createObjectURL (blob ); constvideo =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
请参阅 Track Transformation 了解如何获得更多关于转换的控制。
🌐 See Track Transformation for how you can get more control over the conversion.
另请参阅
🌐 See also