成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何使用React創建視頻和動畫

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

本文轉載自微信公眾號「TianTianUp」,作者小弋。轉載本文請聯系TianTianUp公眾號。

大家好,我是小弋。

分享的內容是:

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

正文

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

它的官網:

https://www.remotion.dev/

簡介

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

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

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

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

如何設置Remotion?

創建一個新的Remotion項目是非常簡單的。但有兩個依賴項你應該先安裝。

步驟1:安裝NodeJS和FFMPEG

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

FFMPEG Downloads page.

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

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

第2步:啟動新項目

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

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

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

Default Remotion Project

Remotion的基礎知識

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

Video Properties

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

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

  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派生這些屬性,就像上面的例子一樣,使你的組件可以重復使用。

Compositions

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

  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組件,每個組件中都有元數據,包括視頻屬性。

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

Compositions List

Animation Properties

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

  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個內建的函數,名為interpolate和spring,你可以用它們來建立更高級的動畫。

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

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

  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個主要任務。它主要是用來給視頻中的元素分配不同的時間框架。在保持元素之間的聯系的同時,它也允許你重復使用同一個組件。

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

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

Timeline View of Remotion Player

  • from: 這定義了框架,該組件應該出現在視頻中。
  • durationInFrames: 以幀為單位的Sequence組件的長度。 

例如,下面的Sequence組件將在20幀后出現在視頻中,并將持續到結束,因為durationOnFrames是無限的。

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

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

創建一個簡單的視頻

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

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

步驟1

首先,我為我的視頻中的3個元素創建了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個組件導入到MyVideo.tsx中,并用Sequence組件包裝,為每個組件分配相關的時間框架。除此之外,我還將幾個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

最后,我將上述所有文件導入Video.tsx,并使用Composition組件傳遞相關元數據。

  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. }; 

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

Finalized Video in Development Mode

結論

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

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

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

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

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

謝謝您的閱讀!!!

 

責任編輯:武曉燕 來源: TianTianUp
相關推薦

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視頻監控人工智能

2021-03-23 07:36:57

FlowIPFSNFT

2023-06-02 22:36:02

鴻蒙彈簧動畫曲線

2024-10-12 09:38:53

2016-11-01 21:02:47

javascriptreact.jsreact-route

2021-04-26 18:48:48

微應用React

2021-04-08 18:39:57

JavaScriptExpress區塊鏈

2023-06-16 09:08:39

ReactContextRFC

2024-02-20 01:53:01

ReactFlutter開發

2019-07-15 11:00:24

ReactNode前端

2024-06-04 14:17:26

2018-10-10 09:00:00

前端框架Angular

2021-05-25 05:28:34

ReactJavaScript前端

2021-06-09 21:49:43

React 360VR虛擬

2020-11-02 11:33:52

ReactVue應用

2021-03-12 18:25:09

開發前端React

2022-07-18 09:01:58

React函數組件Hooks
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 狠狠色综合久久丁香婷婷 | 日韩一二三区视频 | 久久精品二区亚洲w码 | 欧美精品在线免费观看 | 日韩久久久久久 | 国产精品视频网 | 亚洲a视频| 亚洲一区二区三区在线播放 | 欧美 日韩 国产 一区 | 亚洲精品一区二区三区在线观看 | 自拍偷拍亚洲视频 | 91 在线 | 亚欧洲精品在线视频免费观看 | 国产区在线观看 | 久久精品欧美一区二区三区不卡 | 国产成人99 | 日韩成人在线一区 | 久久久中文| 一区二区三区四区视频 | 成人av片在线观看 | 精品国产乱码久久久久久牛牛 | 狠狠天天 | 超碰人人人人 | 亚洲欧美日韩中文字幕一区二区三区 | 欧美成人精品一区二区三区 | 久久精品99国产精品日本 | 精品国产一区一区二区三亚瑟 | 这里精品 | 亚洲v日韩v综合v精品v | 欧美久久久电影 | 日韩最新网址 | 成人在线视频观看 | 欧美视频一区二区三区 | 中文字幕日韩欧美一区二区三区 | 中文字幕视频网 | 五月婷婷中文 | 精品国产一区二区国模嫣然 | 国产午夜在线 | 欧美一二三区 | 美女福利视频一区 | 色吧久久 |