Add compileSampleStats

This commit is contained in:
Vanilagy
2025-01-04 14:25:22 +01:00
parent 5bb9f8609c
commit b8c0d76503
5 changed files with 55 additions and 5 deletions
+42 -1
View File
@@ -1,10 +1,11 @@
import { AudioCodec, MediaCodec, VideoCodec } from './codec';
import { SampleRetrievalOptions } from './media-drain';
import { EncodedAudioSampleDrain, EncodedVideoSampleDrain, SampleRetrievalOptions } from './media-drain';
import { Rotation } from './misc';
import { EncodedAudioSample, EncodedVideoSample } from './sample';
export interface InputTrackBacking {
getCodec(): Promise<MediaCodec | null>;
getFirstTimestamp(): Promise<number>;
computeDuration(): Promise<number>;
}
@@ -30,6 +31,10 @@ export abstract class InputTrack {
return this instanceof InputAudioTrack;
}
getFirstTimestamp() {
return this._backing.getFirstTimestamp();
}
computeDuration() {
return this._backing.computeDuration();
}
@@ -109,6 +114,10 @@ export class InputVideoTrack extends InputTrack {
return false;
}
}
computeSampleStats() {
return computeSampleStats(new EncodedVideoSampleDrain(this));
}
}
export interface InputAudioTrackBacking extends InputTrackBacking {
@@ -174,4 +183,36 @@ export class InputAudioTrack extends InputTrack {
return false;
}
}
computeSampleStats() {
return computeSampleStats(new EncodedAudioSampleDrain(this));
}
}
/** @public */
export type SampleStats = {
sampleCount: number;
averageSampleRate: number;
averageBitrate: number;
};
const computeSampleStats = async (drain: EncodedVideoSampleDrain | EncodedAudioSampleDrain): Promise<SampleStats> => {
let startTimestamp = Infinity;
let endTimestamp = -Infinity;
let sampleCount = 0;
let totalSampleBytes = 0;
for await (const sample of drain.samples(undefined, undefined, { metadataOnly: true })) {
startTimestamp = Math.min(startTimestamp, sample.timestamp);
endTimestamp = Math.max(endTimestamp, sample.timestamp + sample.duration);
sampleCount++;
totalSampleBytes += sample.byteLength;
}
return {
sampleCount,
averageSampleRate: sampleCount ? Math.fround(sampleCount / (endTimestamp - startTimestamp)) : 0,
averageBitrate: sampleCount ? Math.fround(8 * totalSampleBytes / (endTimestamp - startTimestamp)) : 0,
};
};