Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.