Class: FFMPEG::Command

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

Overview

Safely executes shell commands with timeout and proper error handling

Examples:

Basic usage

result = Command.run("ffmpeg", "-i", "input.mp4", "-c", "copy", "output.mp4")
result.success? # => true
result.output   # => "..."

With progress callback

Command.run("ffmpeg", "-i", "input.mp4", "output.mp4") do |line|
  # Parse progress from stderr
  if (match = line.match(/time=(\d+:\d+:\d+\.\d+)/))
    puts "Progress: #{match[1]}"
  end
end

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.build_ffmpeg_command(input:, output:, **options) ⇒ Array<String>

Build FFmpeg command with common options

Parameters:

  • input (String)

    input file path

  • output (String)

    output file path

  • options (Hash)

    FFmpeg options

Returns:

  • (Array<String>)


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
# File 'lib/ffmpeg/command.rb', line 116

def build_ffmpeg_command(input:, output:, **options)
  cmd = [FFMPEG.ffmpeg_binary]

  # Overwrite output
  cmd << "-y" if FFMPEG.configuration.overwrite_output

  # Input options (before -i)
  if options[:seek]
    cmd += ["-ss", options[:seek].to_s]
  end

  if options[:duration]
    cmd += ["-t", options[:duration].to_s]
  end

  # Input file
  cmd += ["-i", input]

  # Video codec
  if options[:video_codec]
    cmd += ["-c:v", options[:video_codec]]
  elsif options[:copy_video]
    cmd += ["-c:v", "copy"]
  end

  # Audio codec
  if options[:audio_codec]
    cmd += ["-c:a", options[:audio_codec]]
  elsif options[:copy_audio]
    cmd += ["-c:a", "copy"]
  elsif options[:no_audio]
    cmd << "-an"
  end

  # Video filters
  if options[:video_filters]&.any?
    cmd += ["-vf", options[:video_filters].join(",")]
  end

  # Audio filters
  if options[:audio_filters]&.any?
    cmd += ["-af", options[:audio_filters].join(",")]
  end

  # Resolution
  if options[:resolution]
    cmd += ["-s", options[:resolution]]
  end

  # Frame rate
  if options[:frame_rate]
    cmd += ["-r", options[:frame_rate].to_s]
  end

  # Bitrate
  if options[:video_bitrate]
    cmd += ["-b:v", options[:video_bitrate]]
  end

  if options[:audio_bitrate]
    cmd += ["-b:a", options[:audio_bitrate]]
  end

  # Threads
  threads = options[:threads] || FFMPEG.configuration.threads
  cmd += ["-threads", threads.to_s] if threads.positive?

  # Custom options
  if options[:custom]
    cmd += Array(options[:custom]).flat_map { |opt| opt.split }
  end

  # Output format
  if options[:format]
    cmd += ["-f", options[:format]]
  end

  # Output file
  cmd << output

  cmd
end

.run(*args, timeout: nil, env: {}) {|String| ... } ⇒ Result

Run a command with arguments

Parameters:

  • args (Array<String>)

    command and arguments

  • timeout (Integer, nil) (defaults to: nil)

    timeout in seconds

  • env (Hash) (defaults to: {})

    environment variables

Yields:

  • (String)

    each line of output for progress tracking

Returns:



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
# File 'lib/ffmpeg/command.rb', line 41

def run(*args, timeout: nil, env: {}, &block)
  timeout ||= FFMPEG.configuration.timeout

  log_command(args)

  output = +""
  error = +""
  exit_status = nil

  begin
    Timeout.timeout(timeout) do
      Open3.popen3(env, *args) do |stdin, stdout, stderr, wait_thr|
        stdin.close

        # Read stdout and stderr in threads to avoid blocking
        stdout_thread = Thread.new do
          stdout.each_line do |line|
            output << line
            yield line if block_given?
          end
        end

        stderr_thread = Thread.new do
          stderr.each_line do |line|
            error << line
            yield line if block_given?
          end
        end

        stdout_thread.join
        stderr_thread.join

        exit_status = wait_thr.value
      end
    end
  rescue Timeout::Error
    raise CommandTimeout.new(args.join(" "), timeout)
  end

  log_result(output, error, exit_status&.exitstatus || 1)

  Result.new(
    output: output,
    error: error,
    exit_code: exit_status&.exitstatus || 1
  )
end

.run!(*args, timeout: nil, env: {}) {|String| ... } ⇒ Result

Run a command and raise on failure

Parameters:

  • args (Array<String>)

    command and arguments

  • timeout (Integer, nil) (defaults to: nil)

    timeout in seconds

  • env (Hash) (defaults to: {})

    environment variables

Yields:

  • (String)

    each line of output

Returns:

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ffmpeg/command.rb', line 96

def run!(*args, timeout: nil, env: {}, &block)
  result = run(*args, timeout: timeout, env: env, &block)

  unless result.success?
    raise TranscodingError.new(
      "Command failed with exit code #{result.exit_code}",
      command: args.join(" "),
      output: result.error,
      exit_code: result.exit_code
    )
  end

  result
end