Help on how to clean a txt files for creating chapter files

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dareing T
Posts: 5
Joined: 15 Jan 2020, 23:12

Help on how to clean a txt files for creating chapter files

16 Jan 2020, 12:26

I am a nub to ahk and coding in general and need help on creating a script on cleaning a txt with timestamps. It would look at each line and would replace 12:34 or 1234 or 123405 or 12:34:05 or 12:34.05 to 00:12:34.000000000 or 00:12:34.050000000. I going to use it for MKVtoolNIX in creating XML chapter files.

Side note any ideas :idea: on how to automate taking the clean numbers and turning them into chapter files for each episode. You don`t want to know how much to time it removes from my life. :wtf:

Also any thoughts on how to copy timestamps from VLC?
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Help on how to clean a txt files for creating chapter files

16 Jan 2020, 14:49

Without seeing your actual file, it's hard to figure out the formatting, but the script below shows how to use RegEx to find matches:

Code: Select all

#SingleInstance, Force

Timestamps =
(
12:34
1234
123405
12:34:05
12:34.05
)

Loop, Parse, % Timestamps, `n
{
	If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{4})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{6})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ":" Match3 "0000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2}).(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	}
}

MsgBox, % NewTime
Dareing T
Posts: 5
Joined: 15 Jan 2020, 23:12

Re: Help on how to clean a txt files for creating chapter files

19 Jan 2020, 13:56

Here`s the chapter file. I don`t understand how to add the .txt file to the script, sorry I`m new to this sort of stuff.
Attachments
Test Capter File.txt
(711 Bytes) Downloaded 47 times
garry
Posts: 3777
Joined: 22 Dec 2013, 12:50

Re: Help on how to clean a txt files for creating chapter files

19 Jan 2020, 16:04

script from TheDewd , read textfile
can also create a GUI with drag&drop to put text file in GUI and then convert

Code: Select all

#SingleInstance, Force
/*
Timestamps =
(
12:34
1234
123405
12:34:05
12:34.05
)
*/
F1=%a_scriptdir%\test capter file.txt
F2=%a_scriptdir%\NEW.txt
ifexist,%f2%
  filedelete,%f2%
fileread,timestamps,%f1%
;msgbox,%timestamps%
Loop, Parse, % Timestamps, `n,`r
{
	If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{4})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{6})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ":" Match3 "0000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2}).(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	}
}

;MsgBox, % NewTime
fileappend,%newtime%,%f2%
run,%f2%
return
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Help on how to clean a txt files for creating chapter files

20 Jan 2020, 09:40

Added another RegExMatch to catch a timecode having a space in-between, and included `r for the Loop, Parse code (thanks, garry!). Used FileSelectFile to allow easy use of the script with your existing chapter files.

Code: Select all

#SingleInstance, Force

FileSelectFile, TimestampFile, 3,, Select Timestamp File..., *.*

If (ErrorLevel) {
	ExitApp
}

FileRead, Timestamp, % TimestampFile

Loop, Parse, % Timestamp, `n, `r
{
	LoopField := RegexReplace(A_LoopField, "^\s+|\s+$")

	If (RegExMatch(LoopField, "^(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(LoopField, "^(\d{2})\s(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(LoopField, "^(\d{4})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(LoopField, "^(\d{6})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	} Else If (RegExMatch(LoopField, "^(\d{2}):(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ":" Match3 "0000000`n"
	} Else If (RegExMatch(LoopField, "^(\d{2}):(\d{2}).(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	} Else {
		NewTime .= LoopField "`n"
	}
}

SplitPath, TimestampFile,,, OutExtension, OutNameNoExt
FileAppend, % NewTime, % OutNameNoExt "_FIXED." OutExtension

MsgBox, % "File created: " OutNameNoExt "_FIXED." OutExtension

ExitApp
Dareing T
Posts: 5
Joined: 15 Jan 2020, 23:12

Re: Help on how to clean a txt files for creating chapter files

20 Jan 2020, 16:37

Thanks for the support this was just what I was looking :thumbup: . That drag and drop idea sounds good I need to look that up on how to do. I probably going that make another script or merge with this one to paste the timestamps into MKVtoolnix to make a XML chapter files.
garry
Posts: 3777
Joined: 22 Dec 2013, 12:50

Re: Help on how to clean a txt files for creating chapter files

21 Jan 2020, 03:55

Fileselectfile also good idea
here a drag&drop example ( create also a test file > fx=%a_desktop%\Test Capture File.txt )
drag&drop different text files to convert ( time format 36:35.10 > 00:36:35.100000000 )

Code: Select all

;-https://www.autohotkey.com/boards/viewtopic.php?p=310518#p310518
/*
;- script from ahk-user TheDewd  
;--  orig text ----------------
d5 17
12:02
23:30
36:35.10
41:19
;-- new text ------------------
00:12:02.000000000
00:23:30.000000000
00:36:35.100000000
00:41:19.000000000
;------------------------------
*/

;-------------------------------------------------------------
;- drag&drop different text-files and click on row in listbox
;- ( example xy.txt > xy_NEW.txt )
#warn
#NoEnv
setworkingdir,%a_scriptdir%
filename1=Drag&Drop Text-files
extensions:="txt,csv,bat,ahk,log,bas"  ;- < extensions
gosub,testfile
   
GUI,2:+AlwaysOnTop
Gui,2: -DPIScale
Gui,2: Color, ControlColor, Black
Gui,2: Font, CDefault s14, Lucida Console
Gui,2:Add, ListBox, x5     y10   w1280 h200 cYellow gRun1 vF1,
GUI,2:show, W1300 H230 X20 Y200,%filename1%
return
2Guiclose:
exitapp
;-----------------------
2GuiDropFiles:
GuiControl,2:,F1
Loop, parse, A_GuiEvent, `n
   {
   SplitPath,a_loopfield, name, dir, ext, name_no_ext, drive
   if ext in %extensions%
      GuiControl,2:,F1,%A_LoopField%
   }
return
;-----------------------
Run1:
Gui,2:submit,nohide
SplitPath, F1, name, dir, ext, name_no_ext, drive
fileread,timestamps,%f1% 
newtime:=""
Loop, Parse, % Timestamps, `n,`r
{
	If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{4})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 ".000000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{6})$", Match)) {
		RegExMatch(Match, "^(\d{2})(\d{2})(\d{2})$", Match)
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2}):(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 ":" Match3 "0000000`n"
	} Else If (RegExMatch(A_LoopField, "^(\d{2}):(\d{2}).(\d{2})$", Match)) {
		NewTime .= "00:" Match1 ":" Match2 "." Match3 "0000000`n"
	}
}
timestamps=
f2=%a_desktop%\%name_no_ext%_NEW.%ext%
ifexist,%f2%
  filedelete,%f2%
fileappend,%newtime%,%f2%
run,%f2%
return
;------- create a test-file --------------------
testfile:
fx=%a_desktop%\Test Capture File.txt
e=
(Ltrim join`r`n
Season 2

ep3
11:29
37:04
45:35

ep4
14:41
24:11
45 36

ep5
21:33
32:37
45:33

EP6 
22:08
31:34
38:26
43:55

ep7
26:32
37:20
43:56

ep8
08:35
26:29
45:30

9
22:00
35:36
45:19

10
08:24
19:34
44:03

11
11:12
34:40
45:27

12
14:43
28:29
37:00
45:29

13
05:24
18:27
37:01
43:50

14
13:37
26:00
32:45
47:10

15
12:35
27:30
45:28

16
21:56
32:04
45:32

d5 17
12:02
23:30
36:35.10
41:19
45:31  

18
22:19
36:27
45:33

19
10:52
33:16
47:31

20
27:00
38:35
47:34

21
15:46
33:05
35:03
36:08
47:34

22
12:44
34:45 
45:26

23
12:22
19:46
44:08

24
12:00
37:51
45:27

S3
D125
)
ifnotexist,%fx%
fileappend,%e%,%fx%
e=
return
;======================== END SCRIPT ==============


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Chunjee, sofista, The_One_True_Rick and 108 guests