Module: Video2gif::Options

Defined in:
lib/video2gif/options.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/video2gif/options.rb', line 9

def self.parse(args)
  options = {}

  parser = OptionParser.new do |parser|
    parser.banner = "      video2gif \#{Video2gif::VERSION}\n\n      Usage: video2gif <video> [<output GIF filename>] [options]\n    BANNER\n\n    parser.separator ''\n    parser.separator 'General GIF options:'\n\n    parser.on('-s SEEK',\n              '--seek SEEK',\n              'Set time to seek to in the input video (use a count of',\n              'seconds or HH:MM:SS.SS format)') do |s|\n      options[:seek] = s\n    end\n\n    parser.on('-t TIME',\n              '--time TIME',\n              'Set duration to use from the input video (use a count of',\n              'seconds)') do |t|\n      options[:time] = t\n    end\n\n    parser.on('-f FRAMES',\n              '--fps FRAMES',\n              'Set frames per second for the resulting GIF (default 10)') do |f|\n      options[:fps] = f\n    end\n\n    parser.on('-w WIDTH',\n              '--width WIDTH',\n              'Scale the width of the resulting GIF in pixels (aspect',\n              'ratio is preserved, default is 400 pixels)') do |w|\n      options[:width] = w\n    end\n\n    # parser.on('-hHEIGHT', '--height=HEIGHT', 'Scale the height of the resulting GIF') do |h|\n    #   options[:height] = h\n    # end\n\n    parser.on('-p PALETTE',\n              '--palette PALETTE',\n              'Set the palette size of the resulting GIF (maximum of 255',\n              'colors)') do |p|\n      options[:palette] = p\n    end\n\n    parser.on('--palette-mode MODE',\n              '--palettemode MODE',\n              'Configure a custom palette statistics mode, using either',\n              '\"full\" (single full-frame palette for the whole GIF),',\n              '\"diff\" (single palette emphasizing movement against a',\n              'static background across frames), or',\n              '\"single\" (individual palette per frame, adds to file size)',\n              '(default \"diff\")') do |p|\n      options[:palettemode] = p\n    end\n\n    parser.on('-d [ALGORITHM]',\n              '--[no-]dither [ALGORITHM]',\n              'Set the dithering algorithm for the palette generation',\n              '(default enabled with \"floyd_steinberg\")') do |d|\n      if d.nil?\n        options[:dither] = 'floyd_steinberg'\n      else\n        options[:dither] = d || 'none'\n      end\n    end\n\n    parser.on('--crop-width SIZE',\n              '--crop-size-w SIZE',\n              'Pixel size of width to select from source video, before scaling') do |s|\n      options[:wregion] = s\n    end\n\n    parser.on('--crop-height SIZE',\n              '--crop-size-h SIZE',\n              'Pixel size of height to select from source video, before scaling') do |s|\n      options[:hregion] = s\n    end\n\n    parser.on('--crop-x OFFSET',\n              '--crop-offset-x OFFSET',\n              'Pixel offset from left to select from source video, before scaling') do |o|\n      options[:xoffset] = o\n    end\n\n    parser.on('--crop-y OFFSET',\n              '--crop-offset-y OFFSET',\n              'Pixel offset from top to select from source video, before scaling') do |o|\n      options[:yoffset] = o\n    end\n\n    parser.on('-a [THRESHOLD]',\n              '--autocrop [THRESHOLD]',\n              'Attempt automatic cropping based on black region, scaled',\n              'from 0 (nothing) to 255 (everything), default threshold 24') do |c|\n      options[:autocrop] = c || 24\n    end\n\n    parser.on('--contrast CONTRAST',\n              'Apply contrast adjustment, scaled from -2.0 to 2.0 (default 1)') do |c|\n      options[:contrast] = c\n      options[:eq] = true\n    end\n\n    parser.on('--brightness BRIGHTNESS',\n              'Apply brightness adjustment, scaled from -1.0 to 1.0 (default 0)') do |b|\n      options[:brightness] = b\n      options[:eq] = true\n    end\n\n    parser.on('--saturation SATURATION',\n              'Apply saturation adjustment, scaled from 0.0 to 3.0 (default 1)') do |s|\n      options[:saturation] = s\n      options[:eq] = true\n    end\n\n    parser.on('--gamma GAMMA',\n              'Apply gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|\n      options[:gamma] = g\n      options[:eq] = true\n    end\n\n    parser.on('--red-gamma GAMMA',\n              'Apply red channel gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|\n      options[:gamma_r] = g\n      options[:eq] = true\n    end\n\n    parser.on('--green-gamma GAMMA',\n              'Apply green channel gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|\n      options[:gamma_g] = g\n      options[:eq] = true\n    end\n\n    parser.on('--blue-gamma GAMMA',\n              'Apply blue channel gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|\n      options[:gamma_b] = g\n      options[:eq] = true\n    end\n\n    parser.on('--tonemap [ALGORITHM]',\n              'Attempt to force tonemapping from HDR (BT.2020) to SDR',\n              '(BT.709) using algorithm (experimental, requires ffmpeg with',\n              'libzimg) (default \"hable\", \"mobius\" is a good alternative)') do |t|\n      options[:tonemap] = t || 'hable'\n    end\n\n    parser.on('--subtitles [INDEX]',\n              '(Experimental, requires ffprobe) Attempt to use the',\n              'subtitles built into the video to overlay text on the',\n              'resulting GIF. May be extremely slow for text subtitles.',\n              'Takes an optional integer value to choose the subtitle',\n              'stream (defaults to the first subtitle stream, index 0)') do |s|\n      options[:subtitles] = s || true\n      options[:subtitle_index] =  if options[:subtitles].is_a?(TrueClass)  # default to first stream\n                                    0\n                                  elsif options[:subtitles].match?(/\\A\\d+\\z/)  # select stream by index\n                                    options[:subtitles].to_i\n                                  elsif options[:subtitles].is_a?(String)  # open subtitles file\n                                    puts 'ERROR: Selecting subtitles by filename is not yet supported!'\n                                    exit 1\n                                  end\n    end\n\n    parser.on('-r MULTIPLIER',\n              '--rate MULTIPLIER',\n              '--speed MULTIPLIER',\n              '(Experimental, SLOW) Multiplier for the speed of the',\n              'GIF, where less than 1 indicates a lower speed and',\n              'greater than 1 indicates a faster speed (default 1)') do |r|\n      options[:rate] = r\n    end\n\n    parser.separator ''\n    parser.separator 'Text overlay options (only used if text is defined):'\n\n    parser.on('-T TEXT',\n              '--text TEXT',\n              'Set text to overlay on the GIF (use \"\\n\" for line breaks)') do |p|\n      options[:text] = p\n    end\n\n    parser.on('-C TEXTCOLOR',\n              '--text-color TEXTCOLOR',\n              'Set the color for text overlay (default white)') do |p|\n      options[:textcolor] = p\n    end\n\n    parser.on('-S TEXTSIZE',\n              '--text-size TEXTSIZE',\n              'Set the point size for text overlay (default 30)') do |p|\n      options[:textsize] = p\n    end\n\n    parser.on('-B TEXTBORDER',\n              '--text-border TEXTBORDER',\n              'Set the width of the border for text overlay (default 1)') do |p|\n      options[:textborder] = p\n    end\n\n    parser.on('-F TEXTFONT',\n              '--text-font TEXTFONT',\n              'Set the font name for text overlay (default \"Arial\")') do |p|\n      options[:textfont] = p\n    end\n\n    parser.on('-V TEXTSTYLE',\n              '--text-variant TEXTVARIANT',\n              'Set the font variant for text overlay (default \"Bold\")') do |p|\n      options[:textvariant] = p\n    end\n\n    parser.on('-X TEXTXPOS',\n              '--text-x-position TEXTXPOS',\n              'Set the X position for the text, starting from left (default is center)') do |p|\n      options[:xpos] = p\n    end\n\n    parser.on('-Y TEXTXPOS',\n              '--text-y-position TEXTYPOS',\n              'Set the Y position for the text, starting from top (default is near bottom)') do |p|\n      options[:ypos] = p\n    end\n\n    parser.separator ''\n    parser.separator 'Other options:'\n\n    parser.on_tail('-v', '--verbose', 'Show ffmpeg command executed and output') do |p|\n      options[:verbose] = p\n    end\n\n    parser.on_tail('-q', '--quiet', 'Suppress all log output (overrides verbose)') do |p|\n      options[:quiet] = p\n    end\n\n    parser.on_tail('-h', '--help', 'Show this message') do\n      puts parser\n      exit\n    end\n\n    parser.parse!(args)\n  end\n\n  parser.parse!\n\n  if args.size < 1 || args.size > 2\n    puts 'ERROR: Specify one video to convert at a time!'\n    puts ''\n    puts parser.help\n    exit 1\n  end\n\n  unless File.exists?(args[0])\n    puts \"ERROR: Specified video file does not exist: \#{args[0]}!\"\n    puts ''\n    puts parser.help\n    exit\n  end\n\n  options[:input_filename]  = args[0]\n  options[:output_filename] = if args[1]\n                                args[1].end_with?('.gif') ? args[1] : args[1] + '.gif'\n                              else\n                                File.join(File.dirname(args[0]),\n                                          File.basename(args[0], '.*') + '.gif')\n                              end\n\n  options\nend\n"