Module: Video2gif::CLI

Defined in:
lib/video2gif/cli.rb

Class Method Summary collapse

Class Method Details

.startObject



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
# File 'lib/video2gif/cli.rb', line 11

def self.start
  unless Utils.is_executable?('ffmpeg')
    puts 'ERROR: Requires FFmpeg to be installed!'
    exit 1
  end

  unless Utils.is_executable?('ffprobe')
    puts 'ERROR: Requires FFmpeg utils to be installed (for ffprobe)!'
    exit 1
  end

  logger = Logger.new(STDOUT)
  options = Options.parse(ARGV)

  lines = []

  Open3.popen2e(*FFmpeg.ffprobe_command(options, logger)) do |stdin, stdout_stderr, thread|
    stdin.close
    stdout_stderr.each do |line|
      logger.info(line.chomp) if options[:verbose] unless options[:quiet]
      lines << line
    end
    stdout_stderr.close

    unless thread.value.success?
      # TODO: more info, output lines with errors?
      raise "Process #{thread.pid} failed! Try again with --verbose to see error."
    end
  end

  options[:probe_infos] = JSON.parse(lines.join, symbolize_names: true)

  if options[:subtitles] && !options[:probe_infos][:streams].any? { |s| s[:codec_type] == 'subtitle' }
    logger.warn('Could not find subtitles in the file, they will be omitted') unless options[:quiet]
  end

  if options[:autocrop]
    Open3.popen2e(*FFmpeg.cropdetect_command(options, logger)) do |stdin, stdout_stderr, thread|
      stdin.close
      stdout_stderr.each do |line|
        logger.info(line.chomp) if options[:verbose] unless options[:quiet]
        if line.include?('Parsed_cropdetect')
          options[:autocrop] = line.match(FFmpeg::CROP_REGEX)
        end
      end
      stdout_stderr.close

      unless thread.value.success?
        # TODO: more info, output lines with errors?
        raise "Process #{thread.pid} failed! Try again with --verbose to see error."
      end
    end
  end

  Open3.popen2e(*FFmpeg.gif_command(options, logger)) do |stdin, stdout_stderr, thread|
    stdin.close
    stdout_stderr.each do |line|
      logger.info(line.chomp) if options[:verbose] unless options[:quiet]
    end
    stdout_stderr.close

    unless thread.value.success?
      # TODO: more info, output lines with errors?
      raise "Process #{thread.pid} failed! Try again with --verbose to see error."
    end
  end
end