格式化代码

This commit is contained in:
一二三 2024-09-02 20:17:40 +08:00
parent 76e8d1a9cf
commit cb05d881d1

View File

@ -1,34 +1,34 @@
import win.ui; import win.ui;
/*DSG{{*/ /*DSG{{*/
mainForm = win.form(text="视频添加二维码";right=519;bottom=759;bgcolor=16777215) mainForm = win.form(text="视频添加二维码"; right=519; bottom=759; bgcolor=16777215)
mainForm.add( 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}; 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;vscroll=1;z=5}; edit={cls="edit"; left=10; top=673; right=510; bottom=750; db=1; dl=1; dr=1; edge=1; multiline=1; readonly=1; vscroll=1; z=5};
plus={cls="plus";text="导入视频";left=14;top=28;right=113;bottom=58;bgcolor=-5197169;border={color=-16777216;radius=3;width=1};dl=0.03;dr=0.78;dt=1;font=LOGFONT(h=-13);forecolor=16777215;z=1}; importButton={cls="plus"; text="导入视频"; left=14; top=28; right=113; bottom=58; bgcolor=-5197169; border={color=-16777216; radius=3; width=1}; dl=0.03; dr=0.78; dt=1; font=LOGFONT(h=-13); forecolor=16777215; z=1};
plus2={cls="plus";text="导入二维码";left=130;top=28;right=229;bottom=58;bgcolor=-5197169;border={color=-16777216;radius=3;width=1};dl=0.25;dr=0.56;dt=1;font=LOGFONT(h=-13);forecolor=16777215;notify=1;z=2}; qrcodeButton={cls="plus"; text="导入二维码"; left=130; top=28; right=229; bottom=58; bgcolor=-5197169; border={color=-16777216; radius=3; width=1}; dl=0.25; dr=0.56; dt=1; font=LOGFONT(h=-13); forecolor=16777215; notify=1; z=2};
plus3={cls="plus";text="保存格式";left=408;top=28;right=507;bottom=58;bgcolor=-5197169;border={color=-16777216;radius=3;width=1};dl=0.78;dr=0.03;dt=1;font=LOGFONT(h=-13);forecolor=5881621;notify=1;z=3} saveButton={cls="plus"; text="保存格式"; left=408; top=28; right=507; bottom=58; bgcolor=-5197169; border={color=-16777216; radius=3; width=1}; dl=0.78; dr=0.03; dt=1; font=LOGFONT(h=-13); forecolor=5881621; notify=1; z=3}
) )
/*}}*/ /*}}*/
mainForm.plus.skin({ mainForm.importButton.skin({
foreground={ foreground={
active=0xFFFFBAB7; active=0xFFFFBAB7;
default=0xFFFFFFFF default=0xFFFFFFFF
}; };
}) })
mainForm.plus2.skin({ mainForm.qrcodeButton.skin({
foreground={ foreground={
active=0xFFFFBAB7; active=0xFFFFBAB7;
default=0xFFFFFFFF default=0xFFFFFFFF
}; };
}) })
mainForm.plus3.skin({ mainForm.saveButton.skin({
foreground={ foreground={
active=0xFFFFBAB7; active=0xFFFFBAB7;
default=0xFF15BF59 default=0xFF15BF59
}; };
}) })
import console; import console;
@ -36,175 +36,168 @@ import gdip;
import fsys.dlg; import fsys.dlg;
import process.ffmpeg; import process.ffmpeg;
var frmChild; // qrcode winform var qrcodeForm; // qrcode winform
var videoPath; var videoPath;
var qrcodePath; var qrcodePath;
var scaleRatio; // 定义一个全局变量来保存缩放比例 var scaleRatio; // 定义一个全局变量来保存缩放比例
var offsetX; // 视频在 custom 内 x 偏移量 var offsetX; // 视频在 custom 内 x 偏移量
var offsetY; // 视频在 custom 内 y 偏移量 var offsetY; // 视频在 custom 内 y 偏移量
showVideo = function(vpath){ showVideo = function(videoDir){
var customW = mainForm.custom.width; var customWidth = mainForm.custom.width;
var customH = mainForm.custom.height; var customHeight = mainForm.custom.height;
var img = gdip.image(vpath + "0001.png"); var img = gdip.image(videoDir + "0001.png");
var imgW = img.width; var imgWidth = img.width;
var imgH = img.height; var imgHeight = img.height;
var aspectRatio = imgW / imgH; var aspectRatio = imgWidth / imgHeight;
var customAspectRatio = customW / customH; var customAspectRatio = customWidth / customHeight;
var newImgW, newImgH; var newImgWidth, newImgHeight;
if (aspectRatio > customAspectRatio) { if (aspectRatio > customAspectRatio) {
newImgW = customW; newImgWidth = customWidth;
newImgH = customW / aspectRatio; newImgHeight = customWidth / aspectRatio;
scaleRatio = customW / imgW; // 保存缩放比例 scaleRatio = customWidth / imgWidth; // 保存缩放比例
} else { } else {
newImgH = customH; newImgHeight = customHeight;
newImgW = customH * aspectRatio; newImgWidth = customHeight * aspectRatio;
scaleRatio = customH / imgH; // 保存缩放比例 scaleRatio = customHeight / imgHeight; // 保存缩放比例
} }
offsetX = (customW - newImgW) / 2; offsetX = (customWidth - newImgWidth) / 2;
offsetY = (customH - newImgH) / 2; offsetY = (customHeight - newImgHeight) / 2;
mainForm.custom.add( mainForm.custom.add(
plus4 = { videoDisplay = {
cls = "plus"; cls = "plus";
left = offsetX; left = offsetX;
top = offsetY; top = offsetY;
right = offsetX + newImgW; right = offsetX + newImgWidth;
bottom = offsetY + newImgH; bottom = offsetY + newImgHeight;
background = vpath + "0001.png"; background = videoDir + "0001.png";
z = 6 z = 6
} }
); );
} }
mainForm.plus.oncommand = function(id,event){ mainForm.importButton.oncommand = function(id,event){
var vpath = fsys.dlg.open("MP4 files (*.mp4)|*.mp4|其他格式 (*.*)|*||"); // 假设只支持mp4格式 var videoFilePath = fsys.dlg.open("MP4 files (*.mp4)|*.mp4|其他格式 (*.*)|*||");
if(vpath){ if(videoFilePath){
var path_sp = io.splitpath(vpath); var videoFileDetails = io.splitpath(videoFilePath);
var temp_dir = io._exedir+"temp\"+path_sp.name+"\"; var tempDir = io._exedir+"temp\"+videoFileDetails.name+"\";
var temp_path = temp_dir + path_sp.file; var tempFilePath = tempDir + videoFileDetails.file;
if(!io.exist(temp_dir)){ if(!io.exist(tempDir)){
io.createDir(temp_dir) io.createDir(tempDir)
} }
fsys.copy(vpath, temp_path); fsys.copy(videoFilePath, tempFilePath);
videoPath = temp_path; videoPath = tempFilePath;
//var ffmpeg = process.ffmpeg(,"-i "+path+" -vf fps=1 "+vpath+"\%04d.png"); var ffmpegCommand = `-i `++tempFilePath++` -vf "select=eq(n\,0)" -vframes 1 -y `++tempDir++`0001.png`;
var str = `-i `++temp_path++` -vf "select=eq(n\,0)" -vframes 1 -y `++temp_dir++`0001.png`; mainForm.edit.print("ffmpeg command: ", ffmpegCommand);
mainForm.edit.print("ffmpeg str: ", str); var ffmpegProcess = process.ffmpeg(, ffmpegCommand);
var ffmpeg = process.ffmpeg(, str); ffmpegProcess.logResponse(mainForm.edit);
ffmpeg.logResponse(mainForm.edit);
ffmpeg.onResponseEnd = function(){ ffmpegProcess.onResponseEnd = function(){
showVideo(temp_dir); showVideo(tempDir);
} }
} }
} }
mainForm.plus2.oncommand = function(id,event){ mainForm.qrcodeButton.oncommand = function(id,event){
// 导入二维码 var qrcodeFilePath = fsys.dlg.open("*.png|*.png|其他格式 (*.*)|*||");
var path = fsys.dlg.open("*.png|*.png|其他格式 (*.*)|*||"); // 假设只支持png格式 if(qrcodeFilePath){
if(path){ var qrcodeFileDetails = io.splitpath(qrcodeFilePath);
var path_sp = io.splitpath(path); var tempDir = io._exedir+"temp\"+qrcodeFileDetails.name+"\";
var temp_dir = io._exedir+"temp\"+path_sp.name+"\"; var tempFilePath = tempDir + qrcodeFileDetails.file;
var temp_path = temp_dir + path_sp.file; if(!io.exist(tempDir)){
if(!io.exist(temp_dir)){ io.createDir(tempDir)
io.createDir(temp_dir) }
fsys.copy(qrcodeFilePath, tempFilePath);
qrcodePath = tempFilePath;
var customWidth = mainForm.custom.width;
var customHeight = mainForm.custom.height;
var img = gdip.image(qrcodeFilePath);
var imgWidth = img.width;
var imgHeight = img.height;
if(imgWidth > customWidth or imgHeight > customWidth){
var aspectRatio = imgWidth / imgHeight;
var customAspectRatio = customWidth / customHeight;
var newImgWidth, newImgHeight;
if (aspectRatio > customAspectRatio) {
newImgWidth = customWidth / 2;
newImgHeight = customWidth / 2 / aspectRatio;
} else {
newImgHeight = customHeight / 2;
newImgWidth = customHeight / 2 * aspectRatio;
}
imgWidth = newImgWidth;
imgHeight = newImgHeight;
}
if(qrcodeForm){
qrcodeForm.close();
}
qrcodeForm = mainForm.loadForm("\dlg\qrcode.aardio");
publish("getQrcode", qrcodeFilePath, imgWidth, imgHeight);
qrcodeForm.show();
qrcodeForm.wndproc = function(hwnd,message,wParam,lParam){
import mouse
select(message) {
case 0x201/*_WM_LBUTTONDOWN*/{
//点击左键移动窗体
qrcodeForm.hitCaption()
}
case 0x46/*_WM_WINDOWPOSCHANGING*/{
// 窗口位置即将改变时
}
case 0x47/*_WM_WINDOWPOSCHANGED*/{
// 窗口位置已经改变时
}
case 0x232/*_WM_EXITSIZEMOVE*/{
// 用户停止拖动窗口或调整窗口大小时
}
}
} }
fsys.copy(path, temp_path);
qrcodePath = temp_path;
var customW = mainForm.custom.width;
var customH = mainForm.custom.height;
var img = gdip.image(path);
var imgW = img.width;
var imgH = img.height;
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, imgW, imgH);
frmChild.show();
frmChild.wndproc = function(hwnd,message,wParam,lParam){
import mouse
select(message) {
case 0x201/*_WM_LBUTTONDOWN*/{
//点击左键移动窗体
frmChild.hitCaption()
}
case 0x46/*_WM_WINDOWPOSCHANGING*/{
// 窗口位置即将改变时
//var x,y = win.getMessagePos(lParam);
//x, y = mouse.getPos();
//mainForm.text = x ++ "," ++ y;
}
case 0x47/*_WM_WINDOWPOSCHANGED*/{
// 窗口位置已经改变时
//x, y = mouse.getPos();
//mainForm.text = x ++ "---" ++ y;
}
case 0x232/*_WM_EXITSIZEMOVE*/{
// 用户停止拖动窗口或调整窗口大小时
}
}
}
} }
} }
mainForm.plus3.oncommand = function(id,event){ mainForm.saveButton.oncommand = function(id,event){
var rc = mainForm.custom.plus4.getRect(); var videoRect = mainForm.custom.videoDisplay.getRect();
var vRect = win.toScreenRect(mainForm.custom.plus4.hwnd,rc); var videoScreenRect = win.toScreenRect(mainForm.custom.videoDisplay.hwnd, videoRect);
var vLeft = vRect.left - offsetX; var videoLeft = videoScreenRect.left - offsetX;
var vTop = vRect.top; var videoTop = videoScreenRect.top;
var iLeft = frmChild.left; var qrcodeLeft = qrcodeForm.left;
var iTop = frmChild.top; var qrcodeTop = qrcodeForm.top;
var x = (iLeft - vLeft) / scaleRatio; var x = (qrcodeLeft - videoLeft) / scaleRatio;
var y = (iTop - vTop) / scaleRatio; var y = (qrcodeTop - videoTop) / scaleRatio;
var vpath = io.splitpath(videoPath); var videoFileDetails = io.splitpath(videoPath);
var qrpath = io.splitpath(qrcodePath); var qrcodeFileDetails = io.splitpath(qrcodePath);
var 二维码裁剪后宽度 = frmChild.width / scaleRatio; var qrcodeCroppedWidth = qrcodeForm.width / scaleRatio;
var 二维码裁剪后高度 = frmChild.height / scaleRatio; var qrcodeCroppedHeight = qrcodeForm.height / scaleRatio;
var qrcodePath_new = io._exedir++"temp\"++ qrpath.name ++ "\" ++ qrpath.name ++ "-output" ++ qrpath.ext; var qrcodeOutputPath = io._exedir++"temp\"++ qrcodeFileDetails.name ++ "\" ++ qrcodeFileDetails.name ++ "-output" ++ qrcodeFileDetails.ext;
// ffmpeg -i input.jpg -vf "scale=width:height" output.jpg // ffmpeg -i input.jpg -vf "scale=width:height" output.jpg
var str = `-i `++ qrcodePath ++` -vf "scale=`++ 二维码裁剪后宽度 ++`:`++ 二维码裁剪后高度 ++`" -y `++qrcodePath_new; var ffmpegCommand = `-i `++ qrcodePath ++` -vf "scale=`++ qrcodeCroppedWidth ++`:`++ qrcodeCroppedHeight ++`" -y `++qrcodeOutputPath;
mainForm.edit.print("ffmpeg str: ", str); mainForm.edit.print("ffmpeg command: ", ffmpegCommand);
var ffmpeg = process.ffmpeg(, str); var ffmpegProcess = process.ffmpeg(, ffmpegCommand);
ffmpeg.logResponse(mainForm.edit); ffmpegProcess.logResponse(mainForm.edit);
ffmpeg.onResponseEnd = function(){ ffmpegProcess.onResponseEnd = function(){
// ffmpeg -i input.mp4 -i overlay.png -filter_complex "overlay=10:10" output.mp4 // 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 ++ `" -y ` ++ io._exedir ++ vpath.name ++`-output`++vpath.ext; var ffmpegCommand = `-i `++ videoPath ++` -i `++ qrcodeOutputPath ++` -filter_complex "overlay=`++ x ++`:`++ y ++ `" -y ` ++ io._exedir ++ videoFileDetails.name ++`-output`++videoFileDetails.ext;
mainForm.edit.print("ffmpeg str: ", str); mainForm.edit.print("ffmpeg command: ", ffmpegCommand);
var ffmpeg = process.ffmpeg(, str); var ffmpegProcess = process.ffmpeg(, ffmpegCommand);
ffmpeg.logResponse(mainForm.edit); ffmpegProcess.logResponse(mainForm.edit);
ffmpeg.onResponseEnd = function(){ ffmpegProcess.onResponseEnd = function(){
mainForm.edit.print("完成。"); mainForm.edit.print("完成。");
mainForm.msgbox("处理完成,在原视频目录下查看 -output后缀") mainForm.msgbox("处理完成,在原视频目录下查看 -output后缀")
} }
} }
} }
::User32.SetWindowDisplayAffinity(mainForm.hwnd, 0x11); ::User32.SetWindowDisplayAffinity(mainForm.hwnd, 0x11);
mainForm.show(); mainForm.show();