common: ffmpeg resolution fix, raw video fixes

This commit is contained in:
Koushik Dutta
2022-05-16 23:36:49 -07:00
parent 2bf6f2e268
commit 03fc068365
2 changed files with 15 additions and 9 deletions

View File

@@ -294,8 +294,8 @@ export async function startParserSession<T extends string>(ffmpegInput: FFmpegIn
// tbh parsing stdout is super sketchy way of doing this.
parseAudioCodec(cp).then(result => inputAudioCodec = result);
await parseVideoCodec(cp).then(result => inputVideoCodec = result);
parseResolution(cp).then(result => inputVideoResolution = result);
await parseVideoCodec(cp).then(result => inputVideoCodec = result);
return {
sdp,
@@ -307,8 +307,8 @@ export async function startParserSession<T extends string>(ffmpegInput: FFmpegIn
},
get inputVideoResolution() {
return {
width: parseInt(inputVideoResolution?.[1]),
height: parseInt(inputVideoResolution?.[2]),
width: parseInt(inputVideoResolution?.[2]),
height: parseInt(inputVideoResolution?.[3]),
}
},
get isActive() { return isActive },

View File

@@ -209,7 +209,7 @@ export function createFragmentedMp4Parser(options?: StreamParserOptions): Stream
}
export interface RawVideoParserOptions {
size?: {
size: {
width: number,
height: number
};
@@ -232,10 +232,9 @@ export const PIXEL_FORMAT_RGB24: RawVideoPixelFormat = {
computeLength: (width, height) => width * height * 3,
}
export function createRawVideoParser(options?: RawVideoParserOptions): StreamParser {
export function createRawVideoParser(options: RawVideoParserOptions): StreamParser {
const pixelFormat = options?.pixelFormat || PIXEL_FORMAT_YUV420P;
let filter: string;
options = options || {};
const { size, everyNFrames } = options;
if (size) {
filter = `scale=${size.width}:${size.height}`;
@@ -248,21 +247,28 @@ export function createRawVideoParser(options?: RawVideoParserOptions): StreamPar
filter = filter + `select=not(mod(n\\,${everyNFrames}))`
}
const inputArguments: string[] = [];
if (options.size)
inputArguments.push('-s', `${options.size.width}x${options.size.height}`);
inputArguments.push('-pix_fmt', pixelFormat.name);
return {
inputArguments,
container: 'rawvideo',
outputArguments: [
...(filter ? ['-vf', filter] : []),
'-s', `${options.size.width}x${options.size.height}`,
'-an',
'-vcodec', 'rawvideo',
'-pix_fmt', pixelFormat.name,
'-f', 'rawvideo',
],
async *parse(socket: Duplex, width: number, height: number): AsyncGenerator<StreamChunk> {
width = size?.width || width;
height = size?.height || height
if (!width || !height)
throw new Error("error parsing rawvideo, unknown width and height");
width = size?.width || width;
height = size?.height || height
const toRead = pixelFormat.computeLength(width, height);
while (true) {
const buffer = await readLength(socket, toRead);