修复合成视频时二维码比例及坐标错误问题,添加plus控件大小动态调试测试代码

This commit is contained in:
阿甘 2024-08-29 00:18:19 +08:00
parent e15f240735
commit 1e8747f557
3 changed files with 225 additions and 28 deletions

View File

@ -1,8 +1,8 @@
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=304;bottom=297;bgcolor=16777215;exmode="none";max=false;min=false;mode="popup";sysmenu=false;title=false)
var winform = win.form(text="aardio form";right=304;bottom=297;bgcolor=16777215;border="none";exmode="none";max=false;min=false;mode="popup";sysmenu=false;title=false)
winform.add(
plus={cls="plus";left=0;top=0;right=167;bottom=159;clipBk=false;dl=1;dt=1;repeat="scale";z=1}
plus={cls="plus";left=0;top=0;right=271;bottom=256;clipBk=false;dl=1;dt=1;repeat="scale";z=1}
)
/*}}*/

View File

@ -1,14 +1,190 @@
import win.ui;
import mouse;
/*DSG{{*/
var winform = win.form(text="aardio form";right=735;bottom=618;bgcolor=16777215;max=false;min=false;mode="popup";sysmenu=false;title=false)
var winform = win.form(text="aardio form";right=759;bottom=469;composited=1;mode="popup")
winform.add(
button={cls="button";text="Button";left=230;top=360;right=332;bottom=413;z=2};
plus={cls="plus";left=0;top=0;right=167;bottom=159;clipBk=false;dl=1;dt=1;repeat="scale";z=1}
button={cls="button";text="向上拖动";left=538;top=138;right=673;bottom=199;z=2};
button2={cls="button";text="向下拖动";left=539;top=239;right=674;bottom=300;z=3};
plus={cls="plus";left=266;top=117;right=466;bottom=317;background="C:\Users\97975\Desktop\视频增加二维码\1_i2skbfmDsHayHhqPfwt6pA.png";border={color=-65536;width=3};clipBk=false;foreRepeat="stretch";notify=1;z=1}
)
/*}}*/
import console;
// 定义边框宽度
var borderWidth = 5;
var isDragging = false;
var startX, startY;
var dragType = ""; // 记录拖动的类型:"top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"
// 重写plus控件的窗口过程函数
winform.plus.wndproc = function(hwnd, message, wParam, lParam){
var x, y = win.getMessagePos(lParam);
if (message == 0x201) { // WM_LBUTTONDOWN
// 获取plus控件的位置和大小
var rect = winform.plus.getClientRect();
winform.text = "Dragging: x=" + x + ", y=" + y;
console.dump(rect.left, rect.top, rect.right, rect.bottom);
// 上边框
if (x > rect.left + borderWidth && x < rect.right - borderWidth && y >= rect.top && y < rect.top + borderWidth) {
console.dump("当前点击了上边框");
isDragging = true;
dragType = "top";
startX = x;
startY = y;
}
// 下边框
else if (x > rect.left + borderWidth && x < rect.right - borderWidth && y > rect.bottom - borderWidth && y <= rect.bottom) {
console.dump("当前点击了下边框");
isDragging = true;
dragType = "bottom";
startX = x;
startY = y;
}
// 左边框
else if (x >= rect.left && x < rect.left + borderWidth && y > rect.top + borderWidth && y < rect.bottom - borderWidth) {
console.dump("当前点击了左边框");
isDragging = true;
dragType = "left";
startX = x;
startY = y;
}
// 右边框
else if (x > rect.right - borderWidth && x <= rect.right && y > rect.top + borderWidth && y < rect.bottom - borderWidth) {
console.dump("当前点击了右边框");
isDragging = true;
dragType = "right";
startX = x;
startY = y;
}
// 左上角
else if (x >= rect.left && x < rect.left + borderWidth && y >= rect.top && y < rect.top + borderWidth) {
console.dump("当前点击了左上角");
isDragging = true;
dragType = "top-left";
startX = x;
startY = y;
}
// 右上角
else if (x > rect.right - borderWidth && x <= rect.right && y >= rect.top && y < rect.top + borderWidth) {
console.dump("当前点击了右上角");
isDragging = true;
dragType = "top-right";
startX = x;
startY = y;
}
// 左下角
else if (x >= rect.left && x < rect.left + borderWidth && y > rect.bottom - borderWidth && y <= rect.bottom) {
console.dump("当前点击了左下角");
isDragging = true;
dragType = "bottom-left";
startX = x;
startY = y;
}
// 右下角
else if (x > rect.right - borderWidth && x <= rect.right && y > rect.bottom - borderWidth && y <= rect.bottom) {
console.dump("当前点击了右下角");
isDragging = true;
dragType = "bottom-right";
startX = x;
startY = y;
}
} else if (message == 0x200) { // WM_MOUSEMOVE
if (isDragging) {
if (dragType == "top") {
var deltaY = y - startY;
winform.text = "Dragging top: deltaY=" + deltaY;
// 处理上边框拖动
if(startY == -1){
winform.plus.top -= 1;
}
else {
winform.plus.top += 1;
}
startY = y;
} else if (dragType == "bottom") {
var deltaY = y - startY;
winform.text = "Dragging bottom: deltaY=" + deltaY;
// 处理下边框拖动
winform.plus.height = winform.plus.height + deltaY;
startY = y;
} else if (dragType == "left") {
var deltaX = x - startX;
winform.text = "Dragging left: deltaX=" + deltaX;
// 处理左边框拖动
winform.plus.left = winform.plus.left + deltaX;
winform.plus.width = winform.plus.width - deltaX;
startX = x;
} else if (dragType == "right") {
var deltaX = x - startX;
winform.text = "Dragging right: deltaX=" + deltaX;
// 处理右边框拖动
winform.plus.width = winform.plus.width + deltaX;
startX = x;
} else if (dragType == "top-left") {
var deltaX = x - startX;
var deltaY = y - startY;
winform.text = "Dragging top-left: deltaX=" + deltaX + ", deltaY=" + deltaY;
// 处理左上角拖动
winform.plus.left = winform.plus.left + deltaX;
winform.plus.width = winform.plus.width - deltaX;
winform.plus.top = winform.plus.top + deltaY;
winform.plus.height = winform.plus.height - deltaY;
startX = x;
startY = y;
} else if (dragType == "top-right") {
var deltaX = x - startX;
var deltaY = y - startY;
winform.text = "Dragging top-right: deltaX=" + deltaX + ", deltaY=" + deltaY;
// 处理右上角拖动
winform.plus.width = winform.plus.width + deltaX;
winform.plus.top = winform.plus.top + deltaY;
winform.plus.height = winform.plus.height - deltaY;
startX = x;
startY = y;
} else if (dragType == "bottom-left") {
var deltaX = x - startX;
var deltaY = y - startY;
winform.text = "Dragging bottom-left: deltaX=" + deltaX + ", deltaY=" + deltaY;
// 处理左下角拖动
winform.plus.left = winform.plus.left + deltaX;
winform.plus.width = winform.plus.width - deltaX;
winform.plus.height = winform.plus.height + deltaY;
startX = x;
startY = y;
} else if (dragType == "bottom-right") {
var deltaX = x - startX;
var deltaY = y - startY;
winform.text = "Dragging bottom-right: deltaX=" + deltaX + ", deltaY=" + deltaY;
// 处理右下角拖动
winform.plus.width = winform.plus.width + deltaX;
winform.plus.height = winform.plus.height + deltaY;
startX = x;
startY = y;
}
}
} else if (message == 0x202) { // WM_LBUTTONUP
if (isDragging) {
isDragging = false;
dragType = "";
winform.text = "Dragging ended";
}
}
// 无返回值则继续调用默认回调函数
}
// 向上拖动,图片增高,上横线上移动
winform.button.oncommand = function(id,event){
//winform.plus.height += 1;
winform.plus.top -= 1;
}
winform.button2.oncommand = function(id,event){
winform.plus.top += 1;
}
winform.show();
win.loopMessage();

View File

@ -46,7 +46,6 @@ 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;
@ -88,22 +87,18 @@ mainForm.plus.oncommand = function(id,event){
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)){
fsys.delete(vpath);
}
io.createDir(vpath)
if(io.exist(vpath+"0001.png")){
//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);
}
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);
}
}
}
}
@ -112,18 +107,34 @@ mainForm.plus2.oncommand = function(id,event){
var path = fsys.dlg.open("*.png|*.png|其他格式 (*.*)|*||"); // 假设只支持png格式
if(path){
qrcodePath = path;
var customW = mainForm.custom.width;
var customH = mainForm.custom.height;
var img = gdip.image(path);
var imgW = img.width;
var imgH = img.height;
// 使用保存的缩放比例来计算新图片的宽度和高度
var newImgW = imgW * scaleRatio;
var newImgH = imgH * scaleRatio;
if(imgW > customW or imgH > customW){
var aspectRatio = imgW / imgH;
var customAspectRatio = customW / customH;
var newImgW, newImgH;
if (aspectRatio > customAspectRatio) {
newImgW = customW / 2;
newImgH = customW / 2 / aspectRatio;
} else {
newImgH = customH / 2;
newImgW = customH / 2 * aspectRatio;
}
imgW = newImgW;
imgH = newImgH;
}
if(frmChild){
frmChild.close();
}
frmChild = mainForm.loadForm("\dlg\qrcode.aardio");
publish("getQrcode", path, newImgW, newImgH);
publish("getQrcode", path, imgW, imgH);
frmChild.show();
}
}
@ -132,7 +143,7 @@ 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 vTop = vRect.top;
var iLeft = frmChild.left;
var iTop = frmChild.top;
var x = (iLeft - vLeft) / scaleRatio;
@ -141,15 +152,25 @@ mainForm.plus3.oncommand = function(id,event){
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 二维码裁剪后宽度 = frmChild.width / scaleRatio;
var 二维码裁剪后高度 = frmChild.height / scaleRatio;
var qrcodePath_new = qrpath.dir ++ qrpath.name ++ "-output" ++ qrpath.ext;
var ffmpeg = process.ffmpeg(vpath.dir, str);
// ffmpeg -i input.jpg -vf "scale=width:height" output.jpg
var ffmpeg = process.ffmpeg(, `-i `++ qrcodePath ++` -vf "scale=`++ 二维码裁剪后宽度 ++`:`++ 二维码裁剪后高度 ++`" `++qrcodePath_new);
ffmpeg.logResponse(mainForm.edit);
ffmpeg.onResponseEnd = function(){
mainForm.edit.print("完成。");
mainForm.msgbox("处理完成,在原视频目录下查看 -output后缀")
// ffmpeg -i input.mp4 -i overlay.png -filter_complex "overlay=10:10" output.mp4
var str = `-i `++ videoPath ++` -i `++ qrcodePath_new ++` -filter_complex "overlay=`++ x ++`:`++ y ++ `" ` ++ vpath.dir ++ vpath.name ++`-output`++vpath.ext;
var ffmpeg = process.ffmpeg(, str);
ffmpeg.logResponse(mainForm.edit);
ffmpeg.onResponseEnd = function(){
mainForm.edit.print(vpath.dir, str);
mainForm.edit.print("完成。");
mainForm.msgbox("处理完成,在原视频目录下查看 -output后缀")
}
}
}
mainForm.show();
return win.loopMessage();
return win.loopMessage();