Skip to content

🎥 Make videos programmatically with React

start working on waveform

Jonny Burger457425c118c1
diff --git a/packages/cli/src/editor/components/AudioWaveform.tsx b/packages/cli/src/editor/components/AudioWaveform.tsx
index 35ae06e376..efca6d57d8 100644
--- a/packages/cli/src/editor/components/AudioWaveform.tsx
+++ b/packages/cli/src/editor/components/AudioWaveform.tsx
@@ -57,7 +57,7 @@ export const AudioWaveform: React.FC<{
flexDirection: 'row',
alignItems: 'center',
position: 'absolute',
- height: 60,
+ height: 80,
}}
>
{normalized.map((w, i) => {
diff --git a/packages/cli/src/editor/components/TimelineElements.tsx b/packages/cli/src/editor/components/TimelineElements.tsx
index c01c1d1111..84fd8a4302 100644
--- a/packages/cli/src/editor/components/TimelineElements.tsx
+++ b/packages/cli/src/editor/components/TimelineElements.tsx
@@ -12,7 +12,7 @@ const Container = styled.div`
`;
export const TimelineElements: React.FC = () => {
- const {sequences, assets} = useContext(Internals.CompositionManager);
+ const {sequences} = useContext(Internals.CompositionManager);
const videoConfig = Internals.useUnsafeVideoConfig();
const timeline = useMemo((): Track[] => {
@@ -20,11 +20,10 @@ export const TimelineElements: React.FC = () => {
return [];
}
return calculateTimeline({
- assets,
sequences,
sequenceDuration: videoConfig.durationInFrames,
});
- }, [assets, sequences, videoConfig]);
+ }, [sequences, videoConfig]);
const inner: React.CSSProperties = useMemo(() => {
return {
@@ -41,7 +40,7 @@ export const TimelineElements: React.FC = () => {
{timeline.map((track) => (
<div key={track.trackId} style={inner}>
{track.sequences.map((s) => {
- return <TimelineSequence key={s.sequence.id} s={s} />;
+ return <TimelineSequence key={s.id} s={s} />;
})}
</div>
))}
diff --git a/packages/cli/src/editor/components/TimelineSequence.tsx b/packages/cli/src/editor/components/TimelineSequence.tsx
index 5d407b1ee6..4eff91e1fc 100644
--- a/packages/cli/src/editor/components/TimelineSequence.tsx
+++ b/packages/cli/src/editor/components/TimelineSequence.tsx
@@ -1,7 +1,6 @@
import React, {useMemo} from 'react';
-import {Internals} from 'remotion';
+import {Internals, TSequence} from 'remotion';
import styled from 'styled-components';
-import {SequenceWithOverlap} from '../helpers/calculate-timeline';
import {TIMELINE_PADDING} from '../helpers/timeline-layout';
import {useWindowSize} from '../hooks/use-window-size';
import {AudioWaveform} from './AudioWaveform';
@@ -20,14 +19,14 @@ const Pre = styled.pre`
export const TIMELINE_LAYER_HEIGHT = 80;
export const TimelineSequence: React.FC<{
- s: SequenceWithOverlap;
+ s: TSequence;
}> = ({s}) => {
const {width} = useWindowSize();
// If a duration is 1, it is essentially a still and it should have width 0
const spatialDuration = Internals.FEATURE_FLAG_V2_BREAKING_CHANGES
- ? s.sequence.duration - 1
- : s.sequence.duration;
+ ? s.duration - 1
+ : s.duration;
const video = Internals.useVideo();
if (!video) {
@@ -44,15 +43,15 @@ export const TimelineSequence: React.FC<{
position: 'absolute',
height: TIMELINE_LAYER_HEIGHT,
marginTop: 1,
- marginLeft: `calc(${(s.sequence.from / lastFrame) * 100}%)`,
+ marginLeft: `calc(${(s.from / lastFrame) * 100}%)`,
width:
- s.sequence.duration === Infinity
+ s.duration === Infinity
? width - TIMELINE_PADDING * 2
: (spatialDuration / lastFrame) * width - TIMELINE_PADDING * 2,
color: 'white',
overflow: 'hidden',
};
- }, [lastFrame, s.sequence.duration, s.sequence.from, spatialDuration, width]);
+ }, [lastFrame, s.duration, s.from, spatialDuration, width]);
const row: React.CSSProperties = useMemo(() => {
return {
@@ -68,7 +67,7 @@ export const TimelineSequence: React.FC<{
const thumbnailFit = Math.ceil(width / thumbnailWidth);
return (
- <div key={s.sequence.id} style={style} title={s.sequence.displayName}>
+ <div key={s.id} style={style} title={s.displayName}>
<div style={row}>
{Internals.FEATURE_FLAG_RICH_PREVIEWS
? new Array(thumbnailFit).fill(true).map((_, i) => {
@@ -87,10 +86,8 @@ export const TimelineSequence: React.FC<{
})
: null}
</div>
- {s.sequence.type === 'audio' ? (
- <AudioWaveform src={s.sequence.src} />
- ) : null}
- <Pre>{s.sequence.displayName}</Pre>
+ {s.type === 'audio' ? <AudioWaveform src={s.src} /> : null}
+ <Pre>{s.displayName}</Pre>
</div>
);
};
diff --git a/packages/cli/src/editor/helpers/calculate-timeline.ts b/packages/cli/src/editor/helpers/calculate-timeline.ts
index ac7959e06b..98d0cc4ce7 100644
--- a/packages/cli/src/editor/helpers/calculate-timeline.ts
+++ b/packages/cli/src/editor/helpers/calculate-timeline.ts
@@ -1,4 +1,4 @@
-import {TAsset, TSequence} from 'remotion';
+import {TSequence} from 'remotion';
export type SequenceWithOverlap = {
sequence: TSequence;
@@ -7,124 +7,45 @@ export type SequenceWithOverlap = {
export type Track = {
trackId: string;
- sequences: SequenceWithOverlap[];
-};
-
-export const calculateOverlays = (
- sequences: TSequence[]
-): SequenceWithOverlap[] => {
- return sequences.map((s) => {
- const overlaps = sequences
- .filter((otherS) => s.id !== otherS.id)
- .filter((otherS) => {
- const otherStart = otherS.from;
- const otherEnd = otherS.duration + otherStart;
- const thisStart = s.from;
- const thisEnd = s.duration + thisStart;
- if (otherStart < thisEnd && otherEnd > thisStart) {
- return true;
- }
- if (thisStart < otherEnd && thisEnd > otherStart) {
- return true;
- }
- return false;
- });
- return {
- sequence: s,
- overlaps,
- };
- });
-};
-
-export const numberOfOverlapsWithPrevious = (
- sequences: SequenceWithOverlap[],
- index: number
-) => {
- const sequencesBefore = sequences.slice(0, index);
- const sequence = sequences[index];
- return sequence.overlaps.filter((overlap) => {
- return sequencesBefore.find((sb) => sb.sequence.id === overlap.id);
- }).length;
-};
-
-const getAudioTracks = (
- assets: TAsset[],
- sequenceDuration: number
-): Track[] => {
- return assets.map(
- (a, i): Track => {
- const fileNameSegments = a.src.split('/');
- return {
- sequences: [
- {
- overlaps: [],
- sequence: {
- displayName: fileNameSegments[fileNameSegments.length - 1],
- // TODO: WRONG
- duration: sequenceDuration,
- from: 0,
- id: `audio-${i}`,
- parent: null,
- type: 'audio',
- src: a.src,
- },
- },
- ],
- trackId: `audio-${i}`,
- };
- }
- );
+ sequences: TSequence[];
};
export const calculateTimeline = ({
- assets,
sequences,
sequenceDuration,
}: {
- assets: TAsset[];
sequences: TSequence[];
sequenceDuration: number;
}): Track[] => {
- const sWithOverlays = calculateOverlays(sequences);
const tracks: Track[] = [];
- if (sWithOverlays.length === 0) {
+ if (sequences.length === 0) {
return [
{
sequences: [
{
- overlaps: [],
- sequence: {
- displayName: '',
- duration: sequenceDuration,
- from: 0,
- id: 'seq',
- parent: null,
- type: 'sequence',
- },
+ displayName: '',
+ duration: sequenceDuration,
+ from: 0,
+ id: 'seq',
+ parent: null,
+ type: 'sequence',
},
],
trackId: '0',
},
- ...getAudioTracks(assets, sequenceDuration),
];
}
- for (let i = 0; i < sWithOverlays.length; i++) {
- const sequence = sWithOverlays[i];
- // Not showing nested sequences
- if (sequence.sequence.parent) {
- continue;
- }
- const overlayCount = numberOfOverlapsWithPrevious(sWithOverlays, i);
- if (!tracks[overlayCount]) {
- tracks[overlayCount] = {
+ for (let i = 0; i < sequences.length; i++) {
+ const sequence = sequences[i];
+ if (!tracks[i]) {
+ tracks[i] = {
sequences: [],
- trackId: String(overlayCount),
+ trackId: String(i),
};
}
- tracks[overlayCount].sequences.push(sequence);
+ tracks[i].sequences.push(sequence);
}
- tracks.push(...getAudioTracks(assets, sequenceDuration));
return tracks;
};
diff --git a/packages/example/src/AudioTestingMute/index.tsx b/packages/example/src/AudioTestingMute/index.tsx
index 95cb011dfc..147c3e3a43 100644
--- a/packages/example/src/AudioTestingMute/index.tsx
+++ b/packages/example/src/AudioTestingMute/index.tsx
@@ -1,12 +1,18 @@
import React from 'react';
-import {Audio, useCurrentFrame, useVideoConfig, Video} from 'remotion';
+import {
+ Audio,
+ Sequence,
+ useCurrentFrame,
+ useVideoConfig,
+ Video,
+} from 'remotion';
// FIXME: commit sample movie + music
import movie from '../resources/framer-music.mp4';
import music from '../resources/sound1.mp3';
const AudioTestingMute: React.FC = () => {
const frame = useCurrentFrame();
- const {durationInFrames, fps} = useVideoConfig();
+ const {fps} = useVideoConfig();
/**
* Interleave music with the movie by muting each other
@@ -29,8 +35,9 @@ const AudioTestingMute: React.FC = () => {
return (
<div>
<Video src={movie} muted={getMuteState('movie')} />
-
- <Audio src={music} muted={getMuteState('music')} />
+ <Sequence from={20} durationInFrames={50}>
+ <Audio src={music} muted={getMuteState('music')} />
+ </Sequence>
</div>
);
};