Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions public/pausio/capture.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 103 additions & 15 deletions public/pausio/capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"use strict";
const $ = id => document.getElementById(id);
const els = {
live: $("camera-video"), playback: $("result-video"), placeholder: $("camera-placeholder"),
live: $("camera-video"), canvas: $("portrait-canvas"), playback: $("result-video"),
placeholder: $("camera-placeholder"),
frame: $("camera-frame"), prompter: $("prompter"), scroll: $("prompter-scroll"),
text: $("prompter-text"), status: $("status"), state: $("capture-state"),
timer: $("timer"), clock: $("timer-text"), format: $("format-pill"),
camera: $("activate-camera"), start: $("record-start"), stop: $("record-stop"),
next: $("new-take"), flip: $("switch-camera"), pause: $("toggle-scroll"),
countdown: $("countdown"), panel: $("download-panel"), download: $("download-video"),
share: $("share-video"), details: $("file-details"), script: $("script-input"),
share: $("share-video"), fileDetails: $("file-details"), script: $("script-input"),
speed: $("speed"), font: $("font-size"), lines: $("gobbo-lines"),
position: $("prompter-position"), framing: $("framing-mode"),
details: $("gobbo-settings"), edit: $("edit-gobbo"),
Expand All @@ -21,6 +22,8 @@
let outputName = "", mode = "idle", facing = "user", raf = 0, lastFrame = 0;
let startedAt = 0, pausedScroll = false, countdownId = 0;
let savedAfterRecording = false, recordedMime = "";
let compositorFrame = 0, recordingStream = null, sourceSize = "";
const PORTRAIT_WIDTH = 720, PORTRAIT_HEIGHT = 1280, COMPOSITE_FPS = 24;

function notice(message, error = false) {
els.status.textContent = message;
Expand All @@ -40,6 +43,8 @@
els.placeholder.hidden = !["idle", "error", "starting"].includes(next);
els.live.hidden = ["finished", "idle", "error", "starting"].includes(next);
els.playback.hidden = next !== "finished";
els.canvas.hidden = !["preview", "countdown", "recording", "stopping"].includes(next)
|| !els.frame.classList.contains("composited");
els.prompter.hidden = next === "finished" || next === "idle" || next === "error";
els.timer.classList.toggle("live", next === "recording");
els.state.textContent = {idle:"PRONTO", error:"ERRORE", starting:"APERTURA…", preview:"CAMERA ATTIVA",
Expand Down Expand Up @@ -108,7 +113,72 @@
];
return candidates.find(type => MediaRecorder.isTypeSupported(type)) || "";
}
// Camera constraints are preferences, not an output-format contract. The
// phone can send 1920×1080 even with a portrait 9:16 request. Draw the same
// 720×1280 composition in the preview and the file we record.
function drawFitted(ctx, sourceWidth, sourceHeight, fit) {
const scale = (fit === "cover" ? Math.max : Math.min)(
PORTRAIT_WIDTH / sourceWidth, PORTRAIT_HEIGHT / sourceHeight
);
const width = sourceWidth * scale, height = sourceHeight * scale;
ctx.drawImage(els.live,
(PORTRAIT_WIDTH - width) / 2, (PORTRAIT_HEIGHT - height) / 2, width, height);
}
function drawPortraitFrame() {
const ctx = els.canvas.getContext("2d", {alpha:false});
if (!ctx) return;
ctx.fillStyle = "#08101a";
ctx.fillRect(0, 0, PORTRAIT_WIDTH, PORTRAIT_HEIGHT);
const width = els.live.videoWidth, height = els.live.videoHeight;
if (!width || !height || els.live.readyState < 2) return;
ctx.imageSmoothingEnabled = true;
if (els.framing.value === "wide") {
// Fill the vertical background, then preserve the complete camera field
// of view in the foreground. No digital zoom or fake camera rotation.
ctx.save();
ctx.filter = "blur(22px) brightness(0.48)";
drawFitted(ctx, width, height, "cover");
ctx.restore();
drawFitted(ctx, width, height, "contain");
} else {
// A physically narrower 9:16 crop is unavoidable when a 16:9 source
// must fully cover a portrait canvas.
drawFitted(ctx, width, height, "cover");
}
}
function compositorTick() {
if (!["preview", "countdown", "recording", "stopping"].includes(mode) || !stream) {
compositorFrame = 0;
return;
}
drawPortraitFrame();
compositorFrame = requestAnimationFrame(compositorTick);
}
function stopCompositor() {
if (compositorFrame) cancelAnimationFrame(compositorFrame);
compositorFrame = 0;
els.frame.classList.remove("composited");
els.canvas.hidden = true;
}
function startCompositor() {
if (typeof els.canvas.captureStream !== "function"
|| !els.canvas.getContext("2d", {alpha:false})) return false;
els.frame.classList.add("composited");
els.canvas.classList.toggle("mirrored", facing === "user");
els.canvas.hidden = false;
compositorFrame = requestAnimationFrame(compositorTick);
return true;
}
function stopRecordingStream() {
// The original audio track belongs to "stream": never stop it while
// MediaRecorder is still finalising its last dataavailable event.
if (recordingStream) {
for (const track of recordingStream.getVideoTracks()) track.stop();
recordingStream = null;
}
}
async function releaseCamera() {
stopCompositor();
if (stream) for (const track of stream.getTracks()) track.stop();
stream = null;
els.live.pause();
Expand Down Expand Up @@ -150,6 +220,9 @@
// Reveal the video BEFORE asking the browser to play it. Safari can leave
// play() pending indefinitely when its target is display:none.
setMode("preview");
if (!startCompositor()) {
throw new Error("Il browser non supporta la registrazione verticale su canvas");
}
try {
const playback = els.live.play();
if (playback && typeof playback.catch === "function") {
Expand All @@ -164,13 +237,11 @@
}
const dimensions = candidate.getVideoTracks()[0]?.getSettings?.() || {};
const width = dimensions.width, height = dimensions.height;
els.format.textContent = width && height ? width + "×" + height + " · originale" :
(social ? "9:16 richiesto" : "Campo ampio richiesto");
if (width && height && width >= height) {
notice("La fotocamera registra in orizzontale (" + width + "×" + height + "). Ruota il telefono in verticale e riattiva la fotocamera prima del take.", true);
} else {
notice("Controlla l'inquadratura, poi tocca Registra. Il gobbo sarà visibile solo sullo schermo.");
}
sourceSize = width && height ? width + "×" + height : "dimensioni automatiche";
els.format.textContent = PORTRAIT_WIDTH + "×" + PORTRAIT_HEIGHT + " · verticale";
const method = social ? "ritaglio a schermo pieno" : "campo completo con sfondo sfocato";
notice("Fotocamera " + sourceSize + " → video verticale " +
PORTRAIT_WIDTH + "×" + PORTRAIT_HEIGHT + " (" + method + "). Il gobbo non viene registrato.");
} catch (error) {
await releaseCamera();
setMode("error");
Expand Down Expand Up @@ -203,8 +274,19 @@
if (seq !== countdownId || !stream || mode !== "countdown") return;
els.countdown.hidden = true;
try {
if (typeof els.canvas.captureStream !== "function") {
throw new Error("Questo browser non supporta l'export video verticale");
}
drawPortraitFrame();
const composedVideo = els.canvas.captureStream(COMPOSITE_FPS);
recordingStream = new MediaStream([
...composedVideo.getVideoTracks(), ...stream.getAudioTracks()
]);
if (!recordingStream.getVideoTracks().length) {
throw new Error("Non è possibile acquisire il video verticale dal canvas");
}
const mimeType = supportedMime();
recorder = new MediaRecorder(stream, mimeType ? {mimeType} : undefined);
recorder = new MediaRecorder(recordingStream, mimeType ? {mimeType} : undefined);
recordedMime = recorder.mimeType || mimeType || "video/webm";
chunks = [];
recorder.ondataavailable = event => { if (event.data && event.data.size) chunks.push(event.data); };
Expand All @@ -217,8 +299,10 @@
raf = requestAnimationFrame(tick);
} catch (error) {
recorder = null;
stopRecordingStream();
setMode("preview");
notice("Impossibile avviare la registrazione su questo browser (" + (error?.name || "formato non supportato") + ").", true);
notice("Impossibile avviare la registrazione verticale: " +
(error?.message || error?.name || "formato non supportato") + ".", true);
}
}
function stopRecording() {
Expand All @@ -236,6 +320,7 @@
async function finishRecording() {
if (mode !== "stopping" && mode !== "recording") return;
stopAnimation();
stopRecordingStream();
await releaseCamera();
const type = recordedMime.includes("mp4") ? "video/mp4" : "video/webm";
output = new Blob(chunks, {type});
Expand All @@ -255,8 +340,9 @@
els.download.href = blobUrl;
els.download.download = outputName;
const seconds = Math.round((performance.now() - startedAt) / 1000);
els.details.textContent = outputName.split(".").pop().toUpperCase() + " · " +
(output.size / (1024 * 1024)).toFixed(1) + " MB · " + seconds + " s · senza gobbo";
els.fileDetails.textContent = outputName.split(".").pop().toUpperCase() + " · " +
(output.size / (1024 * 1024)).toFixed(1) + " MB · " + seconds +
" s · " + PORTRAIT_WIDTH + "×" + PORTRAIT_HEIGHT + " verticale · senza gobbo";
savedAfterRecording = false;
const file = new File([output], outputName, {type});
els.share.hidden = !(navigator.canShare && navigator.canShare({files:[file]}));
Expand Down Expand Up @@ -295,8 +381,10 @@
els.framing.addEventListener("change", async () => {
remember();
if (mode === "preview") {
notice("Aggiorno il campo visivo della fotocamera…");
await activateCamera();
drawPortraitFrame();
const composition = els.framing.value === "social" ?
"video verticale con ritaglio" : "video verticale con campo completo";
notice("Anteprima aggiornata: " + composition + ". Il file registrato userà la stessa inquadratura.");
}
});
els.live.addEventListener("click", () => {
Expand Down
17 changes: 9 additions & 8 deletions public/pausio/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<meta name="color-scheme" content="dark">
<meta name="description" content="Registra video social con gobbo, direttamente sul telefono. Scarica il video senza upload.">
<title>Registra · Pausio</title>
<link rel="stylesheet" href="./capture.css?v=20261111-layout">
<script src="./capture.js?v=20261111-layout" defer></script>
<link rel="stylesheet" href="./capture.css?v=20261111-portrait">
<script src="./capture.js?v=20261111-portrait" defer></script>
</head>
<body>
<div class="shell">
Expand All @@ -23,7 +23,8 @@
<span id="capture-state" class="state-chip">PRONTO</span>
</div>
<div class="camera" id="camera-frame">
<video id="camera-video" autoplay playsinline muted aria-label="Anteprima fotocamera"></video>
<video id="camera-video" autoplay playsinline muted aria-label="Sorgente originale fotocamera"></video>
<canvas id="portrait-canvas" width="720" height="1280" hidden aria-label="Anteprima verticale della registrazione"></canvas>
<video id="result-video" playsinline controls hidden aria-label="Riproduzione del video registrato"></video>
<div id="camera-placeholder" class="camera-placeholder">
<span class="camera-glyph" aria-hidden="true">◎</span>
Expand All @@ -36,7 +37,7 @@
</div>
<div class="camera-hud">
<span id="timer" class="timer"><span id="record-dot" class="record-dot"></span><span id="timer-text">00:00</span></span>
<span id="format-pill" class="format-pill">Campo ampio · formato originale</span>
<span id="format-pill" class="format-pill">720×1280 · verticale</span>
</div>
<div id="countdown" class="countdown" aria-live="assertive" hidden>3</div>
</div>
Expand All @@ -52,7 +53,7 @@
<section id="download-panel" class="download-panel" hidden aria-labelledby="download-title">
<p class="eyebrow">TAKE PRONTO</p>
<h2 id="download-title">Video registrato, pronto da salvare.</h2>
<p id="file-details" class="small">Il gobbo non compare nel file.</p>
<p id="file-details" class="small">Il video salvato è verticale 720×1280 e il gobbo non compare nel file.</p>
<div class="download-actions">
<a id="download-video" class="button primary" href="#" download>↓ Scarica video</a>
<button id="share-video" type="button" class="button secondary" hidden>Condividi / Salva su File</button>
Expand All @@ -74,10 +75,10 @@ <h2 id="download-title">Video registrato, pronto da salvare.</h2>
</label>
<label class="field" for="framing-mode"><span>Inquadratura della fotocamera</span>
<select id="framing-mode">
<option value="wide">Campo ampio · 3:4, meno ritaglio</option>
<option value="social">Social · 9:16, più ravvicinato</option>
<option value="wide">Verticale 9:16 · campo completo, sfondo sfocato</option>
<option value="social">Verticale 9:16 · schermo pieno, ritaglio</option>
</select>
<small>Il campo ampio evita un ritaglio 9:16 forzato. Il file registra il formato reale della fotocamera; il video social 9:16 si può preparare in Pausio sul PC.</small>
<small>Il browser può fornire un video orizzontale anche con il telefono verticale. Pausio compone un file 9:16: «campo completo» mantiene l'intera inquadratura con sfondo sfocato, «schermo pieno» riempie il video ritagliando i lati.</small>
</label>
<div class="slider-line"><label for="speed">Velocità gobbo</label><strong id="speed-value">120 parole/min</strong></div>
<input id="speed" type="range" min="50" max="220" step="5" value="120">
Expand Down
Loading