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 = <<~BANNER
      video2gif #{Video2gif::VERSION}

      Usage: video2gif <video> [<output GIF filename>] [options]
    BANNER

    parser.separator ''
    parser.separator 'General GIF options:'

    parser.on('-s SEEK',
              '--seek SEEK',
              'Set time to seek to in the input video (use a count of',
              'seconds or HH:MM:SS.SS format)') do |s|
      options[:seek] = s
    end

    parser.on('-t TIME',
              '--time TIME',
              'Set duration to use from the input video (use a count of',
              'seconds)') do |t|
      options[:time] = t
    end

    parser.on('-f FRAMES',
              '--fps FRAMES',
              'Set frames per second for the resulting GIF (default 10)') do |f|
      options[:fps] = f
    end

    parser.on('-w WIDTH',
              '--width WIDTH',
              'Scale the width of the resulting GIF in pixels (aspect',
              'ratio is preserved, default is 400 pixels)') do |w|
      options[:width] = w
    end

    # parser.on('-hHEIGHT', '--height=HEIGHT', 'Scale the height of the resulting GIF') do |h|
    #   options[:height] = h
    # end

    parser.on('-p PALETTE',
              '--palette PALETTE',
              'Set the palette size of the resulting GIF (maximum of 255',
              'colors)') do |p|
      options[:palette] = p
    end

    parser.on('--palette-mode MODE',
              '--palettemode MODE',
              'Configure a custom palette statistics mode, using either',
              '"full" (single full-frame palette for the whole GIF),',
              '"diff" (single palette emphasizing movement against a',
              'static background across frames), or',
              '"single" (individual palette per frame, adds to file size)',
              '(default "diff")') do |p|
      options[:palettemode] = p
    end

    parser.on('-d [ALGORITHM]',
              '--[no-]dither [ALGORITHM]',
              'Set the dithering algorithm for the palette generation',
              '(default enabled with "floyd_steinberg")') do |d|
      if d.nil?
        options[:dither] = 'floyd_steinberg'
      else
        options[:dither] = d || 'none'
      end
    end

    parser.on('--crop-width SIZE',
              '--crop-size-w SIZE',
              'Pixel size of width to select from source video, before scaling') do |s|
      options[:wregion] = s
    end

    parser.on('--crop-height SIZE',
              '--crop-size-h SIZE',
              'Pixel size of height to select from source video, before scaling') do |s|
      options[:hregion] = s
    end

    parser.on('--crop-x OFFSET',
              '--crop-offset-x OFFSET',
              'Pixel offset from left to select from source video, before scaling') do |o|
      options[:xoffset] = o
    end

    parser.on('--crop-y OFFSET',
              '--crop-offset-y OFFSET',
              'Pixel offset from top to select from source video, before scaling') do |o|
      options[:yoffset] = o
    end

    parser.on('-a [THRESHOLD]',
              '--autocrop [THRESHOLD]',
              'Attempt automatic cropping based on black region, scaled',
              'from 0 (nothing) to 255 (everything), default threshold 24') do |c|
      options[:autocrop] = c || 24
    end

    parser.on('--contrast CONTRAST',
              'Apply contrast adjustment, scaled from -2.0 to 2.0 (default 1)') do |c|
      options[:contrast] = c
      options[:eq] = true
    end

    parser.on('--brightness BRIGHTNESS',
              'Apply brightness adjustment, scaled from -1.0 to 1.0 (default 0)') do |b|
      options[:brightness] = b
      options[:eq] = true
    end

    parser.on('--saturation SATURATION',
              'Apply saturation adjustment, scaled from 0.0 to 3.0 (default 1)') do |s|
      options[:saturation] = s
      options[:eq] = true
    end

    parser.on('--gamma GAMMA',
              'Apply gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|
      options[:gamma] = g
      options[:eq] = true
    end

    parser.on('--red-gamma GAMMA',
              'Apply red channel gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|
      options[:gamma_r] = g
      options[:eq] = true
    end

    parser.on('--green-gamma GAMMA',
              'Apply green channel gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|
      options[:gamma_g] = g
      options[:eq] = true
    end

    parser.on('--blue-gamma GAMMA',
              'Apply blue channel gamma adjustment, scaled from 0.1 to 10.0 (default 1)') do |g|
      options[:gamma_b] = g
      options[:eq] = true
    end

    parser.on('--tonemap [ALGORITHM]',
              'Attempt to force tonemapping from HDR (BT.2020) to SDR',
              '(BT.709) using algorithm (experimental, requires ffmpeg with',
              'libzimg) (default "hable", "mobius" is a good alternative)') do |t|
      options[:tonemap] = t || 'hable'
    end

    parser.on('--subtitles [INDEX]',
              '(Experimental, requires ffprobe) Attempt to use the',
              'subtitles built into the video to overlay text on the',
              'resulting GIF. May be extremely slow for text subtitles.',
              'Takes an optional integer value to choose the subtitle',
              'stream (defaults to the first subtitle stream, index 0)') do |s|
      options[:subtitles] = s || true
      options[:subtitle_index] =  if options[:subtitles].is_a?(TrueClass)  # default to first stream
                                    0
                                  elsif options[:subtitles].match?(/\A\d+\z/)  # select stream by index
                                    options[:subtitles].to_i
                                  elsif options[:subtitles].is_a?(String)  # open subtitles file
                                    puts 'ERROR: Selecting subtitles by filename is not yet supported!'
                                    exit 1
                                  end
    end

    parser.on('-r MULTIPLIER',
              '--rate MULTIPLIER',
              '--speed MULTIPLIER',
              '(Experimental, SLOW) Multiplier for the speed of the',
              'GIF, where less than 1 indicates a lower speed and',
              'greater than 1 indicates a faster speed (default 1)') do |r|
      options[:rate] = r
    end

    parser.separator ''
    parser.separator 'Text overlay options (only used if text is defined):'

    parser.on('-T TEXT',
              '--text TEXT',
              'Set text to overlay on the GIF (use "\n" for line breaks)') do |p|
      options[:text] = p
    end

    parser.on('-C TEXTCOLOR',
              '--text-color TEXTCOLOR',
              'Set the color for text overlay (default white)') do |p|
      options[:textcolor] = p
    end

    parser.on('-S TEXTSIZE',
              '--text-size TEXTSIZE',
              'Set the point size for text overlay (default 30)') do |p|
      options[:textsize] = p
    end

    parser.on('-B TEXTBORDER',
              '--text-border TEXTBORDER',
              'Set the width of the border for text overlay (default 1)') do |p|
      options[:textborder] = p
    end

    parser.on('-F TEXTFONT',
              '--text-font TEXTFONT',
              'Set the font name for text overlay (default "Arial")') do |p|
      options[:textfont] = p
    end

    parser.on('-V TEXTSTYLE',
              '--text-variant TEXTVARIANT',
              'Set the font variant for text overlay (default "Bold")') do |p|
      options[:textvariant] = p
    end

    parser.on('-X TEXTXPOS',
              '--text-x-position TEXTXPOS',
              'Set the X position for the text, starting from left (default is center)') do |p|
      options[:xpos] = p
    end

    parser.on('-Y TEXTXPOS',
              '--text-y-position TEXTYPOS',
              'Set the Y position for the text, starting from top (default is near bottom)') do |p|
      options[:ypos] = p
    end

    parser.separator ''
    parser.separator 'Other options:'

    parser.on_tail('-v', '--verbose', 'Show ffmpeg command executed and output') do |p|
      options[:verbose] = p
    end

    parser.on_tail('-q', '--quiet', 'Suppress all log output (overrides verbose)') do |p|
      options[:quiet] = p
    end

    parser.on_tail('-h', '--help', 'Show this message') do
      puts parser
      exit
    end

    parser.parse!(args)
  end

  parser.parse!

  if args.size < 1 || args.size > 2
    puts 'ERROR: Specify one video to convert at a time!'
    puts ''
    puts parser.help
    exit 1
  end

  unless File.exists?(args[0])
    puts "ERROR: Specified video file does not exist: #{args[0]}!"
    puts ''
    puts parser.help
    exit
  end

  options[:input_filename]  = args[0]
  options[:output_filename] = if args[1]
                                args[1].end_with?('.gif') ? args[1] : args[1] + '.gif'
                              else
                                File.join(File.dirname(args[0]),
                                          File.basename(args[0], '.*') + '.gif')
                              end

  options
end