Remove input track descriptors, add async getters on tracks for everything, deprecate old sync getters

This commit is contained in:
Vanilagy
2026-04-10 16:23:17 +02:00
parent aa810fc433
commit 051578c2ca
42 changed files with 1386 additions and 1504 deletions
+6 -6
View File
@@ -109,7 +109,7 @@ const initMediaPlayer = async (resource: File | string) => {
);
endTimestamp = await input.getDurationFromMetadata(tracks, { skipLiveWait: true })
?? await input.computeDuration(tracks, { skipLiveWait: true });
isRelativeToUnixEpoch = tracks.some(t => t?.isRelativeToUnixEpoch);
isRelativeToUnixEpoch = (await Promise.all(tracks.map(t => t.getIsRelativeToUnixEpoch()))).some(Boolean);
playbackTimeAtStart = firstTimestamp;
// Configure the time display elements accordingly
@@ -127,7 +127,7 @@ const initMediaPlayer = async (resource: File | string) => {
let problemMessage = '';
if (videoTrack) {
if (videoTrack.codec === null) {
if (await videoTrack.getCodec() === null) {
problemMessage += 'Unsupported video codec. ';
videoTrack = null;
} else if (!(await videoTrack.canDecode())) {
@@ -137,7 +137,7 @@ const initMediaPlayer = async (resource: File | string) => {
}
if (audioTrack) {
if (audioTrack.codec === null) {
if (await audioTrack.getCodec() === null) {
problemMessage += 'Unsupported audio codec. ';
audioTrack = null;
} else if (!(await audioTrack.canDecode())) {
@@ -163,7 +163,7 @@ const initMediaPlayer = async (resource: File | string) => {
// We must create the audio context with the matching sample rate for correct acoustic results
// (especially for low-sample rate files)
audioContext = new AudioContext({ sampleRate: audioTrack?.sampleRate });
audioContext = new AudioContext({ sampleRate: await audioTrack?.getSampleRate() });
gainNode = audioContext.createGain();
gainNode.connect(audioContext.destination);
updateVolume();
@@ -187,8 +187,8 @@ const initMediaPlayer = async (resource: File | string) => {
// Show the canvas if there's a video track, otherwise hide it
if (videoTrack) {
canvas.style.display = '';
canvas.width = videoTrack.displayWidth;
canvas.height = videoTrack.displayHeight;
canvas.width = await videoTrack.getDisplayWidth();
canvas.height = await videoTrack.getDisplayHeight();
} else {
canvas.style.display = 'none';
}
@@ -56,25 +56,25 @@ const extractMetadata = (resource: File | string) => {
'Ends at': input.computeDuration().then(duration => `${duration} seconds`),
'Tracks': input.getTracks().then(tracks => tracks.map(track => ({
'Type': track.type,
'Codec': track.codec,
'Codec': track.getCodec(),
'Full codec string': track.getCodecParameterString(),
'Starts at': track.getFirstTimestamp().then(start => `${start} seconds`),
'Ends at': track.computeDuration().then(duration => `${duration} seconds`),
'Language code': track.languageCode,
'Language code': track.getLanguageCode(),
...(track.isVideoTrack()
? {
'Coded width': `${track.codedWidth} pixels`,
'Coded height': `${track.codedHeight} pixels`,
'Rotation': `${track.rotation}° clockwise`,
'Pixel aspect ratio': `${track.pixelAspectRatio.num}:${track.pixelAspectRatio.den}`,
'Display width': `${track.displayWidth} pixels`,
'Display height': `${track.displayHeight} pixels`,
'Coded width': track.getCodedWidth().then(w => `${w} pixels`),
'Coded height': track.getCodedHeight().then(h => `${h} pixels`),
'Rotation': track.getRotation().then(rot => `${rot}° clockwise`),
'Pixel aspect ratio': track.getPixelAspectRatio().then(par => `${par.num}:${par.den}`),
'Display width': track.getDisplayWidth().then(w => `${w} pixels`),
'Display height': track.getDisplayHeight().then(h => `${h} pixels`),
'Transparency': track.canBeTransparent(),
}
: track.isAudioTrack()
? {
'Number of channels': track.numberOfChannels,
'Sample rate': `${track.sampleRate} Hz`,
'Number of channels': track.getNumberOfChannels(),
'Sample rate': track.getSampleRate().then(rate => `${rate} Hz`),
}
: {}),
'Packet statistics': shortDelay().then(() => track.computePacketStats()).then(stats => ({
@@ -34,7 +34,7 @@ const generateThumbnails = async (resource: File | string) => {
throw new Error('File has no video track.');
}
if (videoTrack.codec === null) {
if (await videoTrack.getCodec() === null) {
throw new Error('Unsupported video codec.');
}
@@ -43,12 +43,14 @@ const generateThumbnails = async (resource: File | string) => {
}
// Compute width and height of the thumbnails such that the larger dimension is equal to THUMBNAIL_SIZE
const width = videoTrack.displayWidth > videoTrack.displayHeight
const displayWidth = await videoTrack.getDisplayWidth();
const displayHeight = await videoTrack.getDisplayHeight();
const width = displayWidth > displayHeight
? THUMBNAIL_SIZE
: Math.floor(THUMBNAIL_SIZE * videoTrack.displayWidth / videoTrack.displayHeight);
const height = videoTrack.displayHeight > videoTrack.displayWidth
: Math.floor(THUMBNAIL_SIZE * displayWidth / displayHeight);
const height = displayHeight > displayWidth
? THUMBNAIL_SIZE
: Math.floor(THUMBNAIL_SIZE * videoTrack.displayHeight / videoTrack.displayWidth);
: Math.floor(THUMBNAIL_SIZE * displayHeight / displayWidth);
// Create thumbnail elements
const thumbnailElements = [];