feat: add gif speed up and speed down command

This commit is contained in:
2024-10-27 17:43:06 +08:00
parent c3dc3fa4e5
commit c0fa5c3a49
3 files changed
+56 -13

No files matched your search

+15 -11
View File
@@ -13,6 +13,7 @@ import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand
import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink
import cn.rtast.fancybot.commands.parse.*
import cn.rtast.fancybot.commands.reply.ImageBedCommand
import cn.rtast.fancybot.commands.reply.SpeedUpGIFCommand
import cn.rtast.fancybot.entity.nailong.NaiLongDetectPayload
import cn.rtast.fancybot.entity.nailong.NaiLongDetectResponse
import cn.rtast.fancybot.enums.WSType
@@ -61,17 +62,6 @@ class FancyBot : OneBotListener {
logger.info("$sender($senderId: $groupId >>> $messageId): $msg")
logger.trace("$sender($senderId: $groupId >>> $messageId: $json")
if (message.images.isNotEmpty()) {
message.images.forEach {
val payload = NaiLongDetectPayload(it.file).toJson()
val result = Http.post<NaiLongDetectResponse>(configManager.naiLongApiUrl, payload)
logger.info("奶龙识别结果: ${result.result}")
if (result.result) {
message.reply("本群禁止发奶龙!")
}
}
}
if (message.message.any { it.type == ArrayMessageType.face && it.data.id.toString() == "419" }) {
message.reply("你发牛魔的火车呢, 我直接就是打断")
}
@@ -104,6 +94,10 @@ class FancyBot : OneBotListener {
val plainTextContent = getMsg.message
.filter { it.type == ArrayMessageType.text }
.joinToString { it.data.text!! }
if (command.contains("加速")) {
val multiply = command.split("加速").last().toFloat()
SpeedUpGIFCommand.speedUp(message, getMsg, multiply)
}
if (command.contains("倒放") || command.contains("/df")) {
// 使用回复消息的方式对一个gif倒放
ReverseGIFCommand.reverse(message, getMsg)
@@ -138,6 +132,16 @@ class FancyBot : OneBotListener {
}
coroutineScope.launch {
if (message.images.isNotEmpty()) {
message.images.forEach {
val payload = NaiLongDetectPayload(it.file).toJson()
val result = Http.post<NaiLongDetectResponse>(configManager.naiLongApiUrl, payload)
logger.info("奶龙识别结果: ${result.result}")
if (result.result) {
message.reply("本群禁止发奶龙!")
}
}
}
message.message.forEach {
if (it.type == ArrayMessageType.image) {
val filename = it.data.file!!.split("=").last() + ".png"
@@ -0,0 +1,33 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/27
*/
package cn.rtast.fancybot.commands.reply
import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink
import cn.rtast.fancybot.commands.parse.AsciiArtCommand
import cn.rtast.fancybot.util.misc.ImageBed
import cn.rtast.fancybot.util.misc.makeGif
import cn.rtast.fancybot.util.misc.toURL
import cn.rtast.rob.entity.GetMessage
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.ob.MessageChain
import com.madgag.gif.fmsware.GifDecoder
object SpeedUpGIFCommand {
suspend fun speedUp(message: GroupMessage, getMsg: GetMessage.Message, multiply: Float) {
val gifUrl = AsciiArtCommand.getImageUrl(getMsg)
val gifStream = gifUrl.toURL().openStream()
val decoder = GifDecoder()
decoder.read(gifStream)
val frames = (0 until decoder.frameCount).map { decoder.getFrame(it) }.reversed()
val gifBase64 = decoder.makeGif(frames, multiply)
val imgBedUrl = ImageBed.upload(gifBase64)
val shortLink = imgBedUrl.makeShortLink()
val msg = MessageChain.Builder().addImage(imgBedUrl).addText(shortLink).build()
message.reply(msg)
}
}
@@ -12,7 +12,7 @@ import com.madgag.gif.fmsware.GifDecoder
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
fun GifDecoder.makeGif(frames: List<BufferedImage>): ByteArray {
fun GifDecoder.makeGif(frames: List<BufferedImage>, speed: Float? = null): ByteArray {
val estimatedSize = frames.size * 50 * 1024
val byteArrayOutputStream = ByteArrayOutputStream(estimatedSize)
val encoder = AnimatedGifEncoder()
@@ -20,7 +20,13 @@ fun GifDecoder.makeGif(frames: List<BufferedImage>): ByteArray {
encoder.setRepeat(0)
val delays = frames.indices.map { this.getDelay(this.frameCount - it - 1) }
frames.indices.forEach { i ->
if (speed == null) {
encoder.setDelay(delays[i])
} else {
var delay = this.getDelay(i)
delay = (delay / speed).toInt()
encoder.setDelay(delay.coerceAtLeast(1))
}
encoder.addFrame(frames[i])
}
encoder.finish()