Skip to content

Instantly share code, notes, and snippets.

@akostadinov
Last active June 8, 2024 21:31
Show Gist options
  • Save akostadinov/ec6b2d73514343f76586602491e98827 to your computer and use it in GitHub Desktop.
Save akostadinov/ec6b2d73514343f76586602491e98827 to your computer and use it in GitHub Desktop.
geerates chapters.txt from a youtube video id
#!/bin/env ruby
# Usage: chapters-youtube.rb IDofTHEfineVIDEO
# see mux.rb - https://gist.github.com/akostadinov/8197947def1fe61c12a5ffffc615c969
require "json"
require "open-uri"
def sec_to_time(sec)
sec = Integer(sec)
hours = sec / 3600
mins = ( sec - hours * 3600 ) / 60
sec = ( sec - hours * 3600 - mins * 60 )
"#{hours}:#{"%02d" % mins}:#{"%02d" % sec}"
end
yt_video_id = ARGV.first
base_url = "https://yt.lemnoslife.com/videos?part=chapters,contentDetails&id="
video_metadata = JSON.parse(URI.open("#{base_url}#{yt_video_id}").read)
chapters_json = video_metadata["items"].first["chapters"]["chapters"]
duration = video_metadata["items"].first["contentDetails"]["duration"]
chapter_lines = chapters_json.map {"#{sec_to_time(_1['time'])} #{_1['title']}" }
chapter_lines << "#{sec_to_time(duration)} end"
puts(chapter_lines.join("\n"))
0:00:00 Intro
0:00:15 Welcome
0:03:43 Special Message
0:12:04 end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment