3
3
$LOAD_PATH. unshift File . dirname ( __FILE__ )
4
4
5
5
require 'logger'
6
- require 'open3'
7
6
8
7
require_relative 'ffmpeg/command_args'
9
8
require_relative 'ffmpeg/errors'
38
37
end
39
38
end
40
39
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.
42
42
#
43
43
# @example
44
44
# FFMPEG.logger = Logger.new($stdout)
45
45
# FFMPEG.io_timeout = 60
46
+ # FFMPEG.io_encoding = Encoding::UTF_8
46
47
# FFMPEG.ffmpeg_binary = '/usr/local/bin/ffmpeg'
47
48
# FFMPEG.ffprobe_binary = '/usr/local/bin/ffprobe'
48
49
module FFMPEG
49
50
SIGKILL = RUBY_PLATFORM =~ /(win|w)(32|64)$/ ? 1 : 'SIGKILL'
50
51
51
52
class << self
52
- attr_writer :logger , :io_timeout
53
+ attr_writer :logger
53
54
54
55
# Get the FFMPEG logger.
55
56
#
@@ -59,14 +60,29 @@ def logger
59
60
end
60
61
61
62
# Get the timeout that's used when waiting for ffmpeg output.
62
- # This timeout is used by ffmpeg_execute calls and the Transcoder class.
63
63
# Defaults to 30 seconds.
64
64
#
65
65
# @return [Integer]
66
66
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
68
82
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
70
86
end
71
87
72
88
# Set the path to the ffmpeg binary.
@@ -93,34 +109,27 @@ def ffmpeg_binary
93
109
94
110
# Safely captures the standard output and the standard error of the ffmpeg command.
95
111
#
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.
97
114
def ffmpeg_capture3 ( *args )
98
115
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 )
103
117
end
104
118
105
119
# Starts a new ffmpeg process with the given arguments.
106
120
# Yields the standard input, the standard output
107
121
# and the standard error streams, as well as the child process
108
122
# to the specified block.
109
123
#
124
+ # @param args [Array<String>] The arguments to pass to ffmpeg.
110
125
# @yieldparam stdin (+IO+) The standard input stream.
111
126
# @yieldparam stdout (+FFMPEG::IO+) The standard output stream.
112
127
# @yieldparam stderr (+FFMPEG::IO+) The standard error stream.
113
128
# @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 , &)
116
131
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 , &)
124
133
end
125
134
126
135
# Execute a ffmpeg command.
@@ -131,12 +140,13 @@ def ffmpeg_popen3(*args, &block)
131
140
# @return [Process::Status]
132
141
def ffmpeg_execute ( *args , reporters : [ Reporters ::Progress ] )
133
142
ffmpeg_popen3 ( *args ) do |_stdin , _stdout , stderr , wait_thr |
134
- stderr . each do |line |
143
+ stderr . each ( chomp : true ) do |line |
135
144
next unless block_given?
136
145
137
146
reporter = reporters . find { |r | r . match? ( line ) }
138
147
reporter ||= Reporters ::Output
139
148
report = reporter . new ( line )
149
+
140
150
yield report
141
151
end
142
152
@@ -169,35 +179,28 @@ def ffprobe_binary=(path)
169
179
170
180
# Safely captures the standard output and the standard error of the ffmpeg command.
171
181
#
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.
173
184
# @raise [Errno::ENOENT] If the ffprobe binary cannot be found.
174
185
def ffprobe_capture3 ( *args )
175
186
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 )
180
188
end
181
189
182
190
# Starts a new ffprobe process with the given arguments.
183
191
# Yields the standard input, the standard output
184
192
# and the standard error streams, as well as the child process
185
193
# to the specified block.
186
194
#
195
+ # @param args [Array<String>] The arguments to pass to ffprobe.
187
196
# @yieldparam stdin (+IO+) The standard input stream.
188
197
# @yieldparam stdout (+FFMPEG::IO+) The standard output stream.
189
198
# @yieldparam stderr (+FFMPEG::IO+) The standard error stream.
190
- # @return [void ]
199
+ # @return [Process::Status, Array<IO, Thread> ]
191
200
# @raise [Errno::ENOENT] If the ffprobe binary cannot be found.
192
- def ffprobe_popen3 ( *args , &block )
201
+ def ffprobe_popen3 ( *args , &)
193
202
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 , &)
201
204
end
202
205
203
206
# Cross-platform way of finding an executable in the $PATH.
0 commit comments