Changing subtitle color

December 18, 2009 at 03:50:02
Specs: Windows XP
I want to change the palette color of all subtitles (idx/sub format) in a folder. As you may know "idx" is in text format, what I need is changing the line start with "palette" from

palette: 000060, 2c2c2c, 2c2c2c, 535353, 8f8f8f, 414141, b0b0b0, c9c9c9, 7b7b7b, ebebeb, 626262, dbdbdb, 353535, 9c9c9c, dfdfdf, d7d7d7
to
palette: 000060, 000000, 2c2c2c, 535353, 8f8f8f, 414141, b0b0b0, c9c9c9, 7b7b7b, ebebeb, 626262, dbdbdb, 353535, ffff00, dfdfdf, d7d7d7

That means, replace the 2nd hex code 2c2c2c with 000000 and the 14th hex code 9c9c9c with ffff00. This will change the subtitle color from grey to yellow.
The orignal hex code may vary from different idx files, but the replacement is fixed.
Can anybody give me some hints of how to write the batch?
Thanks in advance!


See More: Changing subtitle color

Report •


#1
December 18, 2009 at 13:07:57
see if this works. I'm not sure of your file's extension, so i used .idx. as you can see, the new files
are exended as .new, so you will need to "rename" or "move" them at some point.

@echo off && setlocal enabledelayedexpansion
set n1=000000,
set n2=ffff00,
for /f "tokens=1" %%A in ('dir /b /-d *.idx') do (
set fname=%%A
set newname=!fname:.idx=.new!
del !newname! 2>nul
for /f "tokens=*" %%B in (!fname!) do (
set line=%%B
if /i "!line:~0,9!" equ "palette: " (
:::
for /f "tokens=1-15*" %%a in ("!line!") do (
set line=%%a %%b %n1% %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %n2% %%p
)
)
echo !line! >> !newname!
)
)
goto :EOF

(or if you prefer to eliminate the inner loop, at ::: replace the for-loop with:)
set line=!line:~0,17!%n1%!line:~24,89!%n2%:!line:~120!

if you are curious about the code, at cmd prompt you can type:
for /?
set /?
if /?


Report •

#2
December 19, 2009 at 01:34:59
nbrane, thanks for your reply.
The file extension is "idx".
I've tried the batch, both methods work fine except there is a typing mistake in method 2 (an extra ":" after "89!%n2%").
However, both methods can't function properly for filename with space. I tried to modify the code as belows:

@echo off && setlocal enabledelayedexpansion
set n1=000000,
set n2=ffff00,
for /f "tokens=1 delims=" %%A in ('dir /b /-d *.idx') do (
set fname=%%A
set newname=!fname:.idx=.new!
del "!newname!" 2>nul
for /f "tokens=*" %%B in ("fname") do (
set line=%%B
if /i "!line:~0,9!" equ "palette: " (
for /f "tokens=1-15*" %%a in ("!line!") do (
set line=%%a %%b %n1% %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %n2% %%p
)
)
echo !line! >> "!newname!"
)
)
goto :EOF
I've tried this new batch for a file named "TM 110.idx".

There is only one line in the output file (TM 110.new)
TM 110.idx
instead of all the contents in TM 110.idx. It seems that the 2nd for-loop just set line="fname" instead of processing all items on each line of the file.
I've also tried to replace the line
for /f "tokens=*" %%B in ("fname") do (
with
for /f "tokens=*" %%B in ("%%~nxA") do (
The result is the same.

I'm very confused. What's wrong with my modification?


Report •

#3
December 19, 2009 at 06:08:20
"good eye" catching my syntax mistake (mode #2).

begin rant:
whoever the moron was at microsoft who decided to inflict spaces on the windows directorysystem should have their rectum examined to determine if their head is still lodged therein.
end rant.
for a quick-&-dirty fix, replace the following:
for /f "tokens=*" %%B in ("fname") do (

with:
for /f "tokens=*" %%B in ('type "!fname!"') do (

(ie: add single quotes and 'type...'
be sure to restore the exclms around fname)
this "fix" is not IMO really the right way to do it, but i'm too tired right now to discern the finer points of script parsing.

and probably the foll. should be fixed up:
set newname=!fname:.idx=.new!
change to:
set newname=%%~nA.new

i must have been in need of caffiene when i wrote this (the original) up (although it did work fine on "normal" filenames.)
i did test this version with a "spacey" filename and it worked.


Report •

Related Solutions

#4
December 19, 2009 at 21:52:13
Thanks again, nbrane.
First, I would like to apologize for my mistake, I've missed exlcms around fname
the line
for /f "tokens=*" %%B in ("fname") do (
should be
for /f "tokens=*" %%B in ("!fname!") do (
otherwise the result should be fname instead of the original filename.

Your "quick-&-dirty fix" solves the problem, it works for filename with or without space.
Sorry to bother you again, since there is a blank line in the idx file, the new or old batch will remove the blank line.
I've read your answers in other posts but I don't know how to implement the technique to this batch.
Is it possible to keep the blank line in this situation?


Report •

#5
December 20, 2009 at 05:17:14
here's revised version to capture blanks: (tested on a small 2-file set of data)

@echo off && setlocal enabledelayedexpansion
set n1=000000,
set n2=ffff00,
for /f "tokens=*" %%A in ('dir /b /-d *.idx') do (
set fname=%%A
set newname=%%~nA.new
del "!newname!" 2>nul
for /f %%z in ('find /v /c "" ^< "!fname!"') do (set lc=%%z)
call :BBB < "!fname!"
)
goto :eof
:BBB
for /L %%z in (1 1 !lc!) do (
set line=
set /p line=
if /i "!line:~0,9!" equ "palette: " (
:::
for /f "tokens=1-15*" %%a in ("!line!") do (
set line=%%a %%b %n1% %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %n2% %%p
)
)
set line=echo.!line!
!line! >> "!newname!"
)
goto :ex

::or at ::: replace the for-loop with:
::set line=!line:~0,17!%n1%!line:~24,89!%n2%:!line:~120!
:ex


Report •

#6
December 21, 2009 at 03:30:14
Thanks so much for your great help. The new batch works perferctly. Just being curious, both original & new files look exactly the same but the file size of the new one is a litte bit larger than the original. I find out that the batch appends a space for each line. That means, just deleted the space before the append operator >> will make the filesize of new file same as the original one.

Report •


Ask Question