module SplitVideo
Constants
- USAGE
- VERSION
Public Class Methods
split_a_video(filename, options)
click to toggle source
# File lib/split_video.rb, line 16 def self.split_a_video(filename, options) unless (!! options['slices']) ^ (!! options['duration']) puts USAGE exit end if options['slices'] SplitVideo.split_by_slices(filename, options['slices'].to_i, options) elsif options['duration'] SplitVideo.split_by_duration(filename, options['duration'].to_i, options) end end
split_by_duration(filename, duration, options = {})
click to toggle source
# File lib/split_video.rb, line 48 def self.split_by_duration(filename, duration, options = {}) puts "Sorry I haven't implemented this yet." exit end
split_by_slices(filename, num_slices, options = {})
click to toggle source
# File lib/split_video.rb, line 29 def self.split_by_slices(filename, num_slices, options = {}) seconds_per_slice = duration_in_seconds(filename) / num_slices offset = 0.0 (0..(num_slices - 1)).each do |slice| print "Cutting slice #{slice}" %x{ ffmpeg -i "#{filename}" \ -vcodec copy \ -acodec copy \ -ss "#{offset}" \ -t "#{seconds_per_slice}" \ #{output_filename_for(filename) % slice} } puts "...done!" offset += seconds_per_slice end end
Private Class Methods
duration_in_seconds(filename)
click to toggle source
# File lib/split_video.rb, line 55 def self.duration_in_seconds(filename) duration_string = %x{ffmpeg -i #{filename} 2>&1 | grep Duration} duration_string =~ /(\d{2}):(\d{2}):(\d{2}.\d{2})/ hours, minutes, seconds = [$1, $2, $3].map(&:to_f) hours * 3600 + minutes * 60 + seconds end
output_filename_for(filename)
click to toggle source
# File lib/split_video.rb, line 63 def self.output_filename_for(filename) filename.gsub %r{\A(.*)\.([^\.]+)\Z}, '\1-part-%04d.\2' end