Skip to content

Commit

Permalink
fix: compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiaoxuan committed Jun 11, 2024
1 parent c598dc4 commit bce8e0d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 68 deletions.
81 changes: 43 additions & 38 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,72 @@

<body>

<input id="js_file" type="file">
<input id="js_file" type="file" multiple>

<input id="js_time" type="number" value="1" placeholder="画面时间s">
<input id="js_time" type="number" value="5" placeholder="画面时间s">

<button id="js_button">提取</button>


<div id="js_info"></div>

<div id="js_result"></div>

<!-- <script src="./capture.js" type="application/javascript"></script> -->
<script src="../dist/index.js" type="application/javascript"></script>
<script>
const workerPath = new URL(location.origin + '/dist/capture.worker.js');
const wasmPath = new URL(location.origin + '/dist/capture.worker.wasm');
let button = document.querySelector('#js_button');
let resultContainer = document.querySelector('#js_result');
let fileInput = document.querySelector('#js_file');
let timeInput = document.querySelector('#js_time');
let infoContainer = document.querySelector('#js_info');
let timeInput = document.querySelector('#js_time');
const captureTimes = parseInt(timeInput.value, 10);

const capturePro = cheetahCapture.initCapture({
workerPath,
wasmPath,
});

button.addEventListener('click', async () => {
let file = fileInput.files[0];
console.log('===>file', file);
let startTime = Date.now();
capturePro.then((res) => {
res.mountFile({
file,
onSuccess: () => {
console.log('===>mountFile success');
res.getMetadata({
info: 'aigc',
onSuccess: (data) => {
console.log('===>getMetadata', data);
}
});
res.capture({
file,
info: 11,
onChange: (list = {}, now = {}, info = {}) => {
console.log('==>onchange', list, now, info);
const { width, height, duration } = info;
const img = document.createElement('img');
img.src = now.url;
resultContainer.append(img);
const files = fileInput.files;
for (let i = 0; i < files.length; i++) {
let file = files[i];
let startTime = Date.now();
const res = await capturePro;
await new Promise((resolve, reject) => {
res.mountFile({
file,
onSuccess: () => {
console.log('===>mountFile success', i);
res.capture({
info: captureTimes,
onChange: (list = {}, now = {}, info = {}) => {
console.log('==>onchange', list, now, info);
const { width, height, duration } = info;
const img = document.createElement('img');
img.src = now.url;
resultContainer.append(img);

infoContainer.innerHTML = `耗时:${Date.now() - startTime}ms<br>宽度:${width}<br>高度:${height}<br>时长:${duration}s`;
},
onSuccess: (list) => {
console.log('==>onSuccess', list);
res.free({onSuccess: () => {console.log('===free success')}});
}
})
}
})
})
infoContainer.innerHTML = `耗时:${Date.now() - startTime}ms<br>宽度:${width}<br>高度:${height}<br>时长:${duration}s`;
},
onSuccess: (list) => {
console.log('==>onSuccess', list);
res.free({
onSuccess: () => {
console.log('===free success');
resolve();
}
});
}
});
},
onError: (error) => {
console.error('Error mounting file:', error);
reject(error);
}
});
});
}
});
</script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cheetah-capture",
"version": "0.2.0-beta.3",
"version": "0.2.1",
"description": "cheetah-capture是基于ffmpeg的wasm截取视频帧工具",
"keywords": [
"ffmpeg",
Expand Down
26 changes: 0 additions & 26 deletions src/capture.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,43 +191,17 @@ class ImageCapture {
}

getMetadata(key: string, id: number) {
// getMetadata(key, path, id)
if (!this.name) {
throw new Error('Please mount file first!');
}
const cGetMetadata = Module.cwrap('getMetaDataByKey', 'string', ['string', 'string', 'number']);
const metadataValue = cGetMetadata(key, `${this.path}/${this.name}`, id);
// cGetMetadata();

self.postMessage({
type: Events.getMetadataOnSuccess,
meta: metadataValue,
id,
});
// // cGetMetadata();
// const ptr = Module.ccall('getMetaDataByKey', 'number', ['string', 'string', 'number'], [key, `${this.path}/${this.name}`, id]);
// if (!ptr) {
// console.error('Invalid pointer received');
// } else {
// console.log('===>ptr', ptr);
// const length = Module.ccall('get_string_length', 'number', ['number'], [ptr]);
// console.log('===>length', length);
// if (length > 0) {
// const data = new Uint8Array(Module.HEAPU8.buffer, ptr, length);
// const decoder = new TextDecoder('utf-8');
// const metadataValue = decoder.decode(data);
// console.log('==>from worker', metadataValue);
// self.postMessage({
// type: Events.getMetadataOnSuccess,
// meta: metadataValue,
// id,
// });
// } else {
// console.error('Received empty or invalid length');
// }
// }


}
}

Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export async function initCapture({
}
case Events.receiveImageOnSuccess: {
const {id, meta} = e.data || {};
const cbk = pool.getCbk(id);
const cbk = pool.getCbk(id) || {};
const {onSuccess} = cbk;
const {cache} = pool.getCbk(id);
onSuccess && onSuccess({
Expand All @@ -242,14 +242,14 @@ export async function initCapture({
}
case Events.receiveError: {
const {errmsg, id} = e.data || {};
const cbk = pool.getCbk(id);
const cbk = pool.getCbk(id) || {};
const {onError} = cbk;
onError && onError(errmsg);
break;
}
case Events.getMetadataOnSuccess: {
const {id, meta} = e.data || {};
const cbk = pool.getCbk(id);
const cbk = pool.getCbk(id) || {};
const {onSuccess} = cbk;
onSuccess && onSuccess({meta: meta as string});
break;
Expand Down

0 comments on commit bce8e0d

Please sign in to comment.