Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created June 21, 2013 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehodge/5829469 to your computer and use it in GitHub Desktop.
Save jamiehodge/5829469 to your computer and use it in GitHub Desktop.
#!/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?
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