-
-
Save anonymous/f2dcc89559c2b11cc610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'nokogiri' | |
require 'active_support/core_ext' | |
require 'iconv' | |
def avid_text(file_path) | |
html = Nokogiri::HTML(File.open(file_path), nil, 'utf-8') | |
main = {start_time: -1.0, end_time: 0.0, text: ''} | |
suppl = {start_time: -1.0, end_time: 0.0, text: ''} | |
subtitle = "@ This file written with the Avid Caption plugin, version 1\r\n\r\n<begin subtitles>\r\n" | |
html.xpath('.//word').each do |word| | |
if main[:start_time] == -1.0 | |
main[:start_time] = (word['start'].to_f > main[:end_time]) ? word['start'].to_f : main[:end_time] | |
end | |
if main[:text].length + suppl[:text].length >= 80 | |
subtitle << make_line(main) | |
main = {start_time: (main[:end_time] > suppl[:start_time]) ? main[:end_time] : suppl[:start_time], | |
end_time: suppl[:end_time], text: suppl[:text] + word.text} | |
suppl = {start_time: -1.0, end_time: 0, text: ''} | |
elsif main[:text].length >= 60 && main[:text][main[:text].length-1] == " " | |
suppl[:start_time] = word['start'].to_f if suppl[:start_time] == -1.0 | |
suppl[:end_time] = word['end'].to_f | |
suppl[:text] << word.text | |
else | |
main[:end_time] = word['end'].to_f | |
main[:text] << word.text | |
end | |
if word.xpath('./br').present? && main[:text].present? | |
main[:end_time] = suppl[:end_time] if suppl[:end_time] != 0 | |
main[:text] << suppl[:text] | |
subtitle << make_line(main) | |
main = {start_time: -1.0, end_time: main[:end_time], text: ''} | |
suppl = {start_time: -1.0, end_time: 0, text: ''} | |
end | |
end | |
# Write file | |
subtitle << "<end subtitles>\r\n" | |
# Without NULL between each char | |
# subtitle = Iconv.conv("utf-16le", "utf-8", "\ufeff" + subtitle) | |
subtitle = Iconv.conv("utf-16le", "utf-8", "\ufeff" + subtitle).b | |
File.open("#{File.dirname(file_path)}/#{File.basename(file_path, File.extname(file_path))}_avid.txt", 'wb+'){|f| f.write(subtitle)} | |
end | |
def make_line(main) | |
subtitle = '' | |
subtitle << "%02.0f"%((main[:start_time]/3600).to_i) + ':' + "%02.0f"%(((main[:start_time]/60)%60).to_i) + ':' + ("%02.0f"%((main[:start_time].to_i)%60)) + ':' + "%02.0f"%(((main[:start_time].to_f%1)*100).to_i) + ' ' | |
subtitle << "%02.0f"%((main[:end_time]/3600).to_i) + ':' + "%02.0f"%(((main[:end_time]/60)%60).to_i) + ':' + ("%02.0f"%((main[:end_time].to_i)%60)) + ':' + "%02.0f"%(((main[:end_time].to_f%1)*100).to_i) + "\r\n" | |
if main[:text].length < 40 | |
subtitle << main[:text] + "\r\n\r\n" | |
else | |
line1 = main[:text].truncate(main[:text].length/2, separator: ' ', omission: '') + "\r\n" | |
line2 = main[:text][line1.length-1, main[:text].length-1] | |
while line2[0]==' ' | |
line2 = line2[1, line2.length-1] | |
end | |
subtitle << line1 + line2 + "\r\n\r\n" | |
end | |
return subtitle | |
end | |
print 'Enter a path file: ' | |
avid_text gets.chomp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment