Class: FfmpegProgress::Ffmpeg

Inherits:
Object
  • Object
show all
Includes:
Presets, Utils
Defined in:
lib/ffmpeg_progress.rb

Overview

The class that does the actual work.

Constant Summary collapse

LOG_FILE =

The log file for ffmpeg. Progress data will be read from here.

'ffmpeg.log'
DEFAULT_EXT =

The extension for the default output file.

'mp4'
DEFAULT_DIR =

The directory for the default output file.

'converted/'
DEFAULT_THEME =

The default theme, change this with #theme=. FfmpegProgress expects a 256-color capable terminal.

{
  bars: 63, head: '[', full: '=', empty: '-', tail: ']',
  end_fg: 202, full_fg: 214, empty_fg: 202,
  time_fg: 214, block_fg: 214, finish_fg: 40, cancel_fg: 1
}

Constants included from Presets

Presets::BURN_800, Presets::BURN_MKV_SUBS, Presets::DEFAULT_OPTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#colorize, #parse_ffmpeg_time

Constructor Details

#initialize(input_file = nil, ffmpeg_options = DEFAULT_OPTS) ⇒ Ffmpeg

Creates a new FfmpegProgress::Ffmpeg instance.

Parameters:

  • input_file (String) (defaults to: nil)

    the source file name.

  • ffmpeg_options (String) (defaults to: DEFAULT_OPTS)

    the options to pass to ffmpeg. See #options for details and Presets for examples.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ffmpeg_progress.rb', line 117

def initialize(input_file = nil, ffmpeg_options = DEFAULT_OPTS)
  self.input = input_file
  @options = ffmpeg_options

  @output ||= nil
  @duration_ff ||= nil
  @duration ||= nil

  @block = nil

  @pid = nil
  @theme = DEFAULT_THEME

  @last_bar = nil
end

Instance Attribute Details

#durationInteger (readonly)

Returns the duration (in seconds) of the input file.

Returns:

  • (Integer)


89
90
91
# File 'lib/ffmpeg_progress.rb', line 89

def duration
  @duration
end

#duration_ffInteger (readonly)

Returns the duration of the input file as a ffmpeg format string (HH:MM:SS.ms).

Returns:

  • (Integer)


95
96
97
# File 'lib/ffmpeg_progress.rb', line 95

def duration_ff
  @duration_ff
end

#inputString

The input file.

Returns:

  • (String)


81
82
83
# File 'lib/ffmpeg_progress.rb', line 81

def input
  @input
end

#optionsString

The options to pass to ffmpeg. Any occurrences of the string INPUT will be converted to the value of the #input attribute.

Returns:

  • (String)


100
101
102
# File 'lib/ffmpeg_progress.rb', line 100

def options
  @options
end

#outputString

The output file. Directories will be auto-created on execution.

Returns:

  • (String)


85
86
87
# File 'lib/ffmpeg_progress.rb', line 85

def output
  @output
end

#pidInteger (readonly)

The PID of the last spawned ffmpeg process, or nil if none spawned yet.

Returns:

  • (Integer)


109
110
111
# File 'lib/ffmpeg_progress.rb', line 109

def pid
  @pid
end

#themeHash

The theme for the progress bar. See DEFAULT_THEME for details.

Returns:

  • (Hash)


104
105
106
# File 'lib/ffmpeg_progress.rb', line 104

def theme
  @theme
end

Instance Method Details

#commandString

Returns the current ffmpeg command that will be executed by #run.

Returns:

  • (String)


153
154
155
156
# File 'lib/ffmpeg_progress.rb', line 153

def command
  "ffmpeg -i \'#{@input}\' #{@options} \'#{@output}\' &> #{LOG_FILE}"
    .gsub('_INPUT_', "'#{@input}'")
end

#run(&block) ⇒ 0

Run ffmpeg while printing a progress bar to the terminal. If a block is passed, its return value will be attached to the progress bar. The current FfmpegProgress::Ffmpeg instance will be passed to the block.

Returns:

  • (0)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ffmpeg_progress.rb', line 164

def run(&block)
  File.delete(LOG_FILE) if File.exist?(LOG_FILE)

  FileUtils.mkpath(@output.rpartition('/').first) if output.include?('/')

  @pid = spawn command
  @block = block

  sleep 1 until current_time
  monitor_progress

  cleanup

  0
end