-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (157 loc) · 4.93 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const { Telegraf } = require('telegraf');
const { createCanvas, loadImage } = require('canvas');
var ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const path = require('path');
const https = require('https');
const config = require('./config.json');
const bot = new Telegraf(config.BOT_TOKEN);
const assetPath = (filename) => path.resolve(__dirname, 'assets', filename);
const fileIdPath = (fileId, ext, type = '') =>
path.resolve(__dirname, 'tmp', `${fileId}${type}.${ext}`);
const toVideo = (fileId, outname, { replyWithVideo, reply, message }) => {
const vid1 = assetPath('1.mp4');
const vid2 = fileIdPath(fileId, 'mp4', '-2');
const vid3 = assetPath('3.mp4');
const finalNoAudio = fileIdPath(fileId, 'mp4', '-noaudio');
const final = fileIdPath(fileId, 'mp4');
const audio = assetPath('audio.mp3');
const deleteAll = () => {
fs.unlink(outname, () => {});
fs.unlink(final, () => {});
fs.unlink(finalNoAudio, () => {});
fs.unlink(vid2, () => {});
};
const onError = (err) => {
reply('Unfortunately we broken :( try again later', {
reply_to_message_id: message.message_id,
});
console.log('An error occurred: ' + err.message);
deleteAll();
};
console.log('[pipeline 1 of 3]', fileId);
ffmpeg(outname)
.loop(1.259)
.fps(25)
.size('640x480')
.videoCodec('libx264')
.format('mp4')
.noAudio()
.save(vid2)
.on('end', () => {
console.log('[pipeline 2 of 3]', fileId);
ffmpeg()
.mergeAdd(vid1)
.mergeAdd(vid2)
.mergeAdd(vid3)
.mergeToFile(finalNoAudio)
.on('end', () => {
console.log('[pipeline 3 of 3]', fileId);
ffmpeg()
.addInput(finalNoAudio)
.addInput(audio)
.save(final)
.on('end', async () => {
console.log('[send to telegram]', fileId);
try {
await replyWithVideo(
{
filename: `${fileId}.mp4`,
source: final,
},
{
reply_to_message_id: message.message_id,
}
);
} catch (e) {}
deleteAll();
})
.on('error', onError);
})
.on('error', onError);
})
.on('error', onError);
};
const proc = async (fileId, filepath, tele) => {
try {
const image = await loadImage(assetPath('template.png'));
const tempel = await loadImage(filepath);
const canvas = createCanvas(image.width, image.height);
const ctx = canvas.getContext('2d');
ctx.rotate(0);
const w = 265;
const h = 255;
// get the scale
var scale = Math.min(w / tempel.width, h / tempel.height);
var x = w / 2 - (tempel.width / 2) * scale;
var y = h / 2 - (tempel.height / 2) * scale;
// Draw cat with lime helmet
ctx.drawImage(image, 0, 0, image.width, image.height);
ctx.setTransform(0.98, -0.17, 0.3, 1.057, 100, 100);
ctx.drawImage(
tempel,
80 + x,
45 + y,
tempel.width * scale,
tempel.height * scale
);
let outname = fileIdPath(fileId, 'jpg', '-i');
const out = fs.createWriteStream(outname);
const stream = canvas.createJPEGStream();
stream.pipe(out);
out
.on('finish', () => {
toVideo(fileId, outname, tele);
fs.unlink(filepath, () => {});
})
.on('error', () => {
tele.reply('Unfortunately we broken :( try again later', {
reply_to_message_id: tele.message.message_id,
});
});
} catch (e) {
tele.reply('Unfortunately we broken :( try again later', {
reply_to_message_id: tele.message.message_id,
});
}
};
const run = () => {
bot.on('photo', async (ctx) => {
const { message, chat, tg, reply, replyWithChatAction } = ctx;
console.log(JSON.stringify(message, null, 2));
if (chat.type !== 'private') {
if (message.caption && message.caption.match(new RegExp('@t0mas_bot'))) {
// contiune...
} else {
return false;
}
}
replyWithChatAction('upload_video');
let file = null;
if (message.photo.length > 2) {
file = message.photo[1];
} else {
file = message.photo[message.photo.length - 1];
}
const { file_unique_id, file_id } = file;
const res = await tg.getFileLink(file_id);
const ext = res.match(/\.([a-z]+)$/)[0];
const outname = path.resolve(__dirname, 'tmp', `${file_unique_id}${ext}`);
const out = fs.createWriteStream(outname);
out.on('finish', () => {
console.log('[downloaded done]', file_unique_id);
proc(file_unique_id, outname, ctx);
});
const request = https.get(res, function (response) {
response.pipe(out);
});
request.on('error', (e) => {
reply('Unfortunately we broken :( try again later', {
reply_to_message_id: message.message_id,
});
});
});
console.log('listening...');
bot.launch();
};
run();