Skip to content

Instantly share code, notes, and snippets.

@emisjerry
Created May 15, 2020 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emisjerry/4f8d98bc7fe8a17fdc2aafa70a382990 to your computer and use it in GitHub Desktop.
Save emisjerry/4f8d98bc7fe8a17fdc2aafa70a382990 to your computer and use it in GitHub Desktop.
AutoSub-AHK: A GUI for AutoSub, written in Autohotkey
; Generated by AutoGUI 2.6.2
; ===========================
; Author: Jerry Jian
; YouTube: https://www.youtube.com/channel/UC2xBvmv-lhsY4suxv46lplg?view_as=subscriber
; Blog: http://jdev.tw/blog
; v1.0 2020/05/13
; Call gen.bat to generate subtitle. You can change the autosub parameters in gen.bat.
; v1.0.1 2020/05/14 minor changes
; ===========================
#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
SetBatchLines -1
#Include %A_ScriptDir%\ControlColor.ahk
I_ICON := A_ScriptDir . "\cc.ico"
global videoFiles
global VIDEO_FORMAT_FAILED
videoFiles := []
version := "v1.0.1"
; 多語系設定
if (A_Language in 0404, 0c04, 1404) { ; Chinese_Taiwan, Hong Kong, Macau
TITLE := "AutoSub-AHK 字幕產生器"
LANGUAGES := "zh-TW 中文(繁體)||zh-CN 中文(簡體)|en-US English|ja-JP 日本語|ko-KR 한국어|yue-hant-hk 粵語"
VIDEO_LANGUAGE := "影片語言(&L)"
VIDEO_FILES := "選擇影片檔案(&S)"
OUTPUT_FILENAME := "輸出檔名(&O)"
OUTPUT_MSG := "(副檔名必須為.txt或.srt)"
DRAG_MSG := "拖曳功能已啟用"
BTN_OK := "確定執行(&E)"
BTN_CLOSE := "關閉(&C)"
VIDEO_SELECT_TITLE := "選擇影片檔案(可複選)"
VIDEO_FORMAT_FAILED := "影片副檔名必須是 .mp4"
} else if (A_Language in 0804,1004) { ; Chinese_PRC, singapore
TITLE := "AutoSub-AHK 字幕产生器"
LANGUAGES := "zh-TW 中文(繁体)|zh-CN 中文(简体)||en-US English|ja-JP 日本语|ko-KR 한국어|yue-hant-hk 粤语"
VIDEO_LANGUAGE := "视频语言(&L)"
VIDEO_FILES := "选择视频文件(&S)"
OUTPUT_FILENAME := "输出档名(&O)"
OUTPUT_MSG := "(副档名必须为.txt或.srt)"
DRAG_MSG := "拖拽功能已启用"
BTN_OK := "确定执行(&E)"
BTN_CLOSE := "关闭(&C)"
VIDEO_SELECT_TITLE := "选择影片文件(可复选)"
VIDEO_FORMAT_FAILED := "影片副档名必须为 .mp4"
} else {
TITLE := "AutoSub-AHK SubTITLEs Generator"
LANGUAGES := "zh-TW Chinese(Traditional)|zh-CN Chinese(Simplified)|en-US English||ja-JP Japanese|ko-KR Korean|yue-hant-hk Hong Konf"
VIDEO_LANGUAGE := "Video &Language"
VIDEO_FILES := "&Select Video File(s)"
OUTPUT_FILENAME := "&Output file name"
OUTPUT_MSG := "(File extension must ends with .txt or .srt)"
DRAG_MSG := "Drag and Drop Enabled"
BTN_OK := "&Execute"
BTN_CLOSE := "&Close"
VIDEO_SELECT_TITLE := "Select video file(s)"
VIDEO_FORMAT_FAILED := "Video's extension must be .mp4"
}
Menu Tray, Icon, %I_ICON%
Gui +hWndhMainWnd +Resize -MaximizeBox
Gui Font, s12
Gui Add, ComboBox, vCmbLanguage x12 y56 w281, %LANGUAGES%
Gui Font
Gui Font, s12
Gui Add, Text, x-152 y0 w120 h24 +0x200, %VIDEO_LANGUAGE%
Gui Add, Text, x200 y110 w250 h24 +0x200, %DRAG_MSG%
Gui Font
Gui Font, s12
Gui Add, Button, gbtnSelectFiles x12 y104 w180 h35, %VIDEO_FILES%
Gui Font
Gui Font, s12
Gui Add, ListBox, vListBox1 x8 y144 w496 h144 +0x1201
Gui Font
Gui Font, s12
Gui Add, Text, x12 y304 w233 h24, %OUTPUT_FILENAME%
Gui Font
Gui Font, s12
Gui Add, Edit, vedtOutputFilename x8 y336 w496 h27
Gui Font
Gui Font, s16 cGreen
Gui Add, Button, hWndhBtne2 gbtnOK x48 y392 w180 h61 +Default, %BTN_OK%
Gui Font
ControlColor(hBtne2, hMainWnd, 0x008000)
Gui Font, s16
Gui Add, Button, gbtnClose x280 y392 w180 h61, %BTN_CLOSE%
Gui Font
Gui Font, s12
Gui Add, Text, x12 y24 w120 h24 +0x200, %VIDEO_LANGUAGE%
Gui Font
Gui Font, s12
Gui Add, Text, x150 y304 w300 h24 +0x200, %OUTPUT_MSG%
Gui Font
Gui Add, Text, x6 y380 w507 h2 +0x10
Gui Show, w518 h469, %TITLE% %version%
Return
/*
CmbLanguageChange:
GuiControlGet, CmbLanguage,,
msgbox lang=%CmbLanguage%
Return
*/
btnSelectFiles:
;FileSelectFile, OutputVar , Options, RootDir\Filename, Title, Filter
FileSelectFile, files, M3, , %VIDEO_SELECT_TITLE%, *.mp4 ; M3 = Multiselect existing files.
if (files = "") {
return
}
;msgbox files=%files%
folder := ""
Loop, parse, files, `n
{
if (A_Index = 1) {
folder = %A_LoopField%
} else {
isMP4 := chkVideoFile(A_LoopField)
if (isMP4 = 1) {
videoFiles.push(folder . "\" . A_LoopField)
}
}
}
files := ""
for index, element in videoFiles ; Enumeration is the recommended approach in most cases.
{
;MsgBox % "Element number " . index . " is " . element
if (index = 1) {
name := element
StringReplace name, name, .mp4, .srt
GuiControl, , edtOutputFilename, %name%
}
files := files . element . "|"
}
GuiControl, , ListBox1, %files%
Return
btnOK(CtrlHwnd, GuiEvent, EventInfo, ErrLevel := "") {
Gui, Submit
GuiControlGet, CmbLanguage,,
iPos := InStr(CmbLanguage, " ")
Language := Substr(CmbLanguage, 1, iPos-1)
if (Language = "zh-TW") {
Language := "cmn-hant-tw"
} else if (Language = "zh-CN") {
Language := "cmn-hans-cn"
}
;MsgBox lang=%Language%$
GuiControlGet, edtOutputFilename,,
; edtOutputFilename
for index, element in videoFiles
{
videoFilename := element
if (index = 1) {
newFilename := deleteOldFile(edtOutputFilename, Language)
if (InStr(edtOutputFilename, ".txt") > 0) {
batFilename := "gen-txt.bat"
} else {
batFilename := "gen.bat"
}
Run %A_ScriptDir%\%batFilename% %Language% "%videoFilename%" "%edtOutputFilename%" "%newFilename%"
} else {
edtOutputFilename := videoFilename
StringReplace edtOutputFilename, edtOutputFilename, .mp4, .srt
newFilename := deleteOldFile(edtOutputFilename, Language)
if (InStr(edtOutputFilename, ".txt") > 0) {
batFilename := "gen-txt.bat"
} else {
batFilename := "gen.bat"
}
Run %A_ScriptDir%\%batFilename% %Language% "%videoFilename%" "%edtOutputFilename%" "%newFilename%"
}
}
ExitApp
}
btnClose:
WinClose
Return
GuiSize:
If (A_EventInfo == 1) {
Return
}
Return
GuiContextMenu:
Return
GuiDropFiles(GuiHwnd, FileArray, CtrlHwnd, X, Y) {
videoFiles := []
for i, file in FileArray
{
;MsgBox File %i% is:`n%file%
if (chkVideoFile(file)) {
videoFiles.push(file)
}
}
for index, element in videoFiles ; Enumeration is the recommended approach in most cases.
{
if (index = 1) {
name := element
StringReplace name, name, .mp4, .srt
GuiControl, , edtOutputFilename, %name%
}
files := files . element . "|"
}
GuiControl, , ListBox1, %files%
}
OnClipboardChange:
Return
GuiClose:
ExitApp
ListBox1:
Return
/*
* 檢查輸入的影片副檔名是否為.mp4
*/
chkVideoFile(file) {
;MsgBox file=%file%
iPos := InStr(file, ".mp4")
if (iPos <= 0) {
MsgBox %VIDEO_FORMAT_FAILED% (%file%)
Return false
}
Return True
}
/*
* 刪除已存在的 xxx.cmn-hant-tw.srt
*/
deleteOldFile(file, language) {
iPos := InStr(file, ".")
newFilename := Substr(file, 1, iPos) . language . Substr(file, iPos, 10)
if FileExist(newFilename) {
FileDelete, %newFilename%
}
return newFilename
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment