Class: Moshy::Bake

Inherits:
Object
  • Object
show all
Defined in:
lib/moshy/bake.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
# File 'lib/moshy/bake.rb', line 3

def cli(args)
	opts = Slop::Options.new
	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.string '-p', '--pframes', 'Makes sure that there are only P-Frames (no B-Frames). Set this true if you plan to mosh your baked file again. Defaults false.'
	opts.integer '-n', '--iframe-interval', 'Ideal interval for I-Frames to be distributed. Set this to a high number (600) if you plan to mosh your baked file again.'

	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
	}

	if @options[:pframes] == "false"
		@options[:pframes] = false
	else
		@options[:pframes] = true
	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[:pframes], @options[:'iframe-interval'], @options[:bitrate]
end

#prep(file, output, pframes, iframe_interval, bitrate) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/moshy/bake.rb', line 45

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

	# Ensures all frames come out as P-frames, B-frames don't
	# dupe or mosh properly
	if pframes
		ffmpeg.add_output_param ['bf', 0]
	end

	# Keyframe interval, sets as few I-frames as possible.
	# ffmpeg will complain about anything over 600 and cap it.
	if iframe_interval
		ffmpeg.add_output_param ['g', iframe_interval.to_s]
	end

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

	ffmpeg.run
end