自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用React創(chuàng)建視頻和動畫

開發(fā) 前端
Remotion是一個最近推出的庫,它允許您使用 React 創(chuàng)建視頻和動態(tài)圖形。作為一名 Web 開發(fā)人員,我發(fā)現(xiàn)它非常有趣,因為它為我們自己創(chuàng)建視頻和動畫打開了一扇新的大門。

本文轉(zhuǎn)載自微信公眾號「TianTianUp」,作者小弋。轉(zhuǎn)載本文請聯(lián)系TianTianUp公眾號。

大家好,我是小弋。

分享的內(nèi)容是:

如何使用 React Remotion 來創(chuàng)建視頻的,如果你之前對視頻很感興趣的話,這篇文章可以參考。

正文

Remotion是一個最近推出的庫,它允許您使用 React 創(chuàng)建視頻和動態(tài)圖形。作為一名 Web 開發(fā)人員,我發(fā)現(xiàn)它非常有趣,因為它為我們自己創(chuàng)建視頻和動畫打開了一扇新的大門。

它的官網(wǎng):

https://www.remotion.dev/

簡介

Source: https://www.remotion.dev/

正如我提到的,Remotion是最近推出的一個令人興奮的庫,它允許你使用你最喜歡的網(wǎng)絡(luò)技術(shù),如HTML、CSS、JavaScript、TypeScript等來創(chuàng)建視頻和動畫。

除此之外,你還可以使用你所有關(guān)于編程、函數(shù)、算法、API的知識來為視頻添加各種效果。作為一個基于React的庫,Remotion能夠最大限度地利用Reacts的特性,如可重用的組件、強大的組合和快速重載。

Remotion還配備了一個被稱為Remotion Player的播放器,它給你帶來了真正的視頻編輯器的感覺,它可以用瀏覽器來播放和審查你的視頻。

如何設(shè)置Remotion?

創(chuàng)建一個新的Remotion項目是非常簡單的。但有兩個依賴項你應(yīng)該先安裝。

步驟1:安裝NodeJS和FFMPEG

由于安裝NodeJS是非常常見的,我將重點介紹安裝FFMPEG。首先,你需要從他們的下載頁面下載合適版本的FFMPEG。

FFMPEG Downloads page.

然后將其解壓到你選擇的文件夾中,并在CMD中以管理員權(quán)限運行以下命令(在windows中)來更新你的路徑變量。

  1. setx /M PATH "path\to\ffmpeg\bin;%PATH%"  

第2步:啟動新項目

安裝完上述依賴后,初始化一個新的Remotion視頻只需要一個命令,你可以使用yarn或npm來實現(xiàn)。

  1. yarn create video 
  2. or 
  3. npm init video 

你已經(jīng)成功地初始化了你的第一個Remotion項目,你可以使用npm run start來啟動該項目。

Default Remotion Project

Remotion的基礎(chǔ)知識

既然你已經(jīng)啟動了你的Remotion項目,你可以開始創(chuàng)建你的視頻。但我認為在這之前,如果你對Remotion的基礎(chǔ)知識有一定的了解會更好。

Video Properties

Width, height, durationInFrames, fps是由Remotion提供的視頻屬性。

你可以在組件中使用這些屬性來配置組件的像素大小,該組件應(yīng)該播放多少幀,以及每秒鐘的幀數(shù)。

  1. import { useVideoConfig } from “remotion”;export const ExampleVideo = () => { 
  2.  const { fps, durationInFrames, width, height } = useVideoConfig();return ( 
  3.  <div style={{ flex: 1, justifyContent: “center”, alignItems: “center” }}> 
  4.    This video is {durationInFrames / fps} seconds long. 
  5.  </div> 
  6.  ); 
  7. }; 

建議使用useVideoConfig派生這些屬性,就像上面的例子一樣,使你的組件可以重復(fù)使用。

Compositions

Compositions也是Remotion中的一種組件,在這里你可以使用上述屬性作為元數(shù)據(jù)。

  1. import {Composition} from 'remotion'
  2. import {HelloReaders} from './HelloReaders';export const RemotionVideo: React.FC = () => { 
  3.  return ( 
  4.    <> 
  5.     <Composition 
  6.      id=”HelloReaders” 
  7.      component={HelloReaders} 
  8.      durationInFrames={150} 
  9.      fps={30} 
  10.      width={1024} 
  11.      height={720} 
  12.      defaultProps={{ 
  13.       titleText: ‘Welcome to My Blog’, 
  14.       titleColor: ‘black’, 
  15.      }} 
  16.     /> 
  17.     <Composition 
  18.      ... 
  19.     /> 
  20.     <Composition 
  21.      ... 
  22.     /> 
  23.   </> 
  24.  ); 

如果你觀察項目中的Video.tsx文件,你會看到3個Composition組件,每個組件中都有元數(shù)據(jù),包括視頻屬性。

同時,這些組合也顯示在Remotion Player的左上角。

Compositions List

Animation Properties

當涉及到視頻時,動畫是最重要的,而Remotion為您提供了配置一些驚人的動畫的自由。例如,如果你需要一個簡單的臉部效果,你可以逐幀調(diào)整幀的不透明度。

  1. const frame = useCurrentFrame(); 
  2. const opacity = frame >= 20 ? 1 : (frame / 20); 
  3. return ( 
  4.  <div style={{ 
  5.    opacity: opacity 
  6.  }}> 
  7.   Hello Readers! 
  8.  </div> 

除此之外,Remotion還有2個內(nèi)建的函數(shù),名為interpolate和spring,你可以用它們來建立更高級的動畫。

插值函數(shù)接受4個輸入?yún)?shù),包括輸入值(主要是幀),輸入可以承擔(dān)的范圍值,你想把輸入映射到的數(shù)值范圍,以及一個可選參數(shù)。

彈簧動畫通過使動畫更自然,讓你在演示中更有創(chuàng)意。例如,下面的彈簧動畫配置會給你的文本添加一個小的縮放效果。

  1. const {fps} = useVideoConfig(); 
  2. const scale = spring({ 
  3.   fps, 
  4.   from: 0, 
  5.   to: 1, 
  6.   frame 
  7. });return ( 
  8.   <span 
  9.     style={{ 
  10.       color: titleColor, 
  11.       marginLeft: 10, 
  12.       marginRight: 10, 
  13.       transform: `scale(${scale})`, 
  14.       display: ‘inline-block’, 
  15.     }} 
  16.   > 
  17.   Welcome to My Blog 
  18.   </span> 

Spring animation

Sequence Component

Remotion中的 Sequence組件完成了2個主要任務(wù)。它主要是用來給視頻中的元素分配不同的時間框架。在保持元素之間的聯(lián)系的同時,它也允許你重復(fù)使用同一個組件。

Sequence組件是一個高階組件,它有能力容納子組件。除此之外,它還接受3個prop,包括2個必需的prop和1個可選的prop。

  • name : 這是一個可選的prop。你指定的名字將出現(xiàn)在Remotion播放器的時間線上。如果你使用正確的命名模式,你將能夠理解每個組件是如何連接的。

Timeline View of Remotion Player

  • from: 這定義了框架,該組件應(yīng)該出現(xiàn)在視頻中。
  • durationInFrames: 以幀為單位的Sequence組件的長度。 

例如,下面的Sequence組件將在20幀后出現(xiàn)在視頻中,并將持續(xù)到結(jié)束,因為durationOnFrames是無限的。

  1. <Sequence from={20} durationInFrames={Infinity}> 
  2.    <Title titleText={titleText} titleColor={titleColor} /></Sequence

由于你現(xiàn)在對Remotion中的幾個基本屬性和組件有了基本的了解,我們可以開始使用Remotion創(chuàng)建第一個視頻。

創(chuàng)建一個簡單的視頻

正如你在上面的例子中已經(jīng)看到的,我將創(chuàng)建一個簡單的視頻來顯示我的博客的標志和歡迎詞,并有一些動畫。

我將使用我們在文章開頭創(chuàng)建的默認項目布局。

步驟1

首先,我為我的視頻中的3個元素創(chuàng)建了3個組件:Logo.tsx, Title.tsx和SubText.tsx。

Logo.tsx file:

  1. import {spring, useCurrentFrame, useVideoConfig} from ‘remotion’; 
  2. import {Img} from ‘remotion’; 
  3. import image from ‘./logo.png’ 
  4. export const Logo: React.FC<{ 
  5. transitionStart: number; 
  6.  }> = ({transitionStart}) => { 
  7.     
  8.   const videoConfig = useVideoConfig(); 
  9.   const frame = useCurrentFrame(); 
  10.    return ( 
  11.    <div 
  12.     style={{ 
  13.      textAlign: ‘center’, 
  14.      marginTop: ‘10%’, 
  15.      width: videoConfig.width, 
  16.      height: videoConfig.height, 
  17.     }} 
  18.    > 
  19.    <Img  
  20.     style={{ 
  21.      transform:`scale(${spring({ 
  22.       fps: videoConfig.fps, 
  23.       frame: frame — transitionStart, 
  24.       config: { 
  25.        damping: 100, 
  26.        stiffness: 200, 
  27.        mass: 0.5, 
  28.       }, 
  29.      })})`, 
  30.     }}  
  31.     src={image}></Img> 
  32.    </div> 
  33.  ); 
  34. }; 

Title.tsx file:

  1. import {spring, useCurrentFrame, useVideoConfig} from 'remotion';export const Title: React.FC<{ 
  2.  titleText: string; 
  3.  titleColor: string; 
  4. }> = ({titleText, titleColor}) => { const videoConfig = useVideoConfig(); 
  5.  const frame = useCurrentFrame(); 
  6.  const text = titleText.split(‘ ‘).map((text) => ` ${text} `); 
  7.  return ( 
  8.   <h1 
  9.    style={{ 
  10.     fontFamily: ‘Helvetica, Arial’, 
  11.     fontWeight: ‘bold’, 
  12.     fontSize: 110, 
  13.     textAlign: ‘center’, 
  14.     position: ‘absolute’, 
  15.     bottom: 160, 
  16.     width: ‘100%’, 
  17.    }} 
  18.   > 
  19.   {text.map((text, i) => { 
  20.    return ( 
  21.     <span 
  22.      key={text} 
  23.      style={{ 
  24.       color: titleColor, 
  25.       marginLeft: 10, 
  26.       marginRight: 10, 
  27.       transform: `scale(${spring({ 
  28.        fps: videoConfig.fps, 
  29.        frame: frame — i * 5, 
  30.        config: { 
  31.         damping: 100, 
  32.         stiffness: 200, 
  33.         mass: 0.5, 
  34.        }, 
  35.       })})`, 
  36.       display: ‘inline-block’, 
  37.      }} 
  38.     > 
  39.     {text} 
  40.     </span> 
  41.    ); 
  42.   })} 
  43.  </h1> 
  44. ); 
  45. }; 

SubText.tsx file:

  1. import {interpolate, useCurrentFrame} from 'remotion';export const Title: React.FC<{ 
  2.  titleText: string; 
  3.  titleColor: string; 
  4. }> = ({titleText, titleColor}) => { 
  5.   
  6.  const frame = useCurrentFrame(); 
  7.  const opacity = interpolate(frame, [0, 30], [0, 1]);return ( 
  8.   <div 
  9.    style={{ 
  10.     fontFamily: 'Helvetica, Arial'
  11.     fontSize: 40, 
  12.     textAlign: 'center'
  13.     position: 'absolute'
  14.     bottom: 140, 
  15.     width: '100%'
  16.     opacity, 
  17.    }} 
  18.   > 
  19.    Follow me on{' '}<code> medium.com </code>{' 'for more articles 
  20.   </div> 
  21.  ); 
  22. }; 

步驟2

然后,我把這3個組件導(dǎo)入到MyVideo.tsx中,并用Sequence組件包裝,為每個組件分配相關(guān)的時間框架。除此之外,我還將幾個prop和動畫傳遞給子組件。

  1. import {interpolate, Sequence, useCurrentFrame, useVideoConfig} from ‘remotion’; 
  2. import {Logo} from ‘./components/Logo’; 
  3. import {SubText} from ‘./components/SubText’; 
  4. import {Title} from ‘./components/Title’;export const MyVideo: React.FC<{ 
  5. titleText: string; 
  6. titleColor: string; 
  7. }> = ({titleText, titleColor}) => {const frame = useCurrentFrame(); 
  8. const videoConfig = useVideoConfig(); 
  9. const opacity =  
  10.  interpolate( 
  11.   frame, 
  12.   [videoConfig.durationInFrames — 25,  
  13.    videoConfig.durationInFrames 
  14.    15 
  15.   ], 
  16.   [1, 0], 
  17.   {extrapolateLeft: ‘clamp’,extrapolateRight: ‘clamp’,} 
  18.  ); 
  19. const transitionStart = 0;return ( 
  20.  <div style={{flex: 1, backgroundColor: ‘white’}}> 
  21.  <div style={{opacity}}>  <Sequence  
  22.    from={0}  
  23.    durationInFrames={videoConfig.durationInFrames}> 
  24.     <Logo transitionStart={transitionStart} /> 
  25.   </Sequence>  <Sequence  
  26.    from={transitionStart + 35}  
  27.    durationInFrames={Infinity}> 
  28.     <Title titleText={titleText} titleColor={titleColor} /> 
  29.   </Sequence>  <Sequence  
  30.    from={transitionStart + 75}  
  31.    durationInFrames={Infinity}> 
  32.     <SubText /> 
  33.   </Sequence
  34.  </div> 
  35.  </div> 
  36. ); 
  37. }; 

步驟3

最后,我將上述所有文件導(dǎo)入Video.tsx,并使用Composition組件傳遞相關(guān)元數(shù)據(jù)。

  1. import {Composition} from ‘remotion’; 
  2. import {MyVideo} from ‘./MyVideo’; 
  3. import {Logo} from ‘./components/Logo’; 
  4. import {SubText} from ‘./components/SubText’; 
  5. export const RemotionVideo: React.FC = () => { 
  6.  return ( 
  7.   <> 
  8.    <Composition 
  9.     id=”HelloReaders” 
  10.     component={HelloReaders} 
  11.     durationInFrames={150} 
  12.     fps={30} 
  13.     width={1920} 
  14.     height={1080} 
  15.     defaultProps={{ 
  16.      titleText: ‘Welcome to My Blog’, 
  17.      titleColor: ‘black’, 
  18.     }} 
  19.    /> 
  20.    <Composition 
  21.     id=”Logo” 
  22.     component={Logo} 
  23.     durationInFrames={200} 
  24.     fps={30} 
  25.     width={1920} 
  26.     height={1080} 
  27.    /> 
  28.    <Composition 
  29.     id=”Title” 
  30.     component={SubText} 
  31.     durationInFrames={100} 
  32.     fps={30} 
  33.     width={1920} 
  34.     height={1080} 
  35.    /> 
  36.   </> 
  37.  ); 
  38. }; 

現(xiàn)在,你就可以運行你的第一個Remotion視頻了。你可以使用npm run start在開發(fā)模式下看到它,或者使用npm run build保存為mp4文件。

Finalized Video in Development Mode

結(jié)論

雖然Remotion還很年輕,但它已經(jīng)有了一些驚人的功能。它可能還達不到專業(yè)視頻編輯器的質(zhì)量。但我們肯定可以期待一些驚喜的到來。

此外,Remotion還有像參數(shù)化渲染、服務(wù)器端渲染和數(shù)據(jù)獲取這樣的功能,這些對于開發(fā)者來說是非常熟悉的。他們可以利用自己的經(jīng)驗,從這個工具中獲得最大的收益。

最重要的是,對于那些尋求創(chuàng)建個人使用的小視頻或動畫的方法的人來說,它是一個很好的選擇。

在我看來,我們可以利用Remotion來創(chuàng)建簡單的視頻和動畫,用我們所掌握的網(wǎng)絡(luò)開發(fā)知識。但在視頻編輯功能方面,很多東西需要改進和簡化。

不過,我強烈建議你下載Remotion,并給它一個機會。這將是一種全新的體驗。

謝謝您的閱讀??!!

 

責(zé)任編輯:武曉燕 來源: TianTianUp
相關(guān)推薦

2023-11-27 08:24:57

FormikReact

2022-12-15 08:49:58

ReactQR生成器

2022-11-23 08:17:18

CSS動畫貝塞爾

2022-03-03 10:40:25

VSaaS視頻監(jiān)控人工智能

2021-03-23 07:36:57

FlowIPFSNFT

2024-10-12 09:38:53

2023-06-02 22:36:02

鴻蒙彈簧動畫曲線

2016-11-01 21:02:47

javascriptreact.jsreact-route

2021-04-26 18:48:48

微應(yīng)用React

2021-04-08 18:39:57

JavaScriptExpress區(qū)塊鏈

2018-10-10 09:00:00

前端框架Angular

2024-06-04 14:17:26

2023-06-16 09:08:39

ReactContextRFC

2021-05-25 05:28:34

ReactJavaScript前端

2021-06-09 21:49:43

React 360VR虛擬

2024-02-20 01:53:01

ReactFlutter開發(fā)

2019-07-15 11:00:24

ReactNode前端

2020-11-02 11:33:52

ReactVue應(yīng)用

2022-07-18 09:01:58

React函數(shù)組件Hooks

2021-03-12 18:25:09

開發(fā)前端React
點贊
收藏

51CTO技術(shù)棧公眾號