Class: MIAConverter::Converter
- Inherits:
-
Object
- Object
- MIAConverter::Converter
- Defined in:
- lib/MIAConverter.rb
Instance Attribute Summary collapse
-
#length_seconds ⇒ Object
Number of seconds of original video represented in animated GIF.
-
#original_file ⇒ Object
Path to original file.
-
#shot_gap ⇒ Object
seconds apart to take video screenshots.
-
#start_seconds ⇒ Object
Number of seconds into original video to start animated GIF.
-
#transition_time ⇒ Object
Time between animated GIF frame transitions.
Instance Method Summary collapse
- #animated_gif_file_name ⇒ Object
- #animated_gif_path ⇒ Object
-
#base_folder ⇒ Object
Returns path to /tmp/MIAConverter.
- #chopped_video_path ⇒ Object
- #create_animated_image ⇒ Object
- #create_still_images ⇒ Object
-
#extract_relevant_parts_of_video ⇒ Object
Make video shorter, so we have only relevant parts.
-
#get_images_folder ⇒ Object
Returns images folder for this run of the converter object.
-
#get_temp_folder ⇒ Object
Returns path to temporary folder for this run of the converter object.
-
#initialize(original_file, options = {}) ⇒ Converter
constructor
A new instance of Converter.
-
#original_file_valid? ⇒ Boolean
Tests if the passed in original file exists.
-
#process ⇒ Object
Processes input video * extracts relevant parts of original video * produces screenshots * turns screenshots into an animated GIF.
-
#screenshots_folder_path ⇒ Object
Alias for get_images_folder.
-
#still_image_paths ⇒ Object
Array of all .jpg filenames in images folder.
-
#time_second_string ⇒ Object
Returns number string based on time object was created.
Constructor Details
#initialize(original_file, options = {}) ⇒ Converter
Returns a new instance of Converter.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/MIAConverter.rb', line 28 def initialize(original_file, ={}) @original_dir = Dir.pwd @original_file = File. original_file @init_time = Time.now # DEFAULTS @shot_gap = 0.5 @start_seconds = 0 @transition_time = 20 @length_seconds = 20 # Set instance variables from options dictionary .each {|key,value| self.instance_variable_set("@#{key}", value) } end |
Instance Attribute Details
#length_seconds ⇒ Object
Number of seconds of original video represented in animated GIF
19 20 21 |
# File 'lib/MIAConverter.rb', line 19 def length_seconds @length_seconds end |
#original_file ⇒ Object
Path to original file
13 14 15 |
# File 'lib/MIAConverter.rb', line 13 def original_file @original_file end |
#shot_gap ⇒ Object
seconds apart to take video screenshots
22 23 24 |
# File 'lib/MIAConverter.rb', line 22 def shot_gap @shot_gap end |
#start_seconds ⇒ Object
Number of seconds into original video to start animated GIF
16 17 18 |
# File 'lib/MIAConverter.rb', line 16 def start_seconds @start_seconds end |
#transition_time ⇒ Object
Time between animated GIF frame transitions
25 26 27 |
# File 'lib/MIAConverter.rb', line 25 def transition_time @transition_time end |
Instance Method Details
#animated_gif_file_name ⇒ Object
97 98 99 |
# File 'lib/MIAConverter.rb', line 97 def animated_gif_file_name "animated.gif" end |
#animated_gif_path ⇒ Object
105 106 107 |
# File 'lib/MIAConverter.rb', line 105 def animated_gif_path "#{get_temp_folder}/#{animated_gif_file_name}" end |
#base_folder ⇒ Object
Returns path to /tmp/MIAConverter. Creates foler if necessary.
70 71 72 73 74 |
# File 'lib/MIAConverter.rb', line 70 def base_folder temp_folder_path = "/tmp/MIAConverter" Dir.mkdir(temp_folder_path) unless Dir.exists?(temp_folder_path) temp_folder_path end |
#chopped_video_path ⇒ Object
101 102 103 |
# File 'lib/MIAConverter.rb', line 101 def chopped_video_path chopped_path = "#{get_temp_folder}/chopped.mp4" end |
#create_animated_image ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/MIAConverter.rb', line 155 def create_animated_image animation = Magick::ImageList.new(*still_image_paths) animation.delay = @transition_time animation.write(animated_gif_path) animated_gif_path end |
#create_still_images ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/MIAConverter.rb', line 136 def create_still_images img_folder = get_images_folder # call once # BUILD UP THE LIST OF SEEK_TIMES seek_times = (0.0...@length_seconds).step(@shot_gap).to_a.select do |time| time <= @chopped_movie.duration || time <= @length_seconds end # SCREEN GRABS seek_times.each.with_index do |time, idx| padded_string = "%010i" % idx file_name = "#{img_folder}/shot-#{padded_string}.jpg" @chopped_movie.screenshot(file_name, seek_time: time) end still_image_paths end |
#extract_relevant_parts_of_video ⇒ Object
Make video shorter, so we have only relevant parts
127 128 129 130 131 132 133 |
# File 'lib/MIAConverter.rb', line 127 def extract_relevant_parts_of_video orig_movie = FFMPEG::Movie.new(@original_file) throw "Start Seconds beyond end of video" if @start_seconds > orig_movie.duration @chopped_movie = orig_movie.transcode(chopped_video_path, duration: @length_seconds + 1, seek_time: @start_seconds) chopped_video_path end |
#get_images_folder ⇒ Object
Returns images folder for this run of the converter object. Creates folder if necessary.
86 87 88 89 90 |
# File 'lib/MIAConverter.rb', line 86 def get_images_folder images_folder_path = "#{get_temp_folder}/images" Dir.mkdir(images_folder_path) unless Dir.exists?(images_folder_path) images_folder_path end |
#get_temp_folder ⇒ Object
Returns path to temporary folder for this run of the converter object. Creates folder if necessary.
78 79 80 81 82 |
# File 'lib/MIAConverter.rb', line 78 def get_temp_folder current_tmp_path = "#{base_folder}/#{time_second_string}" Dir.mkdir(current_tmp_path) unless Dir.exists?(current_tmp_path) current_tmp_path end |
#original_file_valid? ⇒ Boolean
Tests if the passed in original file exists
46 47 48 |
# File 'lib/MIAConverter.rb', line 46 def original_file_valid? File.exists? @original_file end |
#process ⇒ Object
Processes input video
-
extracts relevant parts of original video
-
produces screenshots
-
turns screenshots into an animated GIF
55 56 57 58 59 |
# File 'lib/MIAConverter.rb', line 55 def process extract_relevant_parts_of_video create_still_images create_animated_image end |
#screenshots_folder_path ⇒ Object
Alias for get_images_folder
110 111 112 |
# File 'lib/MIAConverter.rb', line 110 def screenshots_folder_path get_images_folder end |
#still_image_paths ⇒ Object
Array of all .jpg filenames in images folder
115 116 117 |
# File 'lib/MIAConverter.rb', line 115 def still_image_paths Dir["#{get_images_folder}/*.jpg"] end |
#time_second_string ⇒ Object
Returns number string based on time object was created.
93 94 95 |
# File 'lib/MIAConverter.rb', line 93 def time_second_string @init_time.to_f.to_s.sub('.', '-') end |