Skip to content

Instantly share code, notes, and snippets.

@ephesus
Created May 12, 2016 06:57
Show Gist options
  • Save ephesus/0eb1543584cb1655d5d27ee1064e2725 to your computer and use it in GitHub Desktop.
Save ephesus/0eb1543584cb1655d5d27ee1064e2725 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: UTF-8
# script to sync .srt rips from http://jpsubbers.web44.net/Japanese-Subtitles/
# to match timing of jdramacity versions (remove commercial breaks from .srt files)
require 'srt'
require 'charlock_holmes'
if ARGV.count != 2
puts "<source srt> <destination srt>"
exit
end
file = SRT::File.parse(File.new(ARGV[0]))
parts = file.split(at: ["00:17:00,000", "00:32:00,000", "00:41:21,000"])
parts[0].timeshift(all: "-32s")
parts[0].append( "00:14:28,000" => parts[1])
parts[0].append( "00:27:28,500" => parts[2])
parts[0].append( "00:34:48,000" => parts[3])
result = parts[0].lines.map(&:to_s).join("\n")
# convert detected encoding (usually SHIFT_JIS Japanese) to UTF-8
# detection = CharlockHolmes::EncodingDetector.detect(result)
# result = CharlockHolmes::Converter.convert result, detection[:encoding], 'SHIFT-JIS'
File.open(ARGV[1], 'w') { |f| f.write(result) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment