Class: Moshy::Prep

Inherits:
Object
  • Object
show all
Defined in:
lib/moshy/prep.rb

Instance Method Summary collapse

Instance Method Details

#cli(args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moshy/prep.rb', line 3

def cli(args)
  opts = Slop::Options.new
  opts.banner = "Usage: moshy.rb -m prep -i <file> -o <output> [options]\n"
  opts.separator 'Required Parameters:'
  opts.string '-i', '--input', 'Input file path - can be anything that ffmpeg supports.'
  opts.string '-o', '--output', 'File output path - should end in .avi.'
  opts.separator 'Optional Parameters:'
  opts.integer '-b', '--bitrate', 'Bitrate amount (kb/s). Defaults to 4196. Larger number means higher quality, but larger size.'
  opts.on '-h', '--help' do
    puts opts
    puts "\n"
    puts \
"Preps a video file for datamoshing with moshy by converting it into an
AVI with no B-Frames (they're not good for moshing), and placing as
few I-Frames as possible. Requires ffmpeg be installed locally. Check
the repository's README.md for more information on how to install ffmpeg.

This command is meant to be a simple one-liner that makes your datamoshing
workflow faster. Under the covers, it runs the following ffmpeg command:

ffmpeg -i <moshy input> -bf 0 -g 600 -b:v <moshy bitrate> -o <moshy output>

This takes in an input file (it should theoretically work with any video
file type that ffmpeg supports), makes sure that no B-Frames are rendered,
and sets the ideal I-frame interval to 600 (ffmpeg's max). This seems to
mean that an I-frame will only show up every 30 to 25 seconds
(600f / 30fps = 20s or 600f / 24fps = 25s), but I-Frames must be deposited
wherever there is a hard cut or transition in a video where a P-frame would
not be able to properly predict the motion of pixels."
    exit
  end

  default = {
    :bitrate => 4196
  }

  parser = Slop::Parser.new(opts)
  slop_options = parser.parse(ARGV)
  @options = default.merge(slop_options) { |key, oldval, newval|
    if newval.nil?
      oldval
    else
      newval
    end
  }

  # Check mandatory params

  mandatory = [:input, :output]
  missing = mandatory.select{ |param| @options[param].nil? }
  unless missing.empty?
    puts "Missing options: #{missing.join(', ')}"
    puts slop_options
    exit
  end

  prep @options[:input], @options[:output], @options[:bitrate]
end

#prep(input, output, bitrate) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/moshy/prep.rb', line 61

def prep(input, output, bitrate)
  ffmpeg = Av::Commands::Ffmpeg.new
  ffmpeg.add_source input
  ffmpeg.add_destination output

  # Ensures all frames come out as P-frames, B-frames don't

  # dupe or mosh properly

  ffmpeg.add_output_param ['bf', 0]
  # Keyframe interval, sets as few I-frames as possible.

  # ffmpeg will complain about anything over 600 and cap it.

  ffmpeg.add_output_param ['g', 600]
  # Bitrate

  ffmpeg.add_output_param ['b:v', bitrate.to_s + 'k']

  ffmpeg.run
end