VideoWatermark/main.aardio

156 lines
4.7 KiB
Plaintext
Raw Normal View History

2024-08-27 23:11:10 +08:00
import win.ui;
/*DSG{{*/
mainForm = win.form(text="视频添加二维码";right=519;bottom=759;bgcolor=16777215)
mainForm.add(
custom={cls="custom";text="自定义控件";left=10;top=76;right=510;bottom=668;border=1;clip=1;db=1;dl=1;dr=1;dt=1;z=4};
edit={cls="edit";left=10;top=673;right=510;bottom=750;db=1;dl=1;dr=1;edge=1;multiline=1;readonly=1;z=5};
plus={cls="plus";text="导入视频";left=63;top=28;right=162;bottom=58;bgcolor=-5197169;border={color=-16777216;radius=3;width=1};dl=0.12;dr=0.69;dt=1;font=LOGFONT(h=-13);forecolor=16777215;z=1};
plus2={cls="plus";text="导入二维码";left=179;top=28;right=278;bottom=58;bgcolor=-5197169;border={color=-16777216;radius=3;width=1};dl=0.34;dr=0.47;dt=1;font=LOGFONT(h=-13);forecolor=16777215;notify=1;z=2};
plus3={cls="plus";text="保存格式";left=337;top=28;right=436;bottom=58;bgcolor=-5197169;border={color=-16777216;radius=3;width=1};dl=0.65;dr=0.16;dt=1;font=LOGFONT(h=-13);forecolor=5881621;notify=1;z=3}
)
/*}}*/
mainForm.plus.skin({
foreground={
active=0xFFFFBAB7;
default=0xFFFFFFFF
};
})
mainForm.plus2.skin({
foreground={
active=0xFFFFBAB7;
default=0xFFFFFFFF
};
})
mainForm.plus3.skin({
foreground={
active=0xFFFFBAB7;
default=0xFF15BF59
};
})
import console;
import gdip;
import fsys.dlg;
import process.ffmpeg;
var frmChild; // qrcode winform
var videoPath;
var qrcodePath;
var scaleRatio; // 定义一个全局变量来保存缩放比例
var offsetX; // 视频在 custom 内 x 偏移量
var offsetY; // 视频在 custom 内 y 偏移量
showVideo = function(vpath){
var customW = mainForm.custom.width;
var customH = mainForm.custom.height;
mainForm.edit.print(vpath);
var img = gdip.image(vpath + "0001.png");
var imgW = img.width;
var imgH = img.height;
var aspectRatio = imgW / imgH;
var customAspectRatio = customW / customH;
var newImgW, newImgH;
if (aspectRatio > customAspectRatio) {
newImgW = customW;
newImgH = customW / aspectRatio;
scaleRatio = customW / imgW; // 保存缩放比例
} else {
newImgH = customH;
newImgW = customH * aspectRatio;
scaleRatio = customH / imgH; // 保存缩放比例
}
offsetX = (customW - newImgW) / 2;
offsetY = (customH - newImgH) / 2;
mainForm.custom.add(
plus4 = {
cls = "plus";
left = offsetX;
top = offsetY;
right = offsetX + newImgW;
bottom = offsetY + newImgH;
background = vpath + "0001.png";
z = 6
}
);
}
mainForm.plus.oncommand = function(id,event){
var path = fsys.dlg.open("MP4 files (*.mp4)|*.mp4|其他格式 (*.*)|*||"); // 假设只支持mp4格式
if(path){
videoPath = path;
var tpath = io.splitpath(path);
var vpath = io._exedir+"temp\"+tpath.name+"\";
if(!io.exist(vpath)){
io.createDir(vpath)
}
if(io.exist(vpath+"0001.png")){
showVideo(vpath);
}
else {
//var ffmpeg = process.ffmpeg(,"-i "+path+" -vf fps=1 "+vpath+"\%04d.png");
var ffmpeg = process.ffmpeg(, `-i `++path++` -vf "select=eq(n\,0)" -vframes 1 `++vpath++`\0001.png`);
ffmpeg.logResponse(mainForm.edit);
ffmpeg.onResponseEnd = function(){
showVideo(vpath);
}
}
}
}
mainForm.plus2.oncommand = function(id,event){
// 导入二维码
var path = fsys.dlg.open("*.png|*.png|其他格式 (*.*)|*||"); // 假设只支持png格式
if(path){
qrcodePath = path;
var img = gdip.image(path);
var imgW = img.width;
var imgH = img.height;
// 使用保存的缩放比例来计算新图片的宽度和高度
var newImgW = imgW * scaleRatio;
var newImgH = imgH * scaleRatio;
if(frmChild){
frmChild.close();
}
frmChild = mainForm.loadForm("\dlg\qrcode.aardio");
publish("getQrcode", path, newImgW, newImgH);
frmChild.show();
}
}
mainForm.plus3.oncommand = function(id,event){
var rc = mainForm.custom.plus4.getRect();
var vRect = win.toScreenRect(mainForm.custom.plus4.hwnd,rc);
var vLeft = vRect.left - offsetX;
var vTop = vRect.top - offsetY;
var iLeft = frmChild.left;
var iTop = frmChild.top;
var x = (iLeft - vLeft) / scaleRatio;
var y = (iTop - vTop) / scaleRatio;
var vpath = io.splitpath(videoPath);
var qrpath = io.splitpath(qrcodePath);
var str = `-i `++ vpath.file ++` -i `++ qrpath.file ++` -filter_complex "overlay=`++ x ++`:`++ y ++ `" ` ++ vpath.name ++`-output.mp4`;
var ffmpeg = process.ffmpeg(vpath.dir, str);
ffmpeg.logResponse(mainForm.edit);
ffmpeg.onResponseEnd = function(){
mainForm.edit.print("完成。");
mainForm.msgbox("处理完成,在原视频目录下查看 -output后缀")
}
}
mainForm.show();
return win.loopMessage();