Created
June 21, 2013 07:19
-
-
Save jamiehodge/5829469 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'media' | |
$0 = '0.0' | |
converter = | |
Media.convert do | |
options y: true | |
input '/path/to/input.mov' | |
output '/path/to/output.png' do | |
options ss: 0, 'frames:v' => 1 | |
graph do | |
chain do | |
filter 'thumbnail' | |
filter 'scale' do | |
expressions 'iw*sar', 'ih' | |
end | |
end | |
end | |
end | |
end | |
result = converter.call {|progress| $0 = progress.to_s } | |
$stdout << result.out | |
$stderr << result.error | |
$0 = '1.0' if result.success? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Subprocess | |
extend Forwardable | |
attr_reader :out, :error, :wait_thread | |
def_delegators :status, :success?, :exitstatus | |
def initialize(command) | |
_, @stdout, @stderr, @wait_thread = Open3.popen3(*command) | |
@out = '' | |
@error = '' | |
end | |
def perform(timeout: 1) | |
while wait_thread.alive? | |
select(timeout: timeout) | |
yield progress if block_given? | |
end | |
self | |
end | |
private | |
def_delegators :wait_thread, :pid | |
def progress | |
ps[/(?<=\n).+/] | |
end | |
def ps | |
`ps -p #{pid} -o comm` | |
end | |
def status | |
wait_thread.value | |
end | |
def select(timeout:, buffer: 65536) | |
if IO.select([@stdout, @stderr], nil, nil, timeout) | |
out << @stdout.read_nonblock(buffer) | |
error << @stderr.read_nonblock(buffer) | |
end | |
rescue EOFError, IO::WaitReadable | |
'' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment