33$LOAD_PATH. unshift File . dirname ( __FILE__ )
44
55require 'logger'
6- require 'open3'
76
87require_relative 'ffmpeg/command_args'
98require_relative 'ffmpeg/errors'
3837 end
3938end
4039
41- # The FFMPEG module allows you to customise the behaviour of the FFMPEG library.
40+ # The FFMPEG module allows you to customise the behaviour of the FFMPEG library,
41+ # and provides a set of methods to directly interact with the ffmpeg and ffprobe binaries.
4242#
4343# @example
4444# FFMPEG.logger = Logger.new($stdout)
4545# FFMPEG.io_timeout = 60
46+ # FFMPEG.io_encoding = Encoding::UTF_8
4647# FFMPEG.ffmpeg_binary = '/usr/local/bin/ffmpeg'
4748# FFMPEG.ffprobe_binary = '/usr/local/bin/ffprobe'
4849module FFMPEG
4950 SIGKILL = RUBY_PLATFORM =~ /(win|w)(32|64)$/ ? 1 : 'SIGKILL'
5051
5152 class << self
52- attr_writer :logger , :io_timeout
53+ attr_writer :logger
5354
5455 # Get the FFMPEG logger.
5556 #
@@ -59,14 +60,29 @@ def logger
5960 end
6061
6162 # Get the timeout that's used when waiting for ffmpeg output.
62- # This timeout is used by ffmpeg_execute calls and the Transcoder class.
6363 # Defaults to 30 seconds.
6464 #
6565 # @return [Integer]
6666 def io_timeout
67- return @io_timeout if defined? ( @io_timeout )
67+ FFMPEG ::IO . timeout
68+ end
69+
70+ # Set the timeout that's used when waiting for ffmpeg output.
71+ def io_timeout = ( timeout )
72+ FFMPEG ::IO . timeout = timeout
73+ end
74+
75+ # Get the encoding that's used when reading ffmpeg output.
76+ # Defaults to UTF-8.
77+ #
78+ # @return [Encoding]
79+ def io_encoding
80+ FFMPEG ::IO . encoding
81+ end
6882
69- @io_timeout = 30
83+ # Set the encoding that's used when reading ffmpeg output.
84+ def io_encoding = ( encoding )
85+ FFMPEG ::IO . encoding = encoding
7086 end
7187
7288 # Set the path to the ffmpeg binary.
@@ -93,34 +109,27 @@ def ffmpeg_binary
93109
94110 # Safely captures the standard output and the standard error of the ffmpeg command.
95111 #
96- # @return [Array] The standard output, the standard error, and the process status.
112+ # @param args [Array<String>] The arguments to pass to ffmpeg.
113+ # @return [Array<String, Process::Status>] The standard output, the standard error, and the process status.
97114 def ffmpeg_capture3 ( *args )
98115 logger . debug ( self ) { "ffmpeg -y #{ args . join ( ' ' ) } " }
99- stdout , stderr , status = Open3 . capture3 ( ffmpeg_binary , '-y' , *args )
100- FFMPEG ::IO . encode! ( stdout )
101- FFMPEG ::IO . encode! ( stderr )
102- [ stdout , stderr , status ]
116+ FFMPEG ::IO . capture3 ( ffmpeg_binary , '-y' , *args )
103117 end
104118
105119 # Starts a new ffmpeg process with the given arguments.
106120 # Yields the standard input, the standard output
107121 # and the standard error streams, as well as the child process
108122 # to the specified block.
109123 #
124+ # @param args [Array<String>] The arguments to pass to ffmpeg.
110125 # @yieldparam stdin (+IO+) The standard input stream.
111126 # @yieldparam stdout (+FFMPEG::IO+) The standard output stream.
112127 # @yieldparam stderr (+FFMPEG::IO+) The standard error stream.
113128 # @yieldparam wait_thr (+Thread+) The child process thread.
114- # @return [void ]
115- def ffmpeg_popen3 ( *args , &block )
129+ # @return [Process::Status, Array<IO, Thread> ]
130+ def ffmpeg_popen3 ( *args , &)
116131 logger . debug ( self ) { "ffmpeg -y #{ args . join ( ' ' ) } " }
117- Open3 . popen3 ( ffmpeg_binary , '-y' , *args ) do |stdin , stdout , stderr , wait_thr |
118- block . call ( stdin , FFMPEG ::IO . new ( stdout ) , FFMPEG ::IO . new ( stderr ) , wait_thr )
119- rescue StandardError
120- wait_thr . kill
121- wait_thr . join
122- raise
123- end
132+ FFMPEG ::IO . popen3 ( ffmpeg_binary , '-y' , *args , &)
124133 end
125134
126135 # Execute a ffmpeg command.
@@ -131,12 +140,13 @@ def ffmpeg_popen3(*args, &block)
131140 # @return [Process::Status]
132141 def ffmpeg_execute ( *args , reporters : [ Reporters ::Progress ] )
133142 ffmpeg_popen3 ( *args ) do |_stdin , _stdout , stderr , wait_thr |
134- stderr . each do |line |
143+ stderr . each ( chomp : true ) do |line |
135144 next unless block_given?
136145
137146 reporter = reporters . find { |r | r . match? ( line ) }
138147 reporter ||= Reporters ::Output
139148 report = reporter . new ( line )
149+
140150 yield report
141151 end
142152
@@ -169,35 +179,28 @@ def ffprobe_binary=(path)
169179
170180 # Safely captures the standard output and the standard error of the ffmpeg command.
171181 #
172- # @return [Array] The standard output, the standard error, and the process status.
182+ # @param args [Array<String>] The arguments to pass to ffprobe.
183+ # @return [Array<String, Process::Status>] The standard output, the standard error, and the process status.
173184 # @raise [Errno::ENOENT] If the ffprobe binary cannot be found.
174185 def ffprobe_capture3 ( *args )
175186 logger . debug ( self ) { "ffprobe -y #{ args . join ( ' ' ) } " }
176- stdout , stderr , status = Open3 . capture3 ( ffprobe_binary , '-y' , *args )
177- FFMPEG ::IO . encode! ( stdout )
178- FFMPEG ::IO . encode! ( stderr )
179- [ stdout , stderr , status ]
187+ FFMPEG ::IO . capture3 ( ffprobe_binary , '-y' , *args )
180188 end
181189
182190 # Starts a new ffprobe process with the given arguments.
183191 # Yields the standard input, the standard output
184192 # and the standard error streams, as well as the child process
185193 # to the specified block.
186194 #
195+ # @param args [Array<String>] The arguments to pass to ffprobe.
187196 # @yieldparam stdin (+IO+) The standard input stream.
188197 # @yieldparam stdout (+FFMPEG::IO+) The standard output stream.
189198 # @yieldparam stderr (+FFMPEG::IO+) The standard error stream.
190- # @return [void ]
199+ # @return [Process::Status, Array<IO, Thread> ]
191200 # @raise [Errno::ENOENT] If the ffprobe binary cannot be found.
192- def ffprobe_popen3 ( *args , &block )
201+ def ffprobe_popen3 ( *args , &)
193202 logger . debug ( self ) { "ffprobe -y #{ args . join ( ' ' ) } " }
194- Open3 . popen3 ( ffprobe_binary , '-y' , *args ) do |stdin , stdout , stderr , wait_thr |
195- block . call ( stdin , FFMPEG ::IO . new ( stdout ) , FFMPEG ::IO . new ( stderr ) , wait_thr )
196- rescue StandardError
197- wait_thr . kill
198- wait_thr . join
199- raise
200- end
203+ FFMPEG ::IO . popen3 ( ffprobe_binary , '-y' , *args , &)
201204 end
202205
203206 # Cross-platform way of finding an executable in the $PATH.
0 commit comments