🎥 Make videos programmatically with React
timeline layer height
Jonny Burger3b23c5ac747b
diff --git a/packages/studio/src/components/AudioWaveform.tsx b/packages/studio/src/components/AudioWaveform.tsxindex 4090b39d1c..980353dc47 100644--- a/packages/studio/src/components/AudioWaveform.tsx+++ b/packages/studio/src/components/AudioWaveform.tsx@@ -4,8 +4,8 @@ import React, {useEffect, useMemo, useRef, useState} from 'react';import {Internals} from 'remotion';import {LIGHT_TRANSPARENT} from '../helpers/colors';import {+ getTimelineLayerHeight,TIMELINE_BORDER,- TIMELINE_LAYER_HEIGHT,} from '../helpers/timeline-layout';import {AudioWaveformBar,@@ -18,7 +18,7 @@ const container: React.CSSProperties = {flexDirection: 'row',alignItems: 'flex-end',position: 'absolute',- height: TIMELINE_LAYER_HEIGHT,+ height: getTimelineLayerHeight('other'),};const errorMessage: React.CSSProperties = {fontSize: 13,@@ -80,7 +80,12 @@ export const AudioWaveform: React.FC<{return;}- context.clearRect(0, 0, visualizationWidth, TIMELINE_LAYER_HEIGHT);+ context.clearRect(+ 0,+ 0,+ visualizationWidth,+ getTimelineLayerHeight('other'),+ );if (!doesVolumeChange || typeof volume === 'number') {// The volume is a number, meaning it could change on each frame-// User did not use the (f: number) => number syntax, so we can't draw@@ -90,10 +95,11 @@ export const AudioWaveform: React.FC<{const volumes = volume.split(',').map((v) => Number(v));context.beginPath();- context.moveTo(0, TIMELINE_LAYER_HEIGHT);+ context.moveTo(0, getTimelineLayerHeight('other'));volumes.forEach((v, index) => {const x = (index / (volumes.length - 1)) * visualizationWidth;- const y = (1 - v) * (TIMELINE_LAYER_HEIGHT - TIMELINE_BORDER * 2) + 1;+ const y =+ (1 - v) * (getTimelineLayerHeight('other') - TIMELINE_BORDER * 2) + 1;if (index === 0) {context.moveTo(x, y);} else {@@ -176,7 +182,7 @@ export const AudioWaveform: React.FC<{ref={canvas}style={canvasStyle}width={visualizationWidth}- height={TIMELINE_LAYER_HEIGHT}+ height={getTimelineLayerHeight('other')}/></div>);diff --git a/packages/studio/src/components/AudioWaveformBar.tsx b/packages/studio/src/components/AudioWaveformBar.tsxindex 3785b12426..ba6b083813 100644--- a/packages/studio/src/components/AudioWaveformBar.tsx+++ b/packages/studio/src/components/AudioWaveformBar.tsx@@ -1,5 +1,5 @@import React, {useMemo} from 'react';-import {TIMELINE_LAYER_HEIGHT} from '../helpers/timeline-layout';+import {getTimelineLayerHeight} from '../helpers/timeline-layout';export const WAVEFORM_BAR_LENGTH = 2;export const WAVEFORM_BAR_MARGIN = 1;@@ -27,7 +27,7 @@ export const AudioWaveformBar: React.FC<{const style = useMemo(() => {return {...container,- height: TIMELINE_LAYER_HEIGHT * amplitude * (1 / 0.6366),+ height: getTimelineLayerHeight('other') * amplitude * (1 / 0.6366),};}, [amplitude]);return <div style={style} />;diff --git a/packages/studio/src/components/Timeline/Timeline.tsx b/packages/studio/src/components/Timeline/Timeline.tsxindex 193f289e16..f8c4497078 100644--- a/packages/studio/src/components/Timeline/Timeline.tsx+++ b/packages/studio/src/components/Timeline/Timeline.tsx@@ -4,8 +4,8 @@ import {calculateTimeline} from '../../helpers/calculate-timeline';import {BACKGROUND} from '../../helpers/colors';import type {TrackWithHash} from '../../helpers/get-timeline-sequence-sort-key';import {+ getTimelineLayerHeight,TIMELINE_ITEM_BORDER_BOTTOM,- TIMELINE_LAYER_HEIGHT,} from '../../helpers/timeline-layout';import {VERTICAL_SCROLLBAR_CLASSNAME} from '../Menu/is-menu-item';import {SplitterContainer} from '../Splitter/SplitterContainer';@@ -75,8 +75,15 @@ export const Timeline: React.FC = () => {const inner: React.CSSProperties = useMemo(() => {return {height:- shown.length *- (TIMELINE_LAYER_HEIGHT + Number(TIMELINE_ITEM_BORDER_BOTTOM)) ++ shown.reduce((acc, track) => {+ return (+ acc ++ getTimelineLayerHeight(+ track.sequence.type === 'video' ? 'video' : 'other',+ ) ++ Number(TIMELINE_ITEM_BORDER_BOTTOM)+ );+ }, 0) +TIMELINE_ITEM_BORDER_BOTTOM +(hasBeenCut ? MAX_TIMELINE_TRACKS_NOTICE_HEIGHT : 0) +TIMELINE_TIME_INDICATOR_HEIGHT,@@ -85,7 +92,7 @@ export const Timeline: React.FC = () => {minHeight: '100%',overflowX: 'hidden',};- }, [hasBeenCut, shown.length]);+ }, [hasBeenCut, shown]);return (<divdiff --git a/packages/studio/src/components/Timeline/TimelineListItem.tsx b/packages/studio/src/components/Timeline/TimelineListItem.tsxindex 2b3316c73f..5de31e355a 100644--- a/packages/studio/src/components/Timeline/TimelineListItem.tsx+++ b/packages/studio/src/components/Timeline/TimelineListItem.tsx@@ -3,27 +3,14 @@ import type {TSequence} from 'remotion';import {Internals} from 'remotion';import {TIMELINE_TRACK_SEPARATOR} from '../../helpers/colors';import {+ getTimelineLayerHeight,TIMELINE_ITEM_BORDER_BOTTOM,- TIMELINE_LAYER_HEIGHT,} from '../../helpers/timeline-layout';import {TimelineLayerEye} from './TimelineLayerEye';import {TimelineStack} from './TimelineStack';const SPACING = 5;-const outer: React.CSSProperties = {- height: TIMELINE_LAYER_HEIGHT + TIMELINE_ITEM_BORDER_BOTTOM,- color: 'white',- fontFamily: 'Arial, Helvetica, sans-serif',- display: 'flex',- flexDirection: 'row',- alignItems: 'center',- wordBreak: 'break-all',- textAlign: 'left',- paddingLeft: SPACING,- borderBottom: `1px solid ${TIMELINE_TRACK_SEPARATOR}`,-};-const space: React.CSSProperties = {width: SPACING,flexShrink: 0,@@ -61,6 +48,23 @@ export const TimelineListItem: React.FC<{[sequence.id, setHidden],);+ const outer: React.CSSProperties = useMemo(() => {+ return {+ height:+ getTimelineLayerHeight(sequence.type === 'video' ? 'video' : 'other') ++ TIMELINE_ITEM_BORDER_BOTTOM,+ color: 'white',+ fontFamily: 'Arial, Helvetica, sans-serif',+ display: 'flex',+ flexDirection: 'row',+ alignItems: 'center',+ wordBreak: 'break-all',+ textAlign: 'left',+ paddingLeft: SPACING,+ borderBottom: `1px solid ${TIMELINE_TRACK_SEPARATOR}`,+ };+ }, [sequence.type]);+return (<div style={outer}><TimelineLayerEyediff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsxindex c03303af1b..a8fe309450 100644--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx@@ -6,7 +6,7 @@ import {SEQUENCE_BORDER_WIDTH,getTimelineSequenceLayout,} from '../../helpers/get-timeline-sequence-layout';-import {TIMELINE_LAYER_HEIGHT} from '../../helpers/timeline-layout';+import {getTimelineLayerHeight} from '../../helpers/timeline-layout';import {useMaxMediaDuration} from '../../helpers/use-max-media-duration';import {AudioWaveform} from '../AudioWaveform';import {LoopedTimelineIndicator} from './LoopedTimelineIndicators';@@ -82,7 +82,7 @@ const Inner: React.FC<{border: SEQUENCE_BORDER_WIDTH + 'px solid rgba(255, 255, 255, 0.2)',borderRadius: 2,position: 'absolute',- height: TIMELINE_LAYER_HEIGHT,+ height: getTimelineLayerHeight(s.type === 'video' ? 'video' : 'other'),marginLeft,width,color: 'white',diff --git a/packages/studio/src/components/Timeline/TimelineTracks.tsx b/packages/studio/src/components/Timeline/TimelineTracks.tsxindex efd287e4ac..ed7cd26336 100644--- a/packages/studio/src/components/Timeline/TimelineTracks.tsx+++ b/packages/studio/src/components/Timeline/TimelineTracks.tsx@@ -1,8 +1,8 @@import React, {useMemo} from 'react';import type {TrackWithHash} from '../../helpers/get-timeline-sequence-sort-key';import {+ getTimelineLayerHeight,TIMELINE_ITEM_BORDER_BOTTOM,- TIMELINE_LAYER_HEIGHT,TIMELINE_PADDING,} from '../../helpers/timeline-layout';import {MaxTimelineTracksReached} from './MaxTimelineTracks';@@ -24,13 +24,6 @@ export const TimelineTracks: React.FC<{readonly timeline: TrackWithHash[];readonly hasBeenCut: boolean;}> = ({timeline, hasBeenCut}) => {- const inner: React.CSSProperties = useMemo(() => {- return {- height: TIMELINE_LAYER_HEIGHT,- marginBottom: TIMELINE_ITEM_BORDER_BOTTOM,- };- }, []);-const timelineStyle: React.CSSProperties = useMemo(() => {return {...timelineContent,@@ -48,7 +41,15 @@ export const TimelineTracks: React.FC<{}return (- <div key={track.sequence.id} style={inner}>+ <div+ key={track.sequence.id}+ style={{+ height: getTimelineLayerHeight(+ track.sequence.type === 'video' ? 'video' : 'other',+ ),+ marginBottom: TIMELINE_ITEM_BORDER_BOTTOM,+ }}+ ><TimelineSequence s={track.sequence} /></div>);diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsxindex 25d4d2c368..7fd9c3a731 100644--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx@@ -11,9 +11,9 @@ import {getTimestampFromFrameDatabaseKey,makeFrameDatabaseKey,} from '../../helpers/frame-database';-import {TIMELINE_LAYER_HEIGHT} from '../../helpers/timeline-layout';+import {getTimelineLayerHeight} from '../../helpers/timeline-layout';-const HEIGHT = TIMELINE_LAYER_HEIGHT - 2;+const HEIGHT = getTimelineLayerHeight('video') - 2;const containerStyle: React.CSSProperties = {height: HEIGHT,@@ -136,9 +136,17 @@ const drawSlot = ({const relativeTimestamp = timestamp - fromSeconds * WEBCODECS_TIMESCALE;const frameIndex = relativeTimestamp / durationOfOneFrame;- const left = Math.round(frameIndex * frame.displayWidth); // round to avoid antialiasing-- ctx.drawImage(frame, left, 0);+ const left = Math.floor(+ (frameIndex * frame.displayWidth) / window.devicePixelRatio,+ ); // round to avoid antialiasing++ ctx.drawImage(+ frame,+ left,+ 0,+ frame.displayWidth / window.devicePixelRatio,+ frame.displayHeight / window.devicePixelRatio,+ );filledSlots.set(timestamp, frame.timestamp);};@@ -342,7 +350,7 @@ export const TimelineVideoInfo: React.FC<{},src,onFrame: (frame) => {- const scale = HEIGHT / frame.displayHeight;+ const scale = (HEIGHT / frame.displayHeight) * window.devicePixelRatio;const transformed = WebCodecsInternals.rotateAndResizeVideoFrame({frame,diff --git a/packages/studio/src/helpers/timeline-layout.ts b/packages/studio/src/helpers/timeline-layout.tsindex 7fb23cb866..93472e90f8 100644--- a/packages/studio/src/helpers/timeline-layout.ts+++ b/packages/studio/src/helpers/timeline-layout.ts@@ -1,4 +1,11 @@export const TIMELINE_PADDING = 16;-export const TIMELINE_LAYER_HEIGHT = 25;export const TIMELINE_BORDER = 1;export const TIMELINE_ITEM_BORDER_BOTTOM = 1;++export const getTimelineLayerHeight = (type: 'video' | 'other') => {+ if (type === 'video') {+ return 50;+ }++ return 25;+};