-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo.ts
More file actions
126 lines (115 loc) · 3.44 KB
/
video.ts
File metadata and controls
126 lines (115 loc) · 3.44 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { AcBackgroundJob } from "./models/acBackgroundJob";
const ffmpegStatic = require("ffmpeg-static");
const ffprobeStatic = require("ffprobe-static");
const _ = require("lodash");
process.env.FFMPEG_PATH = ffmpegStatic;
process.env.FFPROBE_PATH = ffprobeStatic.path;
const ffmpeg = require("fluent-ffmpeg");
ffmpeg.setFfmpegPath(ffmpegStatic);
ffmpeg.setFfprobePath(ffprobeStatic.path);
console.log(`ffprope ${ffprobeStatic.path}`)
ffmpeg.getAvailableEncoders((err:any, encoders:any) => {
//console.log('getAvailableEncoders', encoders);
});
console.log(`ffmpeg path ${ffmpegStatic}`)
export const encodeVideo = async (
videoInFilename: string,
videoOutFilename: string,
jobData: JobDataAttributes,
acBackgroundJob: AcBackgroundJob
) => {
return (await new Promise(async (resolve, reject) => {
try {
let height, width;
if (jobData.portrait) {
height = 1280;
width = 720;
} else {
width = 1280;
height = 720;
}
ffmpeg()
.setFfmpegPath(ffmpegStatic)
.setFfprobePath(ffprobeStatic.path)
.input(videoInFilename)
.videoBitrate(2400)
.videoCodec("libx264")
.size(`${width}x${height}`)
.audioCodec("aac")
.audioBitrate(160)
.audioFrequency(44100)
.outputOptions([
"-movflags +faststart",
"-g 48",
])
.on("progress", (info: any) => {
console.log(`Video progress ${info}`)
//acBackgroundJob.progress = info.percent / 2;
//await acBackgroundJob.save();
})
.on("end", () => {
console.log(`Video Encoding End ${videoOutFilename}`)
ffmpeg.ffprobe(videoOutFilename, (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(videoOutFilename);
} catch (error) {
reject(error);
}
})) as number;
};
export const createScreenshots = async (
filename: string,
outFolder: string,
jobData: JobDataAttributes,
duration: number
) => {
return await new Promise(async (resolve, reject) => {
try {
let screenshotConfig = {
filename: jobData.thumbnailPattern.replace("{count}", "%0000i")+".png",
folder: outFolder,
size: jobData.portrait ? "486x864" : "864x486",
};
if (duration < 11) {
screenshotConfig = _.merge(screenshotConfig, {
timestamps: ["0%"],
});
} else {
screenshotConfig = _.merge(screenshotConfig, {
count: duration / 10,
});
}
ffmpeg(filename)
.setFfmpegPath(ffmpegStatic)
.setFfprobePath(ffprobeStatic.path)
.screenshots(screenshotConfig)
.on("end", () => {
resolve(true);
})
.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);
});
} catch (error) {
reject(error);
}
});
};
module.exports = {
createScreenshots,
encodeVideo
};