Class: FFMPEG::Command
- Inherits:
-
Object
- Object
- FFMPEG::Command
- Defined in:
- lib/ffmpeg/command.rb
Overview
Safely executes shell commands with timeout and proper error handling
Defined Under Namespace
Classes: Result
Class Method Summary collapse
-
.build_ffmpeg_command(input:, output:, **options) ⇒ Array<String>
Build FFmpeg command with common options.
-
.run(*args, timeout: nil, env: {}) {|String| ... } ⇒ Result
Run a command with arguments.
-
.run!(*args, timeout: nil, env: {}) {|String| ... } ⇒ Result
Run a command and raise on failure.
Class Method Details
.build_ffmpeg_command(input:, output:, **options) ⇒ Array<String>
Build FFmpeg command with common options
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:, **) cmd = [FFMPEG.ffmpeg_binary] # Overwrite output cmd << "-y" if FFMPEG.configuration.overwrite_output # Input options (before -i) if [:seek] cmd += ["-ss", [:seek].to_s] end if [:duration] cmd += ["-t", [:duration].to_s] end # Input file cmd += ["-i", input] # Video codec if [:video_codec] cmd += ["-c:v", [:video_codec]] elsif [:copy_video] cmd += ["-c:v", "copy"] end # Audio codec if [:audio_codec] cmd += ["-c:a", [:audio_codec]] elsif [:copy_audio] cmd += ["-c:a", "copy"] elsif [:no_audio] cmd << "-an" end # Video filters if [:video_filters]&.any? cmd += ["-vf", [:video_filters].join(",")] end # Audio filters if [:audio_filters]&.any? cmd += ["-af", [:audio_filters].join(",")] end # Resolution if [:resolution] cmd += ["-s", [:resolution]] end # Frame rate if [:frame_rate] cmd += ["-r", [:frame_rate].to_s] end # Bitrate if [:video_bitrate] cmd += ["-b:v", [:video_bitrate]] end if [:audio_bitrate] cmd += ["-b:a", [:audio_bitrate]] end # Threads threads = [:threads] || FFMPEG.configuration.threads cmd += ["-threads", threads.to_s] if threads.positive? # Custom options if [:custom] cmd += Array([:custom]).flat_map { |opt| opt.split } end # Output format if [:format] cmd += ["-f", [:format]] end # Output file cmd << output cmd end |
.run(*args, timeout: nil, env: {}) {|String| ... } ⇒ Result
Run a command with arguments
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
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 |