Skip to content

Instantly share code, notes, and snippets.

@Goblin80
Last active May 6, 2016 18:56
Show Gist options
  • Save Goblin80/61a4b4dd7779a17c5c5b to your computer and use it in GitHub Desktop.
Save Goblin80/61a4b4dd7779a17c5c5b to your computer and use it in GitHub Desktop.
A tool to sync lyrics to songs
require 'colorize'
require 'clipboard'
class LyricFile
attr_accessor :name, :raw, :timed
def initialize(path)
if path == 'Clipboard'
@raw = arrayify(Clipboard.paste)
@name = 'Clipboard'
else
@raw = arrayify(File.read(path))
# Apply CamelCase to the filename
@name = path[0..-5].split(' ').map(&:capitalize).join(' ')
end
# @synced = false
@timed = ''
end
def preview_lyrc(n)
puts "**Displaying a #{n} lines preview:**".cyan
puts "\n"
n.times do |i|
puts "#{raw[i]}\n"
end
end
def sync_lyrc(speed = 1.00)
a = Time.now
@raw.each do |s|
puts s != '' ? s : '**BLANK LINE**'.black.on_white
gets
b = Time.now
@timed += "#{Time.at((b - a) * speed).utc.strftime('[%M:%S.%L]')} #{s.chomp}\r\n"
end
# @synced = true
end
def write_file
File.write("#{@name}.lrc", timed)
puts "Synced lyrics written to \"#{@name}.lrc\"".green
end
def write_clipboard
Clipboard.copy(timed)
puts 'Synced lyrics written to the Clipboard'.green
end
end
def arrayify(s)
s.encode('UTF-8').split("\n").map(&:chomp)
end
s = Clipboard.paste
if s.size > 300 && s[0..3] != '[00:'
q = LyricFile.new('Clipboard')
puts "=> Clipboard data detectd:\n".green
elsif !Dir.glob('*.srt').empty?
q = LyricFile.new(Dir.glob('*.srt')[0])
puts "=> File detected: \"#{q.name}\"\n".green
else
puts 'No lyrics in the Clipboard nor a .srt file'
exit
end
q.preview_lyrc(3)
puts "\nTo start syncing, Press Enter...".green
gets
q.sync_lyrc
puts '=============================================='.green
# q.write_file
q.write_clipboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment