Skip to content

Instantly share code, notes, and snippets.

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