-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanByFFmpeg.js
55 lines (51 loc) · 1.6 KB
/
cleanByFFmpeg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { FFmpeg } from "/libs/ffmpeg/ffmpeg/dist/esm/index.js"
import b64 from './base64.js'
import fileDict from './fileDict.js'
export default async function(file){
if(chrome.offscreen){
return await offscreenRun(file)
} else {
return await run(file)
}
}
async function run(file){
let ffmpeg = new FFmpeg();
let data = await file.arrayBuffer()
await ffmpeg.load({
coreURL: "/libs/ffmpeg/core/dist/esm/ffmpeg-core.js",
})
await ffmpeg.writeFile(file.name, new Uint8Array(data));
await ffmpeg.exec([
'-i', file.name,
'-codec', 'copy',
'-map_metadata', '-1',
'-map_chapters', '-1',
'-disposition', '0',
'-fflags', '+bitexact',
'-flags:v', '+bitexact',
'-flags:a', '+bitexact',
"cleaned"+file.name
]);
let result = await ffmpeg.readFile("cleaned"+file.name);
return result.buffer
}
async function offscreenRun(file){
await chrome.offscreen.createDocument({
url: '/utils/offscreen.html',
reasons: ['WORKERS'],
justification: 'To use ffmpeg.wasm in chromium'
})
let result = await chrome.runtime.sendMessage({
type:"offscreenCleanByFFmpeg",
fileDict: await fileDict.compose(file)
})
await chrome.offscreen.closeDocument()
return b64.decode(result)
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if(!chrome.offscreen && request.type=="offscreenCleanByFFmpeg"){
let file = fileDict.restore(request.fileDict)
run(file).then(ret => sendResponse(b64.encode(ret)))
}
return true
})