careca Posted Sunday at 11:10 AM Posted Sunday at 11:10 AM Hi, i am trying to sync subtitles to movies, but there is a problem with the code, it doesn't stop at 60, depending on the situation, the code writes a number higher than 60. Please see example: expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Run_Debug_Mode=n #pragma compile(ProductVersion, 1.3) #pragma compile(FileVersion, 1.3) #pragma compile(x64, false) #pragma compile(UPX, False) #AutoIt3Wrapper_UseX64=n #AutoIt3Wrapper_Res_SaveSource=y #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 #AutoIt3Wrapper_Run_Tidy=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ;============================================================================= #Region ;Include + Opt Local $aProcessList = ProcessList(@AutoItExe) If $aProcessList[0][0] >= 2 Then Exit ;============================================================================= #include <File.au3> #include <Array.au3> #include <GUIConstants.au3> #include <GUIConstantsEx.au3> #include <TrayConstants.au3> #include <WindowsConstants.au3> #include <WinAPISys.au3> #include <WinAPISysWin.au3> #include <APISysConstants.au3> Opt("TrayMenuMode", 3) Opt("TrayIconHide", 0) Opt("GUIResizeMode", 1) Opt("TrayIconDebug", 1) Opt("TrayAutoPause", 0) Opt("MouseCoordMode", 2) Opt("GUIOnEventMode", 1) Opt("MustDeclareVars", 0) Opt("GUIEventOptions", 1) Opt("TrayOnEventMode", 1) Opt("ExpandEnvStrings", 1) Opt("WinDetectHiddenText", 1) #EndRegion ;Include + Opt ;============================================================================= Local $ReadField = 2500 Local $Split = StringSplit('00:26:56,474 --> 00:26:58,934', ' --> ', 3) Local $Rep0 = StringReplace(StringReplace($Split[0], ',', ''), ':', '') Local $Rep1 = StringReplace(StringReplace($Split[1], ',', ''), ':', '') Local $Rep0F = $Rep0 + $ReadField Local $Rep1F = $Rep1 + $ReadField ConsoleWrite(MyFormat($Rep0F) & ' --> ' & MyFormat($Rep1F)) ;============================================================================= Func MyFormat($sVar) $sVar = StringLeft(StringFormat('%09s', $sVar), 9) Return StringLeft($sVar, 2) & ":" & StringMid($sVar, 3, 2) & ":" & StringMid($sVar, 5, 2) & "," & StringRight($sVar, 3) EndFunc ;==>MyFormat ;============================================================================= Adding 2500 turns 00:26:58,934 into 00:26:61,434 What am i missing? Thanks in advance. Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
Solution ioa747 Posted Sunday at 12:19 PM Solution Posted Sunday at 12:19 PM The problem in your code is that you are treating time (hours, minutes, seconds, milliseconds) as a simple decimal number. When you add 2500 to 58934, the result is of course 61434. However, time is based on 60 for minutes/seconds and 1000 for milliseconds. To fix this, you need to convert the entire timestamp to a total of milliseconds (ms), do the addition there, and then convert the result back to HH:MM:SS,mmm format. expandcollapse popupLocal $ReadField = 2500 ; The milliseconds to add Local $TimeRange = '00:26:56,474 --> 00:26:58,934' Local $Split = StringSplit($TimeRange, ' --> ', 3) ; Process both timestamps Local $StartTime_ms = TimeToMS($Split[0]) Local $EndTime_ms = TimeToMS($Split[1]) ; Add the offset Local $NewStart = MSToTime($StartTime_ms + $ReadField) Local $NewEnd = MSToTime($EndTime_ms + $ReadField) ConsoleWrite($NewStart & ' --> ' & $NewEnd & @CRLF) ; Output: 00:26:58,974 --> 00:27:01,434 ;============================================================================= ; Convert HH:MM:SS,mmm to total Milliseconds Func TimeToMS($sTime) Local $aSplit = StringSplit(StringReplace($sTime, ',', ':'), ':', 3) If UBound($aSplit) < 4 Then Return 0 Local $h = Int($aSplit[0]) * 3600000 Local $m = Int($aSplit[1]) * 60000 Local $s = Int($aSplit[2]) * 1000 Local $ms = Int($aSplit[3]) Return $h + $m + $s + $ms EndFunc ; Convert total Milliseconds back to HH:MM:SS,mmm Func MSToTime($iMS) Local $h = Int($iMS / 3600000) $iMS = Mod($iMS, 3600000) Local $m = Int($iMS / 60000) $iMS = Mod($iMS, 60000) Local $s = Int($iMS / 1000) Local $ms = Mod($iMS, 1000) Return StringFormat("%02d:%02d:%02d,%03d", $h, $m, $s, $ms) EndFunc argumentum and careca 1 1 I know that I know nothing
MattyD Posted Sunday at 01:44 PM Posted Sunday at 01:44 PM Just curious, would you not use SMPTE timecode or something rather than ms in order to sync subtitles to a frame? I'm sure had some conversion code in my midi project which I can probably dig up if need be..
careca Posted Sunday at 02:18 PM Author Posted Sunday at 02:18 PM expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Run_Debug_Mode=n #pragma compile(CompanyName, '') #pragma compile(ProductVersion, 1.3) #pragma compile(FileVersion, 1.3) #pragma compile(x64, false) #pragma compile(UPX, False) #AutoIt3Wrapper_Icon=srtsync.ico #AutoIt3Wrapper_UseX64=n #AutoIt3Wrapper_Res_Comment=By: #AutoIt3Wrapper_Res_Description=Subtitle SRT Synchronizer #AutoIt3Wrapper_Res_SaveSource=y #AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 #AutoIt3Wrapper_Run_Tidy=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ;============================================================================= #Region ;Include + Opt Local $aProcessList = ProcessList(@AutoItExe) If $aProcessList[0][0] >= 2 Then Exit ;============================================================================= #include <File.au3> #include <Array.au3> #include <GUIConstants.au3> #include <GUIConstantsEx.au3> #include <TrayConstants.au3> #include <WindowsConstants.au3> #include <WinAPISys.au3> #include <WinAPISysWin.au3> #include <APISysConstants.au3> Opt("TrayMenuMode", 3) Opt("TrayIconHide", 0) Opt("GUIResizeMode", 1) Opt("TrayIconDebug", 1) Opt("TrayAutoPause", 0) Opt("MouseCoordMode", 2) Opt("GUIOnEventMode", 1) Opt("MustDeclareVars", 0) Opt("GUIEventOptions", 1) Opt("TrayOnEventMode", 1) Opt("ExpandEnvStrings", 1) Opt("WinDetectHiddenText", 1) #EndRegion ;Include + Opt ;============================================================================= #Region ;GuiVars Global $GUI, $Frm_main, $Button_1, $ExitItem, $msg, $RegRemItem, $Check ;MiscVars Global $LabelDrop, $__aGUIDropFiles = 0, $Path, $aArray, $ListFiles Global $array2, $array3, $array4, $arrayR, $Ext, $MathInput, $Sel, $SRT, $FOpen, $FTmpOpen, $FRLine, $LineCount, $Split, $Perc Global $Rep0, $Rep1, $Rep0F, $Rep1F, $ReadField, $Fail = 0, $CntMenu = 0, $FRArray, $ReadPath, $ReadLabel #EndRegion ;GuiVars ;============================================================================= #Region ;GUI $GUI = GUICreate('Subtitle SRT Synchronizer', 350, 145, -1, -1, -1, $WS_EX_ACCEPTFILES) $LabelDrop = GUICtrlCreateLabel("Drop the subtitle .srt file here.", 10, 10, 330, 45, $SS_LEFT) GUICtrlSetTip($LabelDrop, 'Drop the subtitle .srt file here.', 'Filename and path', 1) GUICtrlSetState($LabelDrop, $GUI_DROPACCEPTED) GUICtrlCreateGroup('', 5, 0, 340, 50) $Button_1 = GUICtrlCreateButton('Save Changes', 10, 100) GUICtrlSetOnEvent($Button_1, "Save") $MathInput = GUICtrlCreateInput("+", 10, 65, 100, 20) $Sel = GUICtrlCreateUpdown($MathInput) GUICtrlCreateLabel("Time in 'ms', works with negative values too," & @CRLF & " operator + or - must be present.", 120, 63, 220, 40) GUISetOnEvent($GUI_EVENT_DROPPED, "Drop") GUISetOnEvent($GUI_EVENT_CLOSE, "Quit") GUISetOnEvent($GUI_EVENT_MINIMIZE, "Minimize") GUISetOnEvent($GUI_EVENT_RESTORE, "Restore") Local $CntCheckBox = GUICtrlCreateCheckbox('Set Context Menu', 105, 110) $ExitItem = TrayCreateItem("Close") TrayItemSetOnEvent(-1, "Quit") DirCreate(@LocalAppDataDir & "\SrtSync") FileInstall('SRTSync.ico', @LocalAppDataDir & "\SrtSync\SRTSync.ico", 1) GUISetIcon(@LocalAppDataDir & "\SrtSync\SRTSync.ico") TraySetIcon(@LocalAppDataDir & "\SrtSync\SRTSync.ico") TraySetState(1) TraySetClick(8) GUIRegisterMsg($WM_DROPFILES, 'WM_DROPFILES') _WinAPI_ChangeWindowMessageFilterEx($GUI, $WM_DROPFILES, $MSGFLT_ALLOW) _WinAPI_ChangeWindowMessageFilterEx($GUI, $WM_COPYDATA, $MSGFLT_ALLOW) _WinAPI_ChangeWindowMessageFilterEx($GUI, $WM_COPYGLOBALDATA, $MSGFLT_ALLOW) #EndRegion ;GUI ;============================================================================= If $CmdLine[0] <> 0 Then If $CmdLine[1] <> '' Then $SRT = $CmdLine[1] GUICtrlSetData($LabelDrop, $CmdLine[1]) EndIf EndIf GUISetState() ;============================================================================= If RegRead('HKEY_CURRENT_USER\SOFTWARE\Classes\srtfile\shell\SrtSync', '') = 'SrtSync' Then GUICtrlSetState($CntCheckBox, $GUI_CHECKED) $CntMenu = 1 EndIf ;============================================================================= Func Save() If FileExists($SRT) Then $Path = StringLeft($SRT, StringInStr($SRT, '\', 0, -1)) ;============================================================================= $ReadField = GUICtrlRead($MathInput) $ReadLabel = GUICtrlRead($LabelDrop) FileCopy($SRT, $Path & '.bak') FileCopy($SRT, $Path & 'temp.srt') $FTmpOpen = FileOpen($Path & 'temp.srt', 2) $FOpen = FileOpen($SRT, 0) $FRArray = FileReadToArray($FOpen) For $l = 0 To UBound($FRArray) - 1 If StringInStr($FRArray[$l], ' --> ', 0, -1) Then $Split = StringSplit($FRArray[$l], ' --> ', 3) ; Process both timestamps Local $StartTime_ms = TimeToMS($Split[0]) Local $EndTime_ms = TimeToMS($Split[1]) ; Add the offset Local $NewStart = MSToTime($StartTime_ms + $ReadField) Local $NewEnd = MSToTime($EndTime_ms + $ReadField) FileWriteLine($FTmpOpen, MSToTime($StartTime_ms + $ReadField) & ' --> ' & MSToTime($EndTime_ms + $ReadField)) Else FileWriteLine($FTmpOpen, $FRArray[$l]) EndIf Next FileClose($FTmpOpen) FileClose($FOpen) FileDelete($SRT) FileMove($Path & 'temp.srt', $SRT) Sleep(500) MsgBox(64 + 262144, 'Done', 'Done') Else MsgBox(64 + 262144, 'Failed', 'Srt file not present') EndIf Beep(300, 1000) EndFunc ;==>Save ;============================================================================= ; Convert HH:MM:SS,mmm to total Milliseconds Func TimeToMS($sTime) Local $aSplit = StringSplit(StringReplace($sTime, ',', ':'), ':', 3) If UBound($aSplit) < 4 Then Return 0 Local $h = Int($aSplit[0]) * 3600000 Local $m = Int($aSplit[1]) * 60000 Local $s = Int($aSplit[2]) * 1000 Local $ms = Int($aSplit[3]) Return $h + $m + $s + $ms EndFunc ;==>TimeToMS ;============================================================================= ; Convert total Milliseconds back to HH:MM:SS,mmm Func MSToTime($iMS) Local $h = Int($iMS / 3600000) $iMS = Mod($iMS, 3600000) Local $m = Int($iMS / 60000) $iMS = Mod($iMS, 60000) Local $s = Int($iMS / 1000) Local $ms = Mod($iMS, 1000) Return StringFormat("%02d:%02d:%02d,%03d", $h, $m, $s, $ms) EndFunc ;==>MSToTime ;============================================================================= Func WM_DROPFILES($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $lParam Switch $iMsg Case $WM_DROPFILES Local Const $aReturn = _WinAPI_DragQueryFileEx($wParam) If UBound($aReturn) Then $__aGUIDropFiles = $aReturn Else Local Const $aError[1] = [0] $__aGUIDropFiles = $aError EndIf EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_DROPFILES ;============================================================================= Func Drop() If $__aGUIDropFiles[0] > 1 Then MsgBox(64, 'Drag&Drop', 'Drop only one file please.') ElseIf $__aGUIDropFiles[0] = 1 Then $SRT = $__aGUIDropFiles[1] $array4 = StringLen($__aGUIDropFiles[1]) $array3 = StringInStr($__aGUIDropFiles[1], "\", 0, -1) $array2 = $array4 - $array3 $arrayR = StringTrimLeft($__aGUIDropFiles[1], $array3) $Ext = StringRight($arrayR, 3) If $Ext <> 'srt' Then MsgBox(64, 'Drag&Drop', 'Drop a file with .srt extension.') Else GUICtrlSetData($LabelDrop, $arrayR) EndIf EndIf EndFunc ;==>Drop ;============================================================================= Do ;Main If GUICtrlRead($CntCheckBox) = 1 And $CntMenu = 0 Then $CntMenu = 1 RegWrite('HKEY_CURRENT_USER\SOFTWARE\Classes\.srt', '', 'REG_SZ', 'srtfile') RegWrite('HKEY_CURRENT_USER\SOFTWARE\Classes\srtfile\DefaultIcon', '', 'REG_SZ', '"' & @LocalAppDataDir & '\SrtSync\SRTSync.ico"') RegWrite('HKEY_CURRENT_USER\SOFTWARE\Classes\srtfile\shell\SrtSync', '', 'REG_SZ', 'SrtSync') RegWrite('HKEY_CURRENT_USER\SOFTWARE\Classes\srtfile\shell\SrtSync', 'Icon', 'REG_SZ', '"' & @LocalAppDataDir & '\SrtSync\SRTSync.ico"') RegWrite('HKEY_CURRENT_USER\SOFTWARE\Classes\srtfile\shell\SrtSync\Command', '', 'REG_SZ', '"' & @AutoItExe & '" ' & '"%1"') MsgBox(64, 'Information', 'Registry key set. To use press right mouse button on top of a' & @CRLF & '.srt (subtitle) file, then click "SrtSync" in context menu.') ElseIf GUICtrlRead($CntCheckBox) = 4 And $CntMenu = 1 Then $CntMenu = 0 RegDelete('HKEY_CURRENT_USER\SOFTWARE\Classes\srtfile\shell\SrtSync') MsgBox(64, 'Information', 'Removed the context menu for this application from registry') EndIf ;============================================================================= Sleep(100) Until GUIGetMsg() = $GUI_EVENT_CLOSE ;============================================================================= #Region ;Window Func Minimize() WinSetState('', '', @SW_MINIMIZE) EndFunc ;==>Minimize Func Restore() WinSetState('', '', @SW_RESTORE) EndFunc ;==>Restore Func Quit() Exit EndFunc ;==>Quit #EndRegion ;Window ;============================================================================= Thank you! SRTSync.ico ioa747 and argumentum 2 Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now