Files
tools/压缩包/au3脚本/文件规范化命名-识别格式扩充.au3
T

291 lines
11 KiB
AutoIt
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=icon.ico
#AutoIt3Wrapper_Outfile=VideoFolderRenamer.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseUpx=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <FileConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <Excel.au3>
#include <Inet.au3>
#include <MsgBoxConstants.au3>
; ============================================
; 全局变量(兼容 AutoIt 3.3.18.0)
; ============================================
Global $g_sSelectedPath = ""
Global $g_aMP4Files[0] ; 将在 _FileListToArrayRec 后赋值
Global $g_aResults[1][4] ; 预分配二维数组,列: [MP4文件名, MP4路径, 原文件夹名, 新UUID]
Global $g_iResultCount = 0
Global $g_bProcessing = False
; ============================================
; 主程序入口
; ============================================
Main()
Func Main()
; 创建GUI界面
Local $hGUI = GUICreate("视频文件夹重命名工具 v1.0", 600, 400, -1, -1)
; 选择目录按钮
Local $hBtnSelectDir = GUICtrlCreateButton("📁 选择视频目录", 20, 20, 150, 40)
GUICtrlSetFont(-1, 10, 600)
; 开始执行按钮
Local $hBtnStart = GUICtrlCreateButton("▶️ 开始执行", 200, 20, 150, 40)
GUICtrlSetFont(-1, 10, 600)
GUICtrlSetState(-1, $GUI_DISABLE) ; 初始禁用
; 日志显示框
Local $hLogEdit = GUICtrlCreateEdit("", 20, 80, 560, 280, BitOR($ES_AUTOVSCROLL, $ES_READONLY, $WS_VSCROLL, $ES_MULTILINE))
GUICtrlSetFont(-1, 9, 400, 0, "Consolas")
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetLimit(-1, 100000)
GUISetState(@SW_SHOW, $hGUI)
; 消息循环
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $hBtnSelectDir
; 选择目录
Local $sPath = FileSelectFolder("请选择包含视频的文件夹", "")
If $sPath <> "" Then
$g_sSelectedPath = $sPath
_Log($hLogEdit, "✓ 已选择目录: " & $g_sSelectedPath)
GUICtrlSetState($hBtnStart, $GUI_ENABLE)
EndIf
Case $hBtnStart
If $g_sSelectedPath = "" Then
MsgBox($MB_ICONWARNING, "警告", "请先选择视频目录!")
ContinueCase
EndIf
; 禁用按钮,防止重复点击
GUICtrlSetState($hBtnSelectDir, $GUI_DISABLE)
GUICtrlSetState($hBtnStart, $GUI_DISABLE)
$g_bProcessing = True
; 清空日志和结果
GUICtrlSetData($hLogEdit, "")
$g_iResultCount = 0
ReDim $g_aResults[1][4] ; 重置结果数组
; 扫描MP4文件
_Log($hLogEdit, "🔍 正在扫描目录中的视频文件...")
Local $aTemp = _FileListToArrayRec($g_sSelectedPath, "*.mp4;*.avi;*.mkv;*.mov;*.wmv;*.flv;*.mpeg;*.mpg;*.webm", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
If Not @error And IsArray($aTemp) And UBound($aTemp) > 0 Then
$g_aMP4Files = $aTemp
Else
Dim $g_aMP4Files[0] ; ✅ 正确创建空数组(UBound = 0)
EndIf
If UBound($g_aMP4Files) = 0 Or (UBound($g_aMP4Files) = 1 And $g_aMP4Files[0] = 0) Then
_Log($hLogEdit, "❌ 未找到任何视频文件!")
GUICtrlSetState($hBtnSelectDir, $GUI_ENABLE)
GUICtrlSetState($hBtnStart, $GUI_ENABLE)
$g_bProcessing = False
ContinueCase
EndIf
_Log($hLogEdit, "✓ 找到 " & UBound($g_aMP4Files) & " 个视频文件")
_Log($hLogEdit, "")
; 处理每个MP4文件
Local $iSuccessCount = 0
Local $iFailCount = 0
For $i = 0 To UBound($g_aMP4Files) - 1
If Not $g_bProcessing Then ExitLoop
Local $sMP4Path = $g_aMP4Files[$i]
Local $sMP4Name = StringRegExpReplace($sMP4Path, "^.+[\\/]", "")
Local $sMP4BaseName = StringRegExpReplace($sMP4Name, "(?i)\.(mp4|avi|mkv|mov|wmv|flv|mpeg|mpg|webm)$", "", 1)
Local $sMP4Dir = StringRegExpReplace($sMP4Path, "[\\/][^\\/]+$", "")
; 检查同名文件夹是否存在
Local $sFolderName = $sMP4BaseName
Local $sFolderPath = $sMP4Dir & "\" & $sFolderName
If DirGetSize($sFolderPath) = -1 Then
_Log($hLogEdit, "⚠️ [" & ($i + 1) & "/" & UBound($g_aMP4Files) & "] " & $sMP4Name & " - 未找到同名文件夹")
$iFailCount += 1
ContinueLoop
EndIf
; 检查playlist.m3u8是否存在
Local $sM3U8Path = $sFolderPath & "\playlist.m3u8"
If Not FileExists($sM3U8Path) Then
_Log($hLogEdit, "⚠️ [" & ($i + 1) & "/" & UBound($g_aMP4Files) & "] " & $sMP4Name & " - 文件夹中不存在playlist.m3u8")
$iFailCount += 1
ContinueLoop
EndIf
; 获取新的UUID
_Log($hLogEdit, "[" & ($i + 1) & "/" & UBound($g_aMP4Files) & "] " & $sMP4Name & " - 正在获取新UUID...")
Local $sNewUUID = _GetNewUUID()
If $sNewUUID = "" Then
_Log($hLogEdit, "❌ [" & ($i + 1) & "/" & UBound($g_aMP4Files) & "] " & $sMP4Name & " - 获取UUID失败")
$iFailCount += 1
ContinueLoop
EndIf
; 重命名文件夹
Local $sNewFolderPath = $sMP4Dir & "\" & $sNewUUID
If DirMove($sFolderPath, $sNewFolderPath) Then
_Log($hLogEdit, "✓ [" & ($i + 1) & "/" & UBound($g_aMP4Files) & "] " & $sMP4Name & " - 重命名成功: " & $sFolderName & " → " & $sNewUUID)
; 扩容结果数组(如果需要)
If $g_iResultCount >= UBound($g_aResults) Then
ReDim $g_aResults[$g_iResultCount + 10][4]
EndIf
; 保存结果
$g_aResults[$g_iResultCount][0] = $sMP4Name
$g_aResults[$g_iResultCount][1] = $sMP4Path
$g_aResults[$g_iResultCount][2] = $sFolderName
$g_aResults[$g_iResultCount][3] = $sNewUUID
$g_iResultCount += 1
$iSuccessCount += 1
Else
_Log($hLogEdit, "❌ [" & ($i + 1) & "/" & UBound($g_aMP4Files) & "] " & $sMP4Name & " - 重命名失败")
$iFailCount += 1
EndIf
; 等待0.2秒
Sleep(200)
Next
; 调整结果数组大小
If $g_iResultCount > 0 Then
ReDim $g_aResults[$g_iResultCount][4]
EndIf
; 生成Excel报告
If $g_iResultCount > 0 Then
_Log($hLogEdit, "")
_Log($hLogEdit, "📊 正在生成Excel报告...")
If _GenerateExcelReport() Then
_Log($hLogEdit, "✓ Excel报告已生成: " & @ScriptDir & "\VideoFolderMapping.xlsx")
Else
_Log($hLogEdit, "❌ Excel报告生成失败")
EndIf
EndIf
; 显示统计信息
_Log($hLogEdit, "")
_Log($hLogEdit, "========================================")
_Log($hLogEdit, "📊 处理完成!")
_Log($hLogEdit, " ✓ 成功: " & $iSuccessCount & " 个")
_Log($hLogEdit, " ⚠️ 失败: " & $iFailCount & " 个")
_Log($hLogEdit, " 📁 总计: " & UBound($g_aMP4Files) & " 个视频文件")
_Log($hLogEdit, "========================================")
; 恢复按钮状态
GUICtrlSetState($hBtnSelectDir, $GUI_ENABLE)
GUICtrlSetState($hBtnStart, $GUI_ENABLE)
$g_bProcessing = False
EndSwitch
WEnd
GUIDelete($hGUI)
EndFunc
; ============================================
; 获取新的UUID
; ============================================
Func _GetNewUUID()
Local $sURL = "https://api.yeomai.site/API/ApiGetShortUUID.php"
Local $sResponse = ""
; 尝试最多3次
For $i = 1 To 3
$sResponse = BinaryToString(InetRead($sURL, 10))
If @error = 0 And $sResponse <> "" Then
; 清理响应数据,移除空白字符
$sResponse = StringStripWS($sResponse, $STR_STRIPLEADING + $STR_STRIPTRAILING)
Return $sResponse
EndIf
Sleep(500)
Next
Return ""
EndFunc
; ============================================
; 日志输出函数
; ============================================
Func _Log($hEdit, $sText)
Local $sCurrent = GUICtrlRead($hEdit)
GUICtrlSetData($hEdit, $sCurrent & $sText & @CRLF)
; 滚动到最后一行
Local $iLines = GUICtrlSendMsg($hEdit, $EM_GETLINECOUNT, 0, 0)
GUICtrlSendMsg($hEdit, $EM_LINESCROLL, 0, $iLines)
EndFunc
; ============================================
; 生成Excel报告
; ============================================
Func _GenerateExcelReport()
If $g_iResultCount = 0 Then Return False
Local $oExcel = _Excel_Open(False, False, False, False, True)
If @error Then Return SetError(1, 0, False)
Local $oWorkbook = _Excel_BookNew($oExcel, 1)
If @error Then
_Excel_Close($oExcel)
Return SetError(2, 0, False)
EndIf
; 设置表头
Local $aHeaders[1][4] = [["MP4文件名", "MP4完整路径", "原文件夹名", "新UUID文件夹名"]]
_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aHeaders, "A1")
; 写入数据
_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $g_aResults, "A2")
; 格式化表格
With $oWorkbook.Activesheet
.Range("A1:D1").Font.Bold = True
.Range("A1:D1").Interior.Color = 0xD3D3D3
.Columns("A:A").ColumnWidth = 30
.Columns("B:B").ColumnWidth = 60
.Columns("C:C").ColumnWidth = 30
.Columns("D:D").ColumnWidth = 35
.UsedRange.Borders.Weight = 2
EndWith
; 保存文件
Local $sSavePath = @ScriptDir & "\VideoFolderMapping.xlsx"
_Excel_BookSaveAs($oWorkbook, $sSavePath, $xlOpenXMLWorkbook, True)
If @error Then
_Excel_BookClose($oWorkbook, False)
_Excel_Close($oExcel)
Return SetError(3, 0, False)
EndIf
_Excel_BookClose($oWorkbook, False)
_Excel_Close($oExcel)
Return True
EndFunc