generate and upload thumbnail

This commit is contained in:
MarconLP 2023-04-22 03:06:26 +02:00
parent ece4700fce
commit 822d9ba225
No known key found for this signature in database
GPG key ID: A08A9C8B623F5EA5
2 changed files with 47 additions and 13 deletions

View file

@ -44,6 +44,7 @@ export default function Recorder({ closeModal, step, setStep }: Props) {
const getSignedUrl = api.video.getUploadUrl.useMutation(); const getSignedUrl = api.video.getUploadUrl.useMutation();
const [duration, setDuration] = useState<number>(0); const [duration, setDuration] = useState<number>(0);
const [, setPaywallOpen] = useAtom(paywallAtom); const [, setPaywallOpen] = useAtom(paywallAtom);
const videoRef = useRef<null | HTMLVideoElement>(null);
const handleRecording = async () => { const handleRecording = async () => {
const screenStream = await navigator.mediaDevices.getDisplayMedia({ const screenStream = await navigator.mediaDevices.getDisplayMedia({
@ -142,20 +143,42 @@ export default function Recorder({ closeModal, step, setStep }: Props) {
} }
}; };
function generateThumbnail(video: HTMLVideoElement) {
const canvas = document.createElement("canvas");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas
.getContext("2d")
?.drawImage(video, 0, 0, canvas.width, canvas.height);
return canvas.toDataURL();
}
const handleUpload = async () => { const handleUpload = async () => {
if (!blob) return; if (!blob || !videoRef.current) return;
const dateString = "Recording - " + dayjs().format("D MMM YYYY") + ".webm"; const dateString = "Recording - " + dayjs().format("D MMM YYYY") + ".webm";
setSubmitting(true); setSubmitting(true);
try { try {
const { signedUrl, id } = await getSignedUrl.mutateAsync({ const { signedVideoUrl, signedThumbnailUrl, id } =
key: dateString, await getSignedUrl.mutateAsync({
}); key: dateString,
});
await axios await axios
.put(signedUrl, blob.slice(), { .put(signedVideoUrl, blob.slice(), {
headers: { "Content-Type": "video/webm" }, headers: { "Content-Type": "video/webm" },
}) })
.then(() => {
if (!videoRef.current) return;
return axios.put(
signedThumbnailUrl,
generateThumbnail(videoRef.current),
{
headers: { "Content-Type": "image/png" },
}
);
})
.then(() => { .then(() => {
void router.push("share/" + id); void router.push("share/" + id);
setRecordOpen(false); setRecordOpen(false);
@ -293,13 +316,14 @@ export default function Recorder({ closeModal, step, setStep }: Props) {
) : null} ) : null}
{step === "post" ? ( {step === "post" ? (
<div> <div>
{blob && ( {blob ? (
<video <video
src={URL.createObjectURL(blob)} src={URL.createObjectURL(blob)}
controls controls
ref={videoRef}
className="mb-4 w-[75vw]" className="mb-4 w-[75vw]"
/> />
)} ) : null}
<div className="flex items-center justify-center"> <div className="flex items-center justify-center">
<button <button
type="button" type="button"

View file

@ -83,17 +83,27 @@ export const videoRouter = createTRPCRouter({
}, },
}); });
const putObjectCommand = new PutObjectCommand({ const signedVideoUrl = await getSignedUrl(
Bucket: env.AWS_BUCKET_NAME, s3,
Key: ctx.session.user.id + "/" + video.id, new PutObjectCommand({
}); Bucket: env.AWS_BUCKET_NAME,
Key: ctx.session.user.id + "/" + video.id,
})
);
const signedUrl = await getSignedUrl(s3, putObjectCommand); const signedThumbnailUrl = await getSignedUrl(
s3,
new PutObjectCommand({
Bucket: env.AWS_BUCKET_NAME,
Key: video.userId + "/" + video.id + "thumbnail",
})
);
return { return {
success: true, success: true,
id: video.id, id: video.id,
signedUrl, signedVideoUrl,
signedThumbnailUrl,
}; };
}), }),
setSharing: protectedProcedure setSharing: protectedProcedure