Skip to content

Instantly share code, notes, and snippets.

@Coro365
Created September 1, 2019 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coro365/8d0b1946a9ee12286d76ed73db7d6827 to your computer and use it in GitHub Desktop.
Save Coro365/8d0b1946a9ee12286d76ed73db7d6827 to your computer and use it in GitHub Desktop.
Generate chapter.ini file for youtube comment setlist
# How to
# Meke video file and comment file and video length
# ex. ~/video.mp4 and ~/video.txt
# Run `ruby chapter-for-youtube-setlist.rb 1:23:23 video.txt`
# Generated ~/video.ini
# Run `ffmpeg -i ふろうふしのうた-Zw4bukluYcI.mp4 -i ふろうふしのうた-Zw4bukluYcI.ini -map_metadata 1 -c copy /ふろうふしのうた-Zw4bukluYcI-add-cap.mp4`
def to_ms(time)
t = time.split(":").map { |e| e.to_i }
s, m, h = t.reverse
s = (h.to_i * 60 * 60) + (m * 60) + s
ms = s * 1000
end
movie_length = ARGV[0]
comment_file = File.expand_path(ARGV[1])
file_name = File.basename(comment_file, ".*")
comment_array = File.read(comment_file).split("\n")
chapter_file = File.join(File.dirname(comment_file),file_name + ".ini")
# Match pattern
# ptt = /(^.*?\d{1,2}:\d{2})\s(.*?$)/ # 10:10 title
ptt = /(^.*?\d{1,2}:\d{2})\s(.*?)\/(.*$)/ # 10:10 title/songwriter
songs = comment_array.map { |e| e.match(ptt).to_a }.compact.reject(&:empty?)
chapters = songs.map.with_index do |song, i|
orgin, time, title, songwriter = song.map { |e| e.strip }
index_ms = to_ms(time)
next_index_ms = (songs.size == i+1) ? to_ms(movie_length) : to_ms(songs[i+1][1])
track = <<-"EOS"
[CHAPTER]
TIMEBASE=1/1000
START=#{index_ms}
END=#{next_index_ms}
title=#{title}/#{songwriter}
EOS
end
header = ";FFMETADATA1"
chapters = header + chapters.join
File.write(chapter_file, chapters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment