I’m trying to fix a subtitles (.srt) text file that has some incorrect data with a one-line ruby script. The file looks like this:
53
00:03:52,835 --> 00:03:54,835
Boss?... BOSS?!
54
00:03:54,845 --> 00:03:56,990
55
00:0 --> 00:03:58,490
Go!
I want the 55 stanza to look like this:
55
00:03:56,490 --> 00:03:58,490
Go!
Where the first time stamp is taken from the second but with 2 seconds subtracted.
Here is my attempt, which is not working:
ruby -pi.bak -e 'gsub(/(\d{2}):(\d) --> (\d{2}):(\d{2}):(\d{2}),(\d{3})/, "#{$3}:#{$4}:#{$5},#{$6} --> #{$3}:#{$4}:#{$5.to_i - 2},#{$6}")' *.srt
EDIT
So, as pointed out by the responders, ruby 1.9.2 does not support access of regex captures via the $1, $2, etc. syntax.
The fix I ended up going with was switching back to ruby 1.8.x, and using gsub with a block as @mu suggested, and used the Time.utc / strftime magic suggested by @jonas.
Here is the final solution (on my system /usr/bin/ruby is 1.8.6):
/usr/bin/ruby -pi.bak -e 'gsub(/(\d{2}):(\d) --> (\d{2}):(\d{2}):(\d{2}),(\d{3})/) {"#{(Time.utc(1970,1,1, $3,$4,$5) - 2).strftime("%H:%M:%S")},#{$6} --> #{$3}:#{$4}:#{$5},#{$6}"}' *.srt
I am now watching my movie with correctly formatted subtitles. Thanks guys 🙂
You almost have it but you want to use the block form of
gsubrather than the two argument form and I think you have your subtraction on the wrong side of--->:Ruby 1.8 doesn’t need the
$_with the block form ofgsubbut 1.9 does. The global$1,$2, … aren’t what you think they are except in block form ofgsub:Jonas Elfström is right in the comments about subtracting 2 from the seconds in “00:04:00” making a mess. So you might want to use one of the time classes to handle your subtraction. Something like this:
in place of your
#{$3}:#{$4}:#{$5.to_i - 2},#{$6}should do the trick.Time.utcwants to work with a full date-time rather than just a time so using the Unix epoch (1970-01-01) is a bit of a hack to get around that. Of course, if you try to subtract 2s from 00:00:00 you’ll run into some problems.