Module: Speedrun::FFmpeg

Defined in:
lib/speedrun/ffmpeg.rb

Constant Summary collapse

FREEZE_START_PATTERN =
/freeze_start:\s*([\d.]+)/
FREEZE_END_PATTERN =
/freeze_end:\s*([\d.]+)/
DURATION_PATTERN =
/Duration:\s*(\d{2}):(\d{2}):(\d{2}\.\d+)/
TIME_PATTERN =
/time=(\d{2}):(\d{2}):(\d{2}\.\d+)/

Class Method Summary collapse

Class Method Details

.detect_freezes(file_path, noise_threshold: -70,, min_duration: 1.0, quiet: false, &block) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/speedrun/ffmpeg.rb', line 50

def self.detect_freezes(file_path, noise_threshold: -70, min_duration: 1.0, quiet: false, &block)
  raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path)

  cmd = [
    'ffmpeg',
    '-i', file_path,
    '-vf', "freezedetect=n=#{noise_threshold}dB:d=#{min_duration}",
    '-f', 'null',
    '-'
  ]

  output = String.new
  duration = nil

  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
    stdin.close

    stderr.each_line do |line|
      output << line

      # Extract duration from first line that contains it
      if duration.nil? && line =~ DURATION_PATTERN
        duration = Formatter.parse_time("#{$1}:#{$2}:#{$3}")
      end

      # Extract current time and calculate progress
      if duration && block_given? && line =~ TIME_PATTERN
        current_time = Formatter.parse_time("#{$1}:#{$2}:#{$3}")
        progress = [(current_time / duration * 100).round, 100].min
        block.call(progress)
      end
    end

    wait_thr.value # Wait for process to complete
  end

  parse_freezes(output)
end

.extract_and_concat(input_file, output_file, keep_regions, quiet: false, &block) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/speedrun/ffmpeg.rb', line 89

def self.extract_and_concat(input_file, output_file, keep_regions, quiet: false, &block)
  raise ArgumentError, "File not found: #{input_file}" unless File.exist?(input_file)
  raise ArgumentError, "No regions to keep" if keep_regions.empty?

  abs_input = File.absolute_path(input_file)

  Tempfile.create(['concat', '.txt']) do |concat_file|
    keep_regions.each do |start_time, end_time|
      concat_file.puts "file '#{abs_input}'"
      concat_file.puts "inpoint #{start_time}"
      concat_file.puts "outpoint #{end_time}"
    end
    concat_file.flush

    cmd = [
      'ffmpeg',
      '-f', 'concat',
      '-safe', '0',
      '-i', concat_file.path,
      '-c', 'copy',
      '-y',
      output_file
    ]

    output = String.new
    duration = nil
    success = false

    Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr|
      stdin.close

      stderr.each_line do |line|
        output << line

        # Extract duration from first line that contains it
        if duration.nil? && line =~ DURATION_PATTERN
          duration = Formatter.parse_time("#{$1}:#{$2}:#{$3}")
        end

        # Extract current time and calculate progress
        if duration && block_given? && line =~ TIME_PATTERN
          current_time = Formatter.parse_time("#{$1}:#{$2}:#{$3}")
          progress = [(current_time / duration * 100).round, 100].min
          block.call(progress)
        end
      end

      success = wait_thr.value.success?
    end

    raise "FFmpeg failed: #{output}" unless success

    success
  end
end

.get_duration(file_path) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/speedrun/ffmpeg.rb', line 35

def self.get_duration(file_path)
  raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path)

  cmd = [
    'ffprobe',
    '-v', 'error',
    '-show_entries', 'format=duration',
    '-of', 'default=noprint_wrappers=1:nokey=1',
    file_path
  ].shelljoin

  output = `#{cmd}`
  parse_duration(output)
end

.parse_duration(output) ⇒ Object



14
15
16
# File 'lib/speedrun/ffmpeg.rb', line 14

def self.parse_duration(output)
  output.strip.to_f
end

.parse_freezes(output) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/speedrun/ffmpeg.rb', line 18

def self.parse_freezes(output)
  regions = []
  freeze_start = nil

  output.each_line do |line|
    if line =~ FREEZE_START_PATTERN
      freeze_start = ::Regexp.last_match(1).to_f
    elsif line =~ FREEZE_END_PATTERN && freeze_start
      freeze_end = ::Regexp.last_match(1).to_f
      regions << [freeze_start, freeze_end]
      freeze_start = nil
    end
  end

  regions
end