Skip to content

Instantly share code, notes, and snippets.

@torbiak
Created April 4, 2016 12:21
Show Gist options
  • Save torbiak/05642bad66d0783ce3d8b96da6671dba to your computer and use it in GitHub Desktop.
Save torbiak/05642bad66d0783ce3d8b96da6671dba to your computer and use it in GitHub Desktop.
Clean and format subrip subtitles that have been converted from ASS format by ffmpeg.
#!/usr/bin/env ruby
require 'text/format'
class Entry
attr_accessor :flighting, :body
def initialize
@body = []
end
def formatted_body
body.each do |line|
line.gsub!(/<[^>]*>/, '')
line.gsub!(/\{[^\}]*\}/, '')
line.gsub!("\r", '')
end
# If lines are too long they might overflow the screen. Seems like most
# players support multiple lines at once, though. omxplayer does, at least.
tf = Text::Format.new(columns: 55, first_indent: 0)
tf.text = body.select {|x| x != ''}.join("\n")
tf.format
end
end
def each_entry(io)
e = Entry.new
io.each_line do |line|
line.chomp!
if line =~ /^\d+$/
next if e.body.empty?
yield e
e = Entry.new
next
end
if line =~ /^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$/
e.flighting = line
next
end
e.body << line
end
end
def format
i = 1
each_entry(ARGF) do |e|
puts i
puts e.flighting
puts e.formatted_body
puts
i += 1
end
end
if $0 == __FILE__
format
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment