mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 19:03:46 +02:00
Add remote URL option to examples
This commit is contained in:
@@ -2,6 +2,7 @@ import {
|
||||
Input,
|
||||
ALL_FORMATS,
|
||||
BlobSource,
|
||||
UrlSource,
|
||||
Output,
|
||||
BufferTarget,
|
||||
Mp4OutputFormat,
|
||||
@@ -12,7 +13,8 @@ import {
|
||||
import SampleFileUrl from '../../docs/assets/big-buck-bunny-trimmed.mp4';
|
||||
(document.querySelector('#sample-file-download') as HTMLAnchorElement).href = SampleFileUrl;
|
||||
|
||||
const selectMediaButton = document.querySelector('button') as HTMLButtonElement;
|
||||
const selectMediaButton = document.querySelector('#select-file') as HTMLButtonElement;
|
||||
const loadUrlButton = document.querySelector('#load-url') as HTMLButtonElement;
|
||||
const fileNameElement = document.querySelector('#file-name') as HTMLParagraphElement;
|
||||
const horizontalRule = document.querySelector('hr') as HTMLHRElement;
|
||||
const progressBarContainer = document.querySelector('#progress-bar-container') as HTMLDivElement;
|
||||
@@ -25,11 +27,11 @@ const errorElement = document.querySelector('#error-element') as HTMLParagraphEl
|
||||
let currentConversion: Conversion | null = null;
|
||||
let currentIntervalId = -1;
|
||||
|
||||
const compressFile = async (file: File) => {
|
||||
const compressFile = async (resource: File | string) => {
|
||||
clearInterval(currentIntervalId);
|
||||
await currentConversion?.cancel();
|
||||
|
||||
fileNameElement.textContent = file.name;
|
||||
fileNameElement.textContent = resource instanceof File ? resource.name : resource;
|
||||
horizontalRule.style.display = '';
|
||||
progressBarContainer.style.display = '';
|
||||
speedometer.style.display = '';
|
||||
@@ -39,12 +41,17 @@ const compressFile = async (file: File) => {
|
||||
errorElement.textContent = '';
|
||||
|
||||
try {
|
||||
// Create a new input from the file
|
||||
// Create a new input from the resource
|
||||
const source = resource instanceof File
|
||||
? new BlobSource(resource)
|
||||
: new UrlSource(resource);
|
||||
const input = new Input({
|
||||
source: new BlobSource(file),
|
||||
source,
|
||||
formats: ALL_FORMATS, // Accept all formats
|
||||
});
|
||||
|
||||
const fileSize = await source.getSize();
|
||||
|
||||
// Define the output file
|
||||
const output = new Output({
|
||||
target: new BufferTarget(),
|
||||
@@ -96,7 +103,7 @@ const compressFile = async (file: File) => {
|
||||
|
||||
compressionFacts.style.display = '';
|
||||
compressionFacts.textContent
|
||||
= `${(output.target.buffer!.byteLength / file.size * 100).toPrecision(3)}% of original size`;
|
||||
= `${(output.target.buffer!.byteLength / fileSize * 100).toPrecision(3)}% of original size`;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
@@ -130,6 +137,19 @@ selectMediaButton.addEventListener('click', () => {
|
||||
fileInput.click();
|
||||
});
|
||||
|
||||
loadUrlButton.addEventListener('click', () => {
|
||||
const url = prompt(
|
||||
'Please enter a URL of a media file. Note that it must support cross-origin requests, so have the right'
|
||||
+ ' CORS headers set.',
|
||||
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
|
||||
);
|
||||
if (!url) {
|
||||
return;
|
||||
}
|
||||
|
||||
void compressFile(url);
|
||||
});
|
||||
|
||||
document.addEventListener('dragover', (event) => {
|
||||
event.preventDefault();
|
||||
event.dataTransfer!.dropEffect = 'copy';
|
||||
|
||||
Reference in New Issue
Block a user