Class: FFmpegProgress::FFmpeg

Inherits:
Object
  • Object
show all
Includes:
Presets, Utils
Defined in:
lib/ffmpeg_progress/ffmpeg.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/'

Constants included from Utils

Utils::BELL

Constants included from Presets

Presets::BURN_800, Presets::BURN_MKV_SUBS, Presets::DEFAULT_OPTIONS, Presets::WIDTH_800

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#bell, #parse_ffmpeg_time

Constructor Details

#initialize(input_file, option_hash = {}) ⇒ FFmpeg

Creates a new FFmpegProgress::FFmpeg instance.

Parameters:

  • input_file (String)

    the source file name.

  • option_hash (Hash) (defaults to: {})

Options Hash (option_hash):

  • :output (String)

    the output file to write to.

  • :options (String)

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

  • :theme (Hash, Theme)

    either a theme hash or a Theme object.

  • :bell (Boolean)

    whether to play a bell sound upon finishing the task. (See Utils for details.)



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

def initialize(input_file, option_hash = {})
  self.input = input_file

  @options = option_hash.fetch :options, DEFAULT_OPTIONS
  @theme = Theme.from(option_hash.fetch :theme, Theme.new)
  @bell = option_hash.fetch :bell, false

  @output = option_hash.fetch :output, @output
  @duration_ff ||= nil
  @duration ||= nil

  @block = nil

  @pid = nil

  @last_bar = nil
end

Instance Attribute Details

#durationInteger (readonly)

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

Returns:

  • (Integer)


26
27
28
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 26

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)


32
33
34
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 32

def duration_ff
  @duration_ff
end

#inputString

The input file.

Returns:

  • (String)


18
19
20
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 18

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)


37
38
39
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 37

def options
  @options
end

#outputString

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

Returns:

  • (String)


22
23
24
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 22

def output
  @output
end

#pidInteger (readonly)

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

Returns:

  • (Integer)


46
47
48
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 46

def pid
  @pid
end

#themeHash

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

Returns:

  • (Hash)


41
42
43
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 41

def theme
  @theme
end

Instance Method Details

#commandString

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

Returns:

  • (String)


98
99
100
101
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 98

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)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ffmpeg_progress/ffmpeg.rb', line 109

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