feat(media): media put --web — films browsers refuse (HEVC, AC-3, DTS) converted before sealing

ffprobe decides per stream: H.264/VP8/VP9/AV1 and AAC/MP3/Opus are
copied, anything else becomes H.264 (CRF 20) / AAC with every audio track
and its language kept; embedded text subtitles come out of the same
ffmpeg pass as subtitle tracks; picture subtitles are reported. Output is
an MP4 with the index first. Without --web, put warns when ffprobe sees
streams browsers will not play.

Verified: an HEVC + AC-3 5.1 MKV with embedded Norwegian/English SubRip:
Chromium refuses the original (DEMUXER_ERROR_NO_SUPPORTED_STREAMS); the
--web upload plays with audio decoded and the Norwegian track showing.
78 TS tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 22:05:28 +02:00
parent 422a37e830
commit d784e47ab3
8 changed files with 286 additions and 12 deletions

View File

@@ -14,7 +14,7 @@
* `$XDG_CONFIG_HOME/shade/` and reused; nothing to configure.
*/
import { chmod, mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
import { chmod, mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
import { createWriteStream } from 'node:fs';
import { homedir, tmpdir } from 'node:os';
import { basename, dirname, extname, join } from 'node:path';
@@ -31,6 +31,7 @@ import { OwnerKey } from './owner.js';
import type { MediaMeta } from './sealed.js';
import { MediaSplitter } from './splitter.js';
import { SUBTITLE_EXTENSIONS, subtitleFor, subtitlesOf, withSubtitles } from './subtitles.js';
import { prepareForWeb, webIssues } from './web-prep.js';
import type { SubtitleTrack } from './subtitles.js';
import type { LiveContainer } from './splitter.js';
@@ -208,18 +209,42 @@ async function putFile(client: MediaClient, file: string, flags: Flags, as: { na
let meta: MediaMeta = { name: as.name ?? basename(file) };
const mime = as.mime ?? guessMime(file);
if (mime !== undefined) meta.mime = mime;
if (mime?.startsWith('video/')) {
const tracks = await putSubtitles(client, file, flags);
if (tracks.length > 0) meta = withSubtitles(meta, tracks);
}
const size = (await stat(file)).size;
const tmp = join(tmpdir(), `shade-seal-${toHex(randomBytes(8))}.smf`);
let source = file;
let work: string | null = null;
try {
err(`sealing ${basename(file)} (${(size / 1048576).toFixed(1)} MiB)…`);
const result = await sealFile(file, tmp, { meta });
return await client.putSealed(result, Bun.file(tmp), { meta, ...putOpts(flags) });
if (mime?.startsWith('video/')) {
const tracks = await putSubtitles(client, file, flags);
if (flags.web === true) {
// Re-encode only what browsers cannot play; pull text subtitles out as tracks.
work = await mkdtemp(join(tmpdir(), 'shade-web-'));
const prep = await prepareForWeb(file, work, { onProgress: (l) => err(` ${basename(file)}: ${l}`) });
const stem = basename(file).slice(0, basename(file).length - extname(file).length);
source = prep.file;
meta = { name: as.name ?? `${stem}.mp4`, mime: prep.mime };
for (const sub of prep.subtitles) {
const name = `${stem}${sub.lang !== undefined ? `.${sub.lang}` : ''}.srt`;
const grant = await putFile(client, sub.path, flags, { name, mime: 'text/plain; charset=utf-8' });
tracks.push({ grant, ...(sub.lang !== undefined ? { lang: sub.lang } : {}), ...(sub.title !== undefined ? { label: sub.title } : {}) });
}
} else {
const issues = await webIssues(file);
if (issues.length > 0) {
err(` note: browsers will not play ${issues.join(', ')} in ${basename(file)} (most Android devices will); add --web to convert`);
}
}
if (tracks.length > 0) meta = withSubtitles(meta, tracks);
}
const size = (await stat(source)).size;
const tmp = join(tmpdir(), `shade-seal-${toHex(randomBytes(8))}.smf`);
try {
err(`sealing ${meta.name ?? basename(file)} (${(size / 1048576).toFixed(1)} MiB)…`);
const result = await sealFile(source, tmp, { meta });
return await client.putSealed(result, Bun.file(tmp), { meta, ...putOpts(flags) });
} finally {
await rm(tmp, { force: true });
}
} finally {
await rm(tmp, { force: true });
if (work !== null) await rm(work, { recursive: true, force: true });
}
}
@@ -528,9 +553,13 @@ export const MEDIA_HELP = `
Run a media relay: sealed store, live relay, watch page.
Only this machine's key (and --owners) may upload/publish.
media whoami Print this machine's owner key (to add to a relay's --owners)
media put <file|dir>… --server URL [--album T] [--name N] [--mime M] [--public] [--json]
media put <file|dir>… --server URL [--album T] [--name N] [--mime M] [--web] [--public] [--json]
Seal a film/video/image/song, upload it, print a watch link.
Several files or a directory become an album: one link for all.
Film.no.srt etc. next to a film travel with it as subtitles.
--web: make films play in any browser first (ffmpeg): copy
what browsers play, AC-3/DTS audio → AAC, HEVC → H.264,
embedded text subtitles → subtitle tracks.
media get <link> [-o file|dir|-]
Download and decrypt a sealed file (an album: a directory)
media ls --server URL Your uploads on that server