enable image muxing

This commit is contained in:
2022-02-26 04:08:18 +05:30
parent d5343202aa
commit d0753bda64
4 changed files with 231 additions and 7 deletions

View File

@@ -8,6 +8,8 @@ use serenity::{
use std::{fs::remove_file, path::Path, process::ExitStatus};
use tokio::fs::File;
pub const IMAGE_EXTS: [&'static str; 6] = ["png", "gif", "bmp", "webp", "jpg", "jpeg"];
#[command]
#[aliases("mux")]
pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
@@ -23,7 +25,25 @@ pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let mut trans = FfmpegTranscode::default();
trans.add_input(q[0]).add_input(q[1]);
trans.add_input(q[0]);
let mut ext = Path::new(&q[1]).extension().unwrap().to_str().unwrap();
if IMAGE_EXTS.contains(&ext) {
let resp = reqwest::get(q[1]).await?;
let p = format!("/tmp/{}{}-image.{}", "singh4-", msg.id, ext);
let mut f = File::create(&p).await?;
tokio::io::copy(&mut resp.bytes().await?.as_ref(), &mut f).await?;
trans
.add_arg("loop", 1u32)
.add_input(&p)
.set_vcodec("h264")
.add_arg("vf", "pad=ceil(iw/2)*2:ceil(ih/2)*2") //to correct the dimensions
.add_arg("tune", "stillimage");
ext = "mp4";
} else {
trans.add_input(q[1]);
}
if q.len() > 2 {
trans.set_acodec(q[2]);
@@ -33,20 +53,19 @@ pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
trans.set_vcodec(q[3]);
}
let ext = Path::new(&q[1]).extension().unwrap().to_str().unwrap();
let output = format!("/tmp/{}{}.{}", "singh4-", msg.id, ext);
trans
.add_flag("shortest")
.set_output(&output)
.add_arg("map", "0:a:0")
.add_arg("map", "1:v:0");
.add_map(0, 'a', 0)
.add_map(1, 'v', 0);
let mut m = msg.reply_ping(ctx, "Working").await?;
let exit_code: ExitStatus = trans.run();
let file: File = File::open(&output).await?;
if !exit_code.success() {
if exit_code.code().unwrap() != 0 {
msg.reply(ctx, "Some error occurred, please check the inputs")
.await?;
} else if file.metadata().await.unwrap().len() > 8 * 1024 * 1024 {
@@ -62,9 +81,8 @@ pub async fn remux(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
.content(msg.author.mention())
})
.await?;
m.delete(ctx).await?;
}
m.delete(ctx).await?;
remove_file(output)?;
Ok(())

View File

@@ -73,6 +73,7 @@ impl FfmpegTranscode {
}
pub fn run(&self) -> ExitStatus {
println!("{}", self.args.join(" "));
Command::new("ffmpeg")
.args(&self.args)
.args(vec!["-c:a", &self.acodec])