Code archives/File Utilities/Fix Subtitle SRT Uppercase

This code has been declared by its author to be Public Domain code.

Download source code

Fix Subtitle SRT Uppercase by dw8172016
Being quite familiar with Subtitle Edit, one feature I noticed that was absent in it was the ability to take beginning sentences and ensure they are uppercase.

This is needed if you take CLOSE CAPTIONING and convert it.

While Subtitle Edit has a very nice library to determine ' what proper names need to begin Uppercase, it does not do the same for normal sentences ending in punctuation marks.

Very surprising.

So I made this quick program to rectify that situation. BEWARE ! Saves changes back to original file. Backup first If you just want to experiment.
'     ________________________________________
'    //                                     //
'   // "Fix Subtitle SRT Uppercase"        //
'  // Version: "Shift Key"                //
' // Written by David W (dw817) 04/03/16 //
'//_____________________________________//
'
' Being quite familiar with Subtitle Edit, one feature I
' noticed that was absent in it was the ability to take
' beginning sentences and ensure they are uppercase.
'
' This is needed if you take CLOSE CAPTIONING and convert it.
'
' While Subtitle Edit has a very nice library to determine
' what proper names need to begin Uppercase, it does not do
' the same for normal sentences ending in punctuation marks.
'
' Very surprising.
'
' So I made this quick program to rectify that situation.
' BEWARE ! Saves changes back to original file. Backup
' first If you just want to experiment.

Strict
Local c$,f$,o$,t$,u$,i,br,uc=1,fp:TStream

f$=RequestFile("* LOAD SRT FILE *") ' load select file
If f$="" Or KeyDown(27) Then End ' exit if blank or ESCAPE key

fp=ReadStream(f$) ' get file handle for SRT file
t$=LoadText(fp) ' load it ALL in one swoop into T$
CloseStream fp ' close file

For i=0 Until Len(t$)
  c$=t$[i..i+1] ' read each character one at a time.
  If c$="<" Then br=1 ' ensure we are not inside a bracket command
  If c$=">" And br=1 Then br=0 ' if we are, ignore uppercase requests
  If br=0
    u$=Upper$(c$)
    If uc=1 And u$>="A" And u$<="Z" Then c$=u$ ; uc=0 ' must be a letter to uppercase and flag completion
    If c$="." Or c$="!" Or c$="?" Then uc=1 ' only flag for uppercase with these characters
  EndIf
  o$:+c$ ' add to output text
Next

Print o$ ' show output in debug screen
fp=WriteStream(f$) ' write to file we just read from
WriteString fp,o$ ' enter contents of OUTPUT text
CloseStream fp ' close file

Comments

Derron2016
c$=t$[i..i+1] ' read each character one at a time.

-> c = t[i]
should be enough (and faster)

So you could do a

for local c:int = EachIn t
if c = Asc("<") ...
...


Subtitles might be continuing a previous sentence.

Hi my name ...
.. is Earl.

becomes then
Hi my name ...
.. Is Earl.


Of course this depends on the way the SRT was authored - and what punctuation marks are available in that language.

What if the punctuation mark comes first, or if quotation marks are used "Hi.", «Hi.», „Hi.”


This is my UCFirst function, maybe it is of use for you:

'convert first alpha (optional alphanumeric) character to uppercase.
'does _not_ work with UTF8 characters (would need some utf8-library
'like glib or so)
Function UCFirst:String(s:String, length:Int = 1, skipNumeric:Int = True)
	If s.length = 0 Then Return ""

	For Local start:Int = 0 Until s.length
		Local ch:Int = s[start]
		If (ch>=Asc("a") And ch<=Asc("z")) Or (Not skipNumeric And (ch>=Asc("0") And ch<=Asc("9")))
			Return Left(s, start) + Upper( Mid(s, start+1, length) ) + Right(s, s.length - length - start)
		EndIf
	Next
	
	Return s
End Function

Print UCFirst("~q!hello amigo!~q")
Print UCFirst("~q!hello amigo!~q", 15)

(Edit: shortened it a bit)

In your case you should be able to remove all spaces between ">" and "<" and then split on "><". Keeping the original formatting intact would need two split-steps.


bye
Ron


dw8172016
*POW !*

This would be a nightmare to debug.
If (ch>=Asc("a") And ch<=Asc("z")) Or (Not skipNumeric And (ch>=Asc("0") And ch<=Asc("9")))
			Return Left(s, start) + Upper( Mid(s, start+1, length) ) + Right(s, s.length - length - start)
		EndIf

I'll stay with mine. :)
    If uc=1 And u$>="A" And u$<="Z" Then c$=u$ ; uc=0 ' must be a letter to uppercase and flag completion
    If c$="." Or c$="!" Or c$="?" Then uc=1 ' only flag for uppercase with these characters
 o$:+c$



Code Archives Forum