Class: MIAConverter::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/MIAConverter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})

	@original_dir = Dir.pwd

	@original_file = File.expand_path 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
	options.each {|key,value| self.instance_variable_set("@#{key}", value) }
end

Instance Attribute Details

#length_secondsObject

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_fileObject

Path to original file



13
14
15
# File 'lib/MIAConverter.rb', line 13

def original_file
  @original_file
end

#shot_gapObject

seconds apart to take video screenshots



22
23
24
# File 'lib/MIAConverter.rb', line 22

def shot_gap
  @shot_gap
end

#start_secondsObject

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_timeObject

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_nameObject



97
98
99
# File 'lib/MIAConverter.rb', line 97

def animated_gif_file_name
	"animated.gif"
end

#animated_gif_pathObject



105
106
107
# File 'lib/MIAConverter.rb', line 105

def animated_gif_path
	"#{get_temp_folder}/#{animated_gif_file_name}"
end

#base_folderObject

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_pathObject



101
102
103
# File 'lib/MIAConverter.rb', line 101

def chopped_video_path
	chopped_path = "#{get_temp_folder}/chopped.mp4"
end

#create_animated_imageObject



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_imagesObject



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_videoObject

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_folderObject

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_folderObject

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

Returns:

  • (Boolean)


46
47
48
# File 'lib/MIAConverter.rb', line 46

def original_file_valid?
	File.exists? @original_file
end

#processObject

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_pathObject

Alias for get_images_folder



110
111
112
# File 'lib/MIAConverter.rb', line 110

def screenshots_folder_path
	get_images_folder
end

#still_image_pathsObject

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_stringObject

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