Add Worker.trim

This commit is contained in:
Jerome Wu
2019-11-18 22:46:39 +08:00
parent 7a380f529b
commit e06c9bd19a
5 changed files with 137 additions and 26 deletions

View File

@@ -69,18 +69,31 @@ module.exports = (_options = {}) => {
}))
);
const transcode = (inputPath, outputPath, opts, jobId) => (
const writeText = async (path, text, jobId) => (
startJob(createJob({
id: jobId,
action: 'transcode',
action: 'writeText',
payload: {
inputPath,
outputPath,
options: opts,
path,
text,
},
}))
);
const run = (args, jobId) => (
startJob(createJob({
id: jobId, action: 'run', payload: { args },
}))
);
const transcode = (inputPath, outputPath, opts = '', jobId) => (
run(`${opts} -i ${inputPath} ${outputPath}`, jobId)
);
const trim = (inputPath, outputPath, from, to, opts = '', jobId) => (
run(`${opts} -ss ${from} -i ${inputPath} -t ${to} -c copy ${outputPath}`, jobId)
);
const read = (path, jobId) => (
startJob(createJob({
id: jobId, action: 'read', payload: { path },
@@ -99,12 +112,6 @@ module.exports = (_options = {}) => {
}))
);
const run = (args, jobId) => (
startJob(createJob({
id: jobId, action: 'run', payload: { args },
}))
);
const terminate = async (jobId) => {
if (worker !== null) {
await startJob(createJob({
@@ -145,7 +152,9 @@ module.exports = (_options = {}) => {
setReject,
load,
write,
writeText,
transcode,
trim,
read,
remove,
mkdir,

View File

@@ -26,11 +26,6 @@ const strList2ptr = (strList) => {
return listPtr;
};
const doRun = (_args) => {
const args = [...defaultArgs, ..._args.trim().split(' ')];
ffmpeg(args.length, strList2ptr(args));
};
const load = ({ workerId, payload: { options: { corePath } } }, res) => {
if (Module == null) {
const Core = adapter.getCore(corePath);
@@ -61,15 +56,14 @@ const write = ({
res.resolve({ message: `Write ${path} (${d.length} bytes)` });
};
const transcode = ({
const writeText = ({
payload: {
inputPath,
outputPath,
options = '',
path,
text,
},
}, res) => {
doRun(`${options} -i ${inputPath} ${outputPath}`);
res.resolve({ message: `Complete transcoding ${inputPath} to ${outputPath}` });
Module.FS.writeFile(path, text);
res.resolve({ message: `Write ${path} (${text.length} bytes)` });
};
const read = ({
@@ -100,11 +94,12 @@ const mkdir = ({
const run = ({
payload: {
args,
args: _args,
},
}, res) => {
doRun(args);
res.resolve({ message: `Complete ./ffmpeg ${args}` });
const args = [...defaultArgs, ..._args.trim().split(' ')];
ffmpeg(args.length, strList2ptr(args));
res.resolve({ message: `Complete ${args.join(' ')}` });
};
exports.dispatchHandlers = (packet, send) => {
@@ -124,7 +119,7 @@ exports.dispatchHandlers = (packet, send) => {
({
load,
write,
transcode,
writeText,
read,
remove,
mkdir,