Skip to content

Instantly share code, notes, and snippets.

@brenes
Last active June 8, 2018 07:47
Show Gist options
  • Save brenes/6d4ac03d9e58e94457e0 to your computer and use it in GitHub Desktop.
Save brenes/6d4ac03d9e58e94457e0 to your computer and use it in GitHub Desktop.
Small script for fixing srt files with blank lines
# This small script uses the 'srt' gem to parse the srt file
# It solves problems with certain subtitle files with empty lines that make Flex crash
# You can use it with files, folders with files or even folders with subfolders with files (God bless Recursivity)
# USAGE: ruby fix-srt.rb file/to/fix.srt
# USAGE: ruby fix-srt.rb folder/with/files/to/fix
# USAGE: ruby fix-srt.rb folders/with/subfolders/to/fix
require 'rubygems'
require 'srt'
class StrFixer
def self.fix file_or_directory_path
if File.directory? file_or_directory_path
fix_directory file_or_directory_path
else
fix_file File.new(file_or_directory_path)
end
end
def self.fix_directory directory
Dir.glob("#{directory}/**/*.srt").each do |file_path|
fix_file File.new(file_path)
end
end
def self.fix_file file
puts "Fixing #{file.path}"
str_file = SRT::File.parse(file)
fixed = SRT::File.new
right_lines = 0
str_file.lines.each_with_index do |line, i|
puts line.sequence if line.text.join("").empty?
unless line.text.join("").empty?
right_lines += 1
new_line = line.clone
new_line.sequence = right_lines
fixed.lines << new_line
end
end
file.close
File.open(file.path, "w") { |f| f.write fixed.to_s }
end
end
StrFixer.fix ARGV.first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment