Improve error messages in examples

This commit is contained in:
Vanilagy
2025-07-19 17:28:39 +02:00
parent 4b12468008
commit 904ac2e366
4 changed files with 43 additions and 8 deletions
@@ -38,6 +38,7 @@ const startRecording = async () => {
mainContainer.style.display = 'none'; mainContainer.style.display = 'none';
videoElement.src = ''; videoElement.src = '';
downloadButton.style.display = 'none'; downloadButton.style.display = 'none';
errorElement.textContent = '';
// Paint a white background to the canvas // Paint a white background to the canvas
context.fillStyle = 'white'; context.fillStyle = 'white';
+2
View File
@@ -29,6 +29,8 @@
<hr class="w-full max-w-96 my-4 border-zinc-300 dark:border-zinc-700" style="display: none;"> <hr class="w-full max-w-96 my-4 border-zinc-300 dark:border-zinc-700" style="display: none;">
<p id="error-element" class="text-red-500"></p> <p id="error-element" class="text-red-500"></p>
<p id="warning-element" class="text-amber-500 mb-1"></p>
<div id="player" class="relative bg-black rounded-xl shrink min-h-14 min-w-0 w-full max-w-5xl overflow-hidden select-none" style="display: none;"> <div id="player" class="relative bg-black rounded-xl shrink min-h-14 min-w-0 w-full max-w-5xl overflow-hidden select-none" style="display: none;">
<canvas class="size-full object-contain" width="1280" height="720"></canvas> <canvas class="size-full object-contain" width="1280" height="720"></canvas>
+31 -7
View File
@@ -30,6 +30,7 @@ const volumeIconWrapper = document.querySelector('#volume-icon-wrapper') as HTML
const volumeButton = document.querySelector('#volume-button') as HTMLButtonElement; const volumeButton = document.querySelector('#volume-button') as HTMLButtonElement;
const fullscreenButton = document.querySelector('#fullscreen-button') as HTMLButtonElement; const fullscreenButton = document.querySelector('#fullscreen-button') as HTMLButtonElement;
const errorElement = document.querySelector('#error-element') as HTMLDivElement; const errorElement = document.querySelector('#error-element') as HTMLDivElement;
const warningElement = document.querySelector('#warning-element') as HTMLDivElement;
const context = canvas.getContext('2d', { alpha: false, desynchronized: true })!; const context = canvas.getContext('2d', { alpha: false, desynchronized: true })!;
@@ -81,6 +82,8 @@ const initMediaPlayer = async (file: File) => {
fileNameElement.textContent = file.name; fileNameElement.textContent = file.name;
horizontalRule.style.display = ''; horizontalRule.style.display = '';
playerContainer.style.display = 'none'; playerContainer.style.display = 'none';
errorElement.textContent = '';
warningElement.textContent = '';
// Create an Input from the file // Create an Input from the file
const input = new Input({ const input = new Input({
@@ -95,17 +98,38 @@ const initMediaPlayer = async (file: File) => {
let videoTrack = await input.getPrimaryVideoTrack(); let videoTrack = await input.getPrimaryVideoTrack();
let audioTrack = await input.getPrimaryAudioTrack(); let audioTrack = await input.getPrimaryAudioTrack();
if (!(await videoTrack?.canDecode())) { let problemMessage = '';
// We can't decode the video track, so treat it like there is no video track
videoTrack = null; if (videoTrack) {
if (videoTrack.codec === null) {
problemMessage += 'Unsupported video codec. ';
videoTrack = null;
} else if (!(await videoTrack.canDecode())) {
problemMessage += 'Unable to decode the video track. ';
videoTrack = null;
}
} }
if (!(await audioTrack?.canDecode())) {
// We can't decode the audio track, so treat it like there is no audio track if (audioTrack) {
audioTrack = null; if (audioTrack.codec === null) {
problemMessage += 'Unsupported audio codec. ';
audioTrack = null;
} else if (!(await audioTrack.canDecode())) {
problemMessage += 'Unable to decode the audio track. ';
audioTrack = null;
}
} }
if (!videoTrack && !audioTrack) { if (!videoTrack && !audioTrack) {
throw new Error('Media file has no playable video or audio track.'); if (!problemMessage) {
problemMessage = 'No audio or video track found.';
}
throw new Error(problemMessage);
}
if (problemMessage) {
warningElement.textContent = problemMessage;
} }
// We must create the audio context with the matching sample rate for correct acoustic results // We must create the audio context with the matching sample rate for correct acoustic results
@@ -15,7 +15,7 @@ const THUMBNAIL_SIZE = 200;
const generateThumbnails = async (file: File) => { const generateThumbnails = async (file: File) => {
fileNameElement.textContent = file.name; fileNameElement.textContent = file.name;
horizontalRule.style.display = ''; horizontalRule.style.display = '';
errorElement.innerHTML = ''; errorElement.textContent = '';
thumbnailContainer.innerHTML = ''; thumbnailContainer.innerHTML = '';
try { try {
@@ -30,6 +30,14 @@ const generateThumbnails = async (file: File) => {
throw new Error('File has no video track.'); throw new Error('File has no video track.');
} }
if (videoTrack.codec === null) {
throw new Error('Unsupported video codec.');
}
if (!(await videoTrack.canDecode())) {
throw new Error('Unable to decode the video track.');
}
// Compute width and height of the thumbnails such that the larger dimension is equal to THUMBNAIL_SIZE // Compute width and height of the thumbnails such that the larger dimension is equal to THUMBNAIL_SIZE
const width = videoTrack.displayWidth > videoTrack.displayHeight const width = videoTrack.displayWidth > videoTrack.displayHeight
? THUMBNAIL_SIZE ? THUMBNAIL_SIZE