--- name: gif description: RemotionでGIF、APNG、AVIF、WebPを表示する metadata: tags: gif, animation, images, animated, apng, avif, webp --- # Remotionでアニメーション画像を使用する ## 基本的な使い方 `` を使用して、RemotionのタイムラインにGIF、APNG、AVIF、WebP画像を同期させて表示します: ```tsx import {AnimatedImage, staticFile} from 'remotion'; export const MyComposition = () => { return ; }; ``` リモートURLもサポートされています(CORSが有効になっている必要があります): ```tsx ``` ## サイズとフィット `fit` プロパティでコンテナへの画像の収め方を制御します: ```tsx // 引き伸ばしてフィル(デフォルト) // アスペクト比を維持してコンテナ内に収める // コンテナを埋め、必要に応じてクロップ ``` ## 再生速度 `playbackRate` でアニメーション速度を制御します: ```tsx {/* 2倍速 */} {/* 半速 */} ``` ## ループの動作 アニメーション終了後の動作を制御します: ```tsx // 無限ループ(デフォルト) // 一度再生して最終フレームを表示 // 一度再生してキャンバスをクリア ``` ## スタイリング 追加のCSSには `style` プロパティを使用します(サイズは `width` と `height` プロパティで指定): ```tsx ``` ## GIFの長さを取得する `@remotion/gif` の `getGifDurationInSeconds()` を使用してGIFの長さを取得します。 ```bash npx remotion add @remotion/gif # npmを使うプロジェクト bunx remotion add @remotion/gif # bunを使うプロジェクト yarn remotion add @remotion/gif # yarnを使うプロジェクト pnpm exec remotion add @remotion/gif # pnpmを使うプロジェクト ``` ```tsx import {getGifDurationInSeconds} from '@remotion/gif'; import {staticFile} from 'remotion'; const duration = await getGifDurationInSeconds(staticFile('animation.gif')); console.log(duration); // 例: 2.5 ``` GIFに合わせてコンポジションの長さを設定するのに便利です: ```tsx import {getGifDurationInSeconds} from '@remotion/gif'; import {staticFile, CalculateMetadataFunction} from 'remotion'; const calculateMetadata: CalculateMetadataFunction = async () => { const duration = await getGifDurationInSeconds(staticFile('animation.gif')); return { durationInFrames: Math.ceil(duration * 30), }; }; ``` ## 代替手段 `` が動作しない場合(ChromeとFirefoxのみサポート)、代わりに `@remotion/gif` の `` を使用できます。 ```bash npx remotion add @remotion/gif # npmを使うプロジェクト bunx remotion add @remotion/gif # bunを使うプロジェクト yarn remotion add @remotion/gif # yarnを使うプロジェクト pnpm exec remotion add @remotion/gif # pnpmを使うプロジェクト ``` ```tsx import {Gif} from '@remotion/gif'; import {staticFile} from 'remotion'; export const MyComposition = () => { return ; }; ``` `` コンポーネントは `` と同じプロパティを持ちますが、GIFファイルのみをサポートします。