Skip to content

Instantly share code, notes, and snippets.

@ucnv
Last active December 21, 2015 01:39
Show Gist options
  • Save ucnv/6229377 to your computer and use it in GitHub Desktop.
Save ucnv/6229377 to your computer and use it in GitHub Desktop.
require 'fileutils'
require 'tmpdir'
require 'cocaine'
require 'aviglitch'
infile = ARGV.shift
skip = (ARGV.shift || 1).to_i
outfile = './out.avi'
info = Cocaine::CommandLine.new('ffmpeg', '-i :in 2>&1', :expected_outcodes => [0, 1]).run(:in => infile)
tbc = info.match /(¥d+) tbc/
if tbc.nil?
puts "Usage: ruby %s <infile>" % $0
exit 1
end
fps = (2 * tbc[1].to_i).to_s
audio, video1, video2 = ()
audiom = info.scan /Stream #([¥d:]+): Audio:/
unless audiom.empty?
audio = audiom[0][0]
end
videom = info.scan /Stream #([¥d:]+): Video:/
unless videom.empty?
video1 = videom[0][0]
video2 = videom[1][0]
end
Dir.mktmpdir do |dir|
audioout = "%s/audio.wav" % dir
videoout = "%s/video.avi" % dir
video1out = "%s/video1.avi" % dir
video2out = "%s/video2.avi" % dir
cmd = Cocaine::CommandLine.new 'ffmpeg', '-i :in -map :map -r :fps -codec copy -y :out'
cmd.run :in => infile, :map => audio, :fps => fps, :out => audioout
cmd.run :in => infile, :map => video1, :fps => fps, :out => video1out
cmd.run :in => infile, :map => video2, :fps => fps, :out => video2out
va = AviGlitch.open video1out
vb = AviGlitch.open video2out
vbase = AviGlitch.open video2out # AviGlitch::Frames must has #clone method
vbase.glitch_with_index do |d, i|
pos = (i / skip).to_i
p = pos * skip
p += 1 unless p % 2 == 0
t = pos % 2 == 0 ? va : vb
t.frames[p].data
end
vbase.output videoout
cmd = Cocaine::CommandLine.new 'ffmpeg', '-i :video -i :audio -codec copy -y :out'
cmd.run :audio => audioout, :video => videoout, :out => outfile
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment