-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ts-sdk] Add Blob
as MP4 input
#1007
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import Smelter from '@swmansion/smelter-web-wasm'; | ||
import { InputStream, Rescaler, useInputStreams, View, Text } from '@swmansion/smelter'; | ||
import CompositorVideo from '../components/SmelterVideo'; | ||
import NotoSansFont from '../../assets/NotoSans.ttf'; | ||
|
||
function UploadMp4Example() { | ||
const smelter = useSmelter(); | ||
|
||
const onCreate = useCallback(async (smelter: Smelter) => { | ||
await smelter.registerFont(NotoSansFont); | ||
}, []); | ||
const onUpload = async (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (!e.target.files) { | ||
console.error('No files were uploaded'); | ||
return; | ||
} | ||
|
||
let file = e.target.files[0]; | ||
|
||
if (!smelter) { | ||
console.error('Smelter has not been initialized yet'); | ||
return; | ||
} | ||
|
||
await smelter.unregisterInput('file'); | ||
await smelter.registerInput('file', { type: 'mp4', blob: file }); | ||
}; | ||
|
||
if (!smelter) { | ||
return <div className="card" />; | ||
} | ||
|
||
return ( | ||
<div className="card"> | ||
<div style={{ margin: 20 }}> | ||
<label htmlFor="upload-input" style={{ padding: 10 }}> | ||
Upload MP4 | ||
</label> | ||
<input id="upload-input" type="file" onChange={onUpload} accept="video/mp4" /> | ||
</div> | ||
<CompositorVideo | ||
outputId="output" | ||
width={1280} | ||
height={720} | ||
smelter={smelter} | ||
onVideoCreated={onCreate}> | ||
<Scene /> | ||
</CompositorVideo> | ||
</div> | ||
); | ||
} | ||
|
||
function Scene() { | ||
const inputs = useInputStreams(); | ||
if (!inputs['file']) { | ||
return ( | ||
<View style={{ backgroundColor: 'black' }}> | ||
<View style={{ top: 340, left: 560 }}> | ||
<Text style={{ fontSize: 24, color: 'white' }}>Upload an MP4 file</Text> | ||
</View> | ||
</View> | ||
); | ||
} | ||
return ( | ||
<View style={{ backgroundColor: 'black' }}> | ||
<Rescaler> | ||
<InputStream inputId="file" /> | ||
</Rescaler> | ||
</View> | ||
); | ||
} | ||
|
||
function useSmelter(): Smelter | undefined { | ||
const [smelter, setSmelter] = useState<Smelter>(); | ||
useEffect(() => { | ||
const smelter = new Smelter(); | ||
|
||
let cancel = false; | ||
const promise = (async () => { | ||
await smelter.init(); | ||
await smelter.start(); | ||
if (!cancel) { | ||
setSmelter(smelter); | ||
} | ||
})(); | ||
|
||
return () => { | ||
cancel = true; | ||
void (async () => { | ||
await promise.catch(() => {}); | ||
await smelter.terminate(); | ||
})(); | ||
}; | ||
}, []); | ||
return smelter; | ||
} | ||
|
||
export default UploadMp4Example; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import { _smelterInternals } from '@swmansion/smelter'; | |
*/ | ||
export type RegisterInputRequest = | ||
| Api.RegisterInput | ||
| { type: 'mp4_blob'; blob: any } | ||
Comment on lines
15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like adding new type just for that, it might make sense if it was more generic to handle some other cases types in the future, but not in this form |
||
| { type: 'camera' } | ||
| { type: 'screen_capture' } | ||
| { type: 'stream'; stream: any }; | ||
|
@@ -52,6 +53,13 @@ export function intoRegisterInput(input: RegisterInput): RegisterInputRequest { | |
} | ||
|
||
function intoMp4RegisterInput(input: Inputs.RegisterMp4Input): RegisterInputRequest { | ||
if (input.blob) { | ||
return { | ||
type: 'mp4_blob', | ||
blob: input.blob, | ||
}; | ||
} | ||
|
||
return { | ||
type: 'mp4', | ||
url: input.url, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { Input as CoreInput } from '@swmansion/smelter-core'; | ||
import type { WorkerMessage } from '../workerApi'; | ||
import { assert } from '../utils'; | ||
import { assert, downloadToArrayBuffer } from '../utils'; | ||
import { handleRegisterCameraInput } from './input/camera'; | ||
import { handleRegisterScreenCaptureInput } from './input/screenCapture'; | ||
import { handleRegisterStreamInput } from './input/stream'; | ||
|
@@ -23,7 +23,11 @@ export async function handleRegisterInputRequest( | |
): Promise<RegisterInputResult> { | ||
if (body.type === 'mp4') { | ||
assert(body.url, 'mp4 URL is required'); | ||
return handleRegisterMp4Input(ctx, inputId, body.url); | ||
const arrayBuffer = await downloadToArrayBuffer(body.url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the whole point of having functions like handleRegisterMp4Input is to separate mp4-specific code from common code. Preparing an array buffer here only unnecessarily complicates this function which should be only a simple if-else tree to call appropriate handlers. Additionally, it spreads potential functions that can throw an error all over the place. |
||
return await handleRegisterMp4Input(ctx, inputId, arrayBuffer); | ||
} else if (body.type === 'mp4_blob') { | ||
const arrayBuffer = await (body.blob as Blob).arrayBuffer(); | ||
return await handleRegisterMp4Input(ctx, inputId, arrayBuffer); | ||
} else if (body.type === 'camera') { | ||
return await handleRegisterCameraInput(ctx, inputId); | ||
} else if (body.type === 'screen_capture') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,8 @@ export async function sleep(timeoutMs: number): Promise<void> { | |
export function framerateToDurationMs(framerate: Framerate): number { | ||
return (1000 * framerate.den) / framerate.num; | ||
} | ||
|
||
export async function downloadToArrayBuffer(url: string): Promise<ArrayBuffer> { | ||
const response = await fetch(url); | ||
return await response.arrayBuffer(); | ||
} | ||
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in my opinion this util is not helpful |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was not used by any example, but
MultipleOutputs
had pretty much the same implementation so I put everything here