-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/com/github/djaler/evilbot/handlers/VideoConverterHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.github.djaler.evilbot.handlers | ||
|
||
import com.github.djaler.evilbot.handlers.base.MessageHandler | ||
import com.github.djaler.evilbot.service.VideoConvertService | ||
import com.github.djaler.evilbot.utils.toTemporaryFile | ||
import dev.inmo.tgbotapi.bot.RequestsExecutor | ||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile | ||
import dev.inmo.tgbotapi.extensions.api.send.media.sendVideo | ||
import dev.inmo.tgbotapi.extensions.api.send.reply | ||
import dev.inmo.tgbotapi.extensions.api.send.withUploadVideoAction | ||
import dev.inmo.tgbotapi.extensions.utils.asContentMessage | ||
import dev.inmo.tgbotapi.extensions.utils.asDocumentContent | ||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile | ||
import dev.inmo.tgbotapi.types.message.abstracts.Message | ||
import org.springframework.stereotype.Component | ||
import java.io.File | ||
import java.util.* | ||
import kotlin.io.path.createTempDirectory | ||
|
||
@Component | ||
class VideoConverterHandler( | ||
private val requestExecutor: RequestsExecutor, | ||
private val videoConvertService: VideoConvertService, | ||
) : MessageHandler() { | ||
override suspend fun handleMessage(message: Message): Boolean { | ||
val document = message.asContentMessage()?.content?.asDocumentContent() ?: return false | ||
val mimeType = document.media.mimeType ?: return false | ||
if (mimeType.primaryType != "video") { | ||
return false | ||
} | ||
if (mimeType.subType == "mp4") { | ||
return false | ||
} | ||
|
||
val videoBytes = requestExecutor.downloadFile(document) | ||
val mp4File = convertToMp4(videoBytes, document.media.fileName ?: "video") | ||
|
||
try { | ||
requestExecutor.withUploadVideoAction(message.chat) { | ||
requestExecutor.sendVideo(message.chat, video = mp4File.asMultipartFile()) | ||
} | ||
requestExecutor.reply(message, text = "Вот тебе mp4, не благодари") | ||
} finally { | ||
mp4File.delete() | ||
} | ||
|
||
return true | ||
} | ||
|
||
private fun convertToMp4(bytes: ByteArray, fileName: String): File { | ||
val directory = createTempDirectory(UUID.randomUUID().toString()).toFile() | ||
|
||
bytes.toTemporaryFile(directory, fileName).use { inputFile -> | ||
val outputFile = File(directory, inputFile.nameWithoutExtension + ".mp4") | ||
videoConvertService.convertToMp4(inputFile, outputFile) | ||
return outputFile | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/github/djaler/evilbot/service/VideoConvertService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.djaler.evilbot.service | ||
|
||
import org.springframework.stereotype.Service | ||
import ws.schild.jave.Encoder | ||
import ws.schild.jave.MultimediaObject | ||
import ws.schild.jave.encode.AudioAttributes | ||
import ws.schild.jave.encode.EncodingAttributes | ||
import ws.schild.jave.encode.VideoAttributes | ||
import java.io.File | ||
|
||
@Service | ||
class VideoConvertService { | ||
fun convertToMp4(input: File, output: File) { | ||
val audioAttributes = AudioAttributes() | ||
val videoAttributes = VideoAttributes() | ||
val encodingAttributes = EncodingAttributes().apply { | ||
setAudioAttributes(audioAttributes) | ||
setVideoAttributes(videoAttributes) | ||
setOutputFormat("mp4") | ||
} | ||
|
||
val encoder = Encoder() | ||
encoder.encode(MultimediaObject(input), output, encodingAttributes) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters