Descriptive error messages for video stories

This commit is contained in:
Fedor Indutny 2023-02-28 14:17:22 -08:00 committed by GitHub
parent c038c07b06
commit 4549291b7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 289 additions and 179 deletions

View file

@ -3,9 +3,10 @@
import MP4Box from 'mp4box';
import { VIDEO_MP4, isVideo } from '../types/MIME';
import { SECOND } from './durations';
import { KIBIBYTE, getRenderDetailsForLimit } from '../types/AttachmentSize';
import { explodePromise } from './explodePromise';
const MAX_VIDEO_DURATION = 30 * SECOND;
const MAX_VIDEO_DURATION_IN_SEC = 30;
type MP4ArrayBuffer = ArrayBuffer & { fileStart: number };
@ -13,6 +14,7 @@ export enum ReasonVideoNotGood {
AllGoodNevermind = 'AllGoodNevermind',
CouldNotReadFile = 'CouldNotReadFile',
TooLong = 'TooLong',
TooBig = 'TooBig',
UnsupportedCodec = 'UnsupportedCodec',
UnsupportedContainer = 'UnsupportedContainer',
}
@ -25,70 +27,106 @@ function createMp4ArrayBuffer(src: ArrayBuffer): MP4ArrayBuffer {
return arrayBuffer as MP4ArrayBuffer;
}
export type IsVideoGoodForStoriesResultType = Readonly<
| {
reason: Exclude<
ReasonVideoNotGood,
ReasonVideoNotGood.TooLong | ReasonVideoNotGood.TooBig
>;
}
| {
reason: ReasonVideoNotGood.TooLong;
maxDurationInSec: number;
}
| {
reason: ReasonVideoNotGood.TooBig;
renderDetails: ReturnType<typeof getRenderDetailsForLimit>;
}
>;
export type IsVideoGoodForStoriesOptionsType = Readonly<{
maxAttachmentSizeInKb: number;
}>;
export async function isVideoGoodForStories(
file: File
): Promise<ReasonVideoNotGood> {
file: File,
{ maxAttachmentSizeInKb }: IsVideoGoodForStoriesOptionsType
): Promise<IsVideoGoodForStoriesResultType> {
if (!isVideo(file.type)) {
return ReasonVideoNotGood.AllGoodNevermind;
return { reason: ReasonVideoNotGood.AllGoodNevermind };
}
if (file.type !== VIDEO_MP4) {
return ReasonVideoNotGood.UnsupportedContainer;
return { reason: ReasonVideoNotGood.UnsupportedContainer };
}
try {
const src = await new Promise<ArrayBuffer>((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (reader.result) {
resolve(reader.result as ArrayBuffer);
} else {
reject(ReasonVideoNotGood.CouldNotReadFile);
}
};
reader.readAsArrayBuffer(file);
});
let src: ArrayBuffer;
const arrayBuffer = createMp4ArrayBuffer(src);
{
const { promise, resolve } = explodePromise<ArrayBuffer | undefined>();
const mp4 = MP4Box.createFile();
await new Promise<void>((resolve, reject) => {
mp4.onReady = info => {
// mp4box returns a `duration` in `timescale` units
const seconds = info.duration / info.timescale;
const milliseconds = seconds * 1000;
const reader = new FileReader();
reader.onload = () => {
if (reader.result) {
resolve(reader.result as ArrayBuffer);
} else {
resolve(undefined);
}
};
reader.readAsArrayBuffer(file);
if (milliseconds > MAX_VIDEO_DURATION) {
reject(ReasonVideoNotGood.TooLong);
return;
}
const codecs = /codecs="([\w,.]+)"/.exec(info.mime);
if (!codecs || !codecs[1]) {
reject(ReasonVideoNotGood.UnsupportedCodec);
return;
}
const isH264 = codecs[1]
.split(',')
.some(codec => codec.startsWith('avc1'));
if (!isH264) {
reject(ReasonVideoNotGood.UnsupportedCodec);
return;
}
resolve();
};
mp4.appendBuffer(arrayBuffer);
});
mp4.flush();
return ReasonVideoNotGood.AllGoodNevermind;
} catch (err) {
if (err instanceof Error) {
throw err;
const maybeSrc = await promise;
if (maybeSrc === undefined) {
return { reason: ReasonVideoNotGood.CouldNotReadFile };
}
return err;
src = maybeSrc;
}
if (src.byteLength / KIBIBYTE > maxAttachmentSizeInKb) {
return {
reason: ReasonVideoNotGood.TooBig,
renderDetails: getRenderDetailsForLimit(maxAttachmentSizeInKb),
};
}
const arrayBuffer = createMp4ArrayBuffer(src);
const { promise, resolve } =
explodePromise<IsVideoGoodForStoriesResultType>();
const mp4 = MP4Box.createFile();
mp4.onReady = info => {
// mp4box returns a `duration` in `timescale` units
const seconds = info.duration / info.timescale;
if (seconds > MAX_VIDEO_DURATION_IN_SEC) {
resolve({
reason: ReasonVideoNotGood.TooLong,
maxDurationInSec: MAX_VIDEO_DURATION_IN_SEC,
});
return;
}
const codecs = /codecs="([\w,.]+)"/.exec(info.mime);
if (!codecs || !codecs[1]) {
resolve({ reason: ReasonVideoNotGood.UnsupportedCodec });
return;
}
const isH264 = codecs[1].split(',').some(codec => codec.startsWith('avc1'));
if (!isH264) {
resolve({ reason: ReasonVideoNotGood.UnsupportedCodec });
return;
}
resolve({ reason: ReasonVideoNotGood.AllGoodNevermind });
};
mp4.appendBuffer(arrayBuffer);
try {
return await promise;
} finally {
mp4.flush();
}
}