-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry-nvidia.js
173 lines (160 loc) · 5.45 KB
/
entry-nvidia.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
require('dotenv').config()
const spawn = require('child_process').spawn;
const { Client } = require("discord.js");
const yt = require('youtube-search-without-api-key');
const { nanoid } = require("nanoid");
const fs = require('fs').promises;
const client = new Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const path = require("path");
var moment = require('moment');
client.once('ready', () => {
console.log('Bot is running!');
});
const prefix = "!";
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const commandBody = message.content.slice(prefix.length);
const args = commandBody.split(' ');
const command = args.shift()
if (command === "ping") {
const timeTaken = Date.now() - message.createdTimestamp;
message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
}
else if (command === "s") {
const searchTerm = args.join(' ')
const videos = await yt.search(searchTerm);
const topResults = videos.length > 9 ? videos.slice(0, 9) : videos
const shortVideo = topResults.find(x => moment(x.duration_raw, "mm:ss").minutes() < 1)
if (shortVideo) {
return await handleProcess(message, shortVideo.url, true)
} else {
return message.reply(`Hyvä hakusana.... no videos under 1minute found from top 10 results ${Date.now() - message.createdTimestamp}ms`);
}
}
else if (isValidHttpUrl(command)) {
message.suppressEmbeds(true)
await handleProcess(message, command)
}
});
const handleProcess = async (message, url, reply) => {
message.channel.sendTyping()
const filename = await downloadVideo(url)
if (!filename) return message.reply(`Hyvä linkki... failed to ytdl... ${Date.now() - message.createdTimestamp}ms`);
if (await getFileSize(filename) > 8) {
const smaller = await transcode(filename, 46)
if (!smaller) return message.reply(`Hyvä linkki... failed to transcode ${Date.now() - message.createdTimestamp}ms`)
const smallerSize = await getFileSize(`output-${filename}`)
if (smallerSize > 8 ) return message.reply(`Hyvä linkki... after downgrade filesize was: ${smallerSize}Mb`)
} else {
await fs.rename(filename, `output-${filename}`)
}
if (reply) {
message.reply({ files: [`output-${filename}`] })
} else {
message.channel.send({ files: [`output-${filename}`] })
message.delete()
}
}
client.login(process.env.TOKEN)
const getFileSize = async filename => {
const stats = await fs.stat(filename)
const fileSizeInBytes = stats.size;
const fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024);
return fileSizeInMegabytes
}
const downloadVideo = async (link) => new Promise((resolve, reject) => {
const filename = nanoid(8)
const ytdlp = spawn('yt-dlp', [
"--verbose",
"--flat-playlist",
(new URL(link)).hostname.includes('tiktok.com') ? "--no-geo-bypass" : "--geo-bypass",
"--max-filesize",
"65m",
"-f",
"(mp4)[height<=720][tbr<=1500]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"-S",
"codec:h264",
"--merge-output-format",
"mp4",
"-o",
`${filename}.%(ext)s`,
`${link}`
]);
ytdlp.stderr.on('data', (data) => {
console.log(data.toString())
});
ytdlp.stdout.on('data', (data) => {
console.log(data.toString())
});
ytdlp.on('close', async (code) => {
const files = await fs.readdir(__dirname);
resolve(files.find(x => x.includes(filename)));
});
});
const transcode = (filename, crf) => new Promise((resolve, reject) => {
const ffmpeg = spawn('ffmpeg', [
'-y',
"-vsync",
"0",
"-hwaccel",
"cuvid",
"-c:v",
"h264_cuvid",
'-i',
`${filename}`,
"-c:v",
"h264_nvenc",
"-rc:v",
"vbr",
"-cq:v",
crf,
'-preset',
'slow',
"-c:a",
"aac",
"-b:a",
"128k",
`output-${filename.split(".")[0]}.mp4`
])
ffmpeg.stderr.on('data', (data) => {
console.log(`${data}`);
});
ffmpeg.on('close', async (code) => {
const duration = await getVideoLength(`output-${filename}`)
resolve(duration > 0)
});
});
const isValidHttpUrl = (string) => {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
const getVideoLength = (filename) => new Promise((resolve, reject) => {
const ffprobe = spawn('ffprobe', ["-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename]);
let duration = 0
ffprobe.stderr.on('data', (data) => {
console.log(`${data}`);
});
ffprobe.stdout.on('data', (data) => {
duration = data.toString()
});
ffprobe.on('close', (code) => {
if (code === 0) {
resolve(Number(duration));
}
resolve(-1)
});
});
// https://trac.ffmpeg.org/wiki/Encode/H.264#twopass
const calculateBitrate = (seconds) => {
// (8 MiB * 8192 [converts MiB to kBit]) / x seconds
const videoSizekBit = (8 * 8192) / seconds
// videoSizekBit - 128 kBit/s (desired audio bitrate) = x kBit/s video bitrate
const fileSizekBit = videoSizekBit - 128
return fileSizekBit
}