Skip to content

Instantly share code, notes, and snippets.

@monsieuroeuf
Last active February 21, 2024 05:44
Show Gist options
  • Save monsieuroeuf/d0eca163e39aaa65b60a to your computer and use it in GitHub Desktop.
Save monsieuroeuf/d0eca163e39aaa65b60a to your computer and use it in GitHub Desktop.
Makes evenly spaced markers for Adobe Premiere Pro, useful for cutting to music. Give it a BPM & duration and it'll give you some marker data for pasting into an XML document.
#!/usr/bin/env ruby -wU
# markerbeat.rb by Ian Haigh 2015
# distributed under the MIT license, more info here
# http://opensource.org/licenses/MIT
require 'optparse'
class FrameMarker
attr_accessor :bpm, :duration, :framesPerSecond
def initialize
@duration = 120 # default 2 mins
@framesPerSecond = 25.0
@bpm = 60.0
end
def frameAtTime(t)
(t * @framesPerSecond).round()
end
def allFrames(upTo)
counter = 0
ary = []
until counter > upTo.to_f
counter += beatDuration
ary.push((counter * @framesPerSecond).round())
end
return ary
end
def beatDuration
60.0/@bpm
end
def to_s
return <<-END
FPS: #{@framesPerSecond}
BPM: #{@bpm}
Duration: #{@duration}
Beat duration: #{beatDuration}
END
end
def xml
s=""
allFrames(@duration).each do |frame|
s += <<-THE_END
<marker>
<comment></comment>
<name></name>
<in>#{frame}</in>
<out>-1</out>
</marker>
THE_END
end
s
end
end
f = FrameMarker.new()
options = {}
optparse = OptionParser.new do |opts|
opts.on("-h", "--help", 'Display this screen') do
puts opts
exit
end
opts.on('-b=BPM', "--bpm BPM", 'Beats per minute') do |bpm|
f.bpm = bpm.to_f
end
opts.on('-x', "--xml", "Print XML suitable for Premiere Pro") do |variable|
options[:xml] = true
end
opts.on('-d=DURATION', "--duration DURATION", "Duration of the music in seconds") do |d|
# options[:duration] = d
f.duration = d
end
opts.on('-f=FPS', "--frames FPS", "Frames per second (default: 25)") do |d|
# options[:fps] = d
f.framesPerSecond = d.to_f
end
end
if ARGV.empty?
puts optparse
exit
end
optparse.parse!
puts f.xml if options[:xml]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment