-
Notifications
You must be signed in to change notification settings - Fork 1
/
_fluent2.js
125 lines (116 loc) · 3.54 KB
/
_fluent2.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
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
import path from "path";
import fs from "fs";
import ffmpeg from "fluent-ffmpeg";
const input = path.resolve(process.cwd(), "client/src/components", "video.mp4");
const output = path.resolve(
process.cwd(),
"client/src/PlayGround",
"video.mp4"
);
console.log(input, output);
const outputWidth = 1920; // Desired output width
const outputHeight = 1080; // Desired output height
function resizingFFmpeg(video, width, height, tempFile, autoPad, padColor) {
return new Promise((res, rej) => {
let ff = ffmpeg()
.input(video)
.outputOptions(`-vf scale=${width}:${height},setsar=1`);
autoPad ? (ff = ff.autoPad(autoPad, padColor)) : null;
ff.output(tempFile)
.on("progress", function(progress) {
console.log("...Resizing frame ", progress.frames);
})
.on("error", function(err) {
console.log("Problem performing ffmpeg resize");
rej(err);
})
.on("end", function() {
console.log("End resizingFFmpeg:", tempFile);
res(tempFile);
})
.run();
});
}
async function getDimentions(media) {
return new Promise((res, rej) => {
ffmpeg.ffprobe(media, (err, metadata) => {
if (err) return rej(err);
res({
width: metadata.streams[0].width,
height: metadata.streams[0].height
});
});
});
}
function videoCropCenterFFmpeg(video, w, h, tempFile) {
return new Promise((res, rej) => {
ffmpeg()
.input(video)
.videoFilters([
{
filter: "crop",
options: {
w,
h
}
}
])
.output(tempFile)
.on("progress", function(progress) {
console.log("...Cropping frame ", progress.frames);
})
.on("error", function(err) {
console.log("Problem performing ffmpeg cropping function");
rej(err);
})
.on("end", function() {
console.log("End videoCropCenterFFmpeg:", tempFile);
res(tempFile);
})
.run();
});
}
const scaleAndResizeVideo = async (video, options = {}) => {
try {
const { width: newWidth = "640", height: newHeight = "1024" } = options;
const { width, height } = await getDimentions(video);
if ((width / height).toFixed(2) > (newWidth / newHeight).toFixed(2)) {
// y=0 case
// landscape to potrait case
const x = width - (newWidth / newHeight) * height;
console.log(`New Intrim Res: ${width - x}x${height}`);
const cropping = path.resolve(
process.cwd(),
"client/src/PlayGround",
"tmpFile.mp4"
);
// let cropped = await videoCropCenterFFmpeg(
// video,
// width - x,
// height,
// cropping
// );
// unlink temp cropping file
// fs.unlink(cropping, err => {
// if (err) console.log("Erorr unlinking: ", err.message);
// console.log(`Temp file ${cropping} deleted Successfuly...`);
// });
let resized = await resizingFFmpeg(video, newWidth, newHeight, output);
return resized;
} else if (
(width / height).toFixed(2) < (newWidth / newHeight).toFixed(2)
) {
// x=0 case
// potrait to landscape case
// calculate crop or resize with padding or blur sides
// or just return with black bars on the side
return await resizingFFmpeg(video, newWidth, newHeight, output, true);
} else {
console.log("Same Aspect Ratio forward for resizing");
return await resizingFFmpeg(video, newWidth, newHeight, output);
}
} catch (err) {
console.log(err.message);
}
};
scaleAndResizeVideo(input);