-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.ts
88 lines (81 loc) · 2.43 KB
/
audio.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { AcBackgroundJob } from "./models/acBackgroundJob";
const ffmpegStatic = require("ffmpeg-static");
const ffprobeStatic = require("ffprobe-static");
const ffmpeg = require("fluent-ffmpeg");
ffmpeg.setFfmpegPath(ffmpegStatic);
ffmpeg.setFfprobePath(ffprobeStatic.path);
export const encodeAudio = async (
audioInFilename: string,
audioOutFilename: string,
jobData: JobDataAttributes,
acBackgroundJob: AcBackgroundJob
) => {
return (await new Promise(async (resolve, reject) => {
try {
ffmpeg()
.setFfmpegPath(ffmpegStatic)
.setFfprobePath(ffprobeStatic.path)
.input(audioInFilename)
.audioCodec("aac")
.audioBitrate(160)
.audioFrequency(44100)
.on("progress", async (info: any) => {
//acBackgroundJob.progress = info.percent / 2;
//await acBackgroundJob.save();
})
.on("end", () => {
ffmpeg.ffprobe(audioOutFilename, (err: string, metadata: any) => {
if (err) {
reject(err);
} else {
resolve(metadata.format.duration as number);
}
});
})
.on("error", (err: any, stdout: string, stderr: string) => {
console.error(`Error: ${err.message}`);
console.error(`ffmpeg output: ${stdout}`);
console.error(`ffmpeg stderr: ${stderr}`);
reject(err);
})
.save(audioOutFilename);
} catch (error) {
reject(error);
}
})) as number;
};
export const encodeFlac = async (
videoInFilename: string,
audioOutFilename: string
) => {
return (await new Promise(async (resolve, reject) => {
try {
ffmpeg()
.setFfmpegPath(ffmpegStatic)
.setFfprobePath(ffprobeStatic.path)
.input(videoInFilename)
.audioCodec("flac")
.on("progress", async (info: any) => {
//acBackgroundJob.progress = info.percent / 2;
//await acBackgroundJob.save();
})
.on("end", () => {
resolve(1);
})
.on("error", (err: any, stdout: string, stderr: string) => {
console.error(`Error: ${err.message}`);
console.error(`ffmpeg output: ${stdout}`);
console.error(`ffmpeg stderr: ${stderr}`);
resolve(0);
})
.save(audioOutFilename);
} catch (error) {
console.error(error);
resolve(0);
}
})) as number;
};
module.exports = {
encodeAudio,
encodeFlac
};