Skip to content

Instantly share code, notes, and snippets.

@vysogot
Created March 16, 2017 09:15
Show Gist options
  • Save vysogot/b0e9ba886803210b0124af5d548eceaf to your computer and use it in GitHub Desktop.
Save vysogot/b0e9ba886803210b0124af5d548eceaf to your computer and use it in GitHub Desktop.
YouTube Transcript to SRT - Ruby script
# Paste your transcript between the quotes
# and run
# ruby youtube-transcript-to-srt.rb > subtitles.srt
transcript = "0:00 Hello and welcome
0:05 This is a very nice story
0:10 and it needs subtitles"
times = transcript.split("\n").map {|x| x[0..4].strip.rjust(5, '0')}
times << times[-1]
buffer = ""
index = 0
transcript.each_line do |line|
text = line[5..-1].strip
srt_entry = "#{index+1}\n00:#{times[index]},000 --> 00:#{times[index+1]},000\n#{text}\n\n"
buffer += srt_entry
index += 1
end
puts buffer
#Output:
#1
#00:00:00,000 --> 00:00:05,000
#Hello and welcome
#
#2
#00:00:05,000 --> 00:00:10,000
#This is a very nice story
#
#3
#00:00:10,000 --> 00:00:10,000
#and it needs subtitles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment