Class: Speedrun::Trimmer

Inherits:
Object
  • Object
show all
Defined in:
lib/speedrun/trimmer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file, output_file = nil, noise_threshold: -70,, min_duration: 1.0, quiet: false) ⇒ Trimmer

Returns a new instance of Trimmer.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
# File 'lib/speedrun/trimmer.rb', line 10

def initialize(input_file, output_file = nil, noise_threshold: -70, min_duration: 1.0, quiet: false)
  raise ArgumentError, "File not found: #{input_file}" unless File.exist?(input_file)

  @input_file = input_file
  @output_file = output_file || generate_output_filename(input_file)
  @noise_threshold = noise_threshold
  @min_duration = min_duration
  @dry_run = false
  @quiet = quiet
end

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



8
9
10
# File 'lib/speedrun/trimmer.rb', line 8

def dry_run
  @dry_run
end

#input_fileObject (readonly)

Returns the value of attribute input_file.



7
8
9
# File 'lib/speedrun/trimmer.rb', line 7

def input_file
  @input_file
end

#output_fileObject (readonly)

Returns the value of attribute output_file.



7
8
9
# File 'lib/speedrun/trimmer.rb', line 7

def output_file
  @output_file
end

Instance Method Details

#dry_run?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/speedrun/trimmer.rb', line 21

def dry_run?
  @dry_run
end

#quiet?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/speedrun/trimmer.rb', line 25

def quiet?
  @quiet
end

#runObject



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/speedrun/trimmer.rb', line 29

def run
  output "Input:  #{@input_file}"
  output "Output: #{@output_file}"
  output ""

  # Step 1: Detect freeze regions
  progress_bar = Progress.create(title: "Analyzing video", total: 100, quiet: @quiet)
  freeze_regions = FFmpeg.detect_freezes(
    @input_file,
    noise_threshold: @noise_threshold,
    min_duration: @min_duration,
    quiet: @quiet
  ) do |progress|
    progress_bar&.progress = progress if progress
  end
  progress_bar&.finish

  if freeze_regions.empty?
    output "No freeze regions detected. Copying original video..."
    FileUtils.cp(@input_file, @output_file) unless dry_run?
    return
  end

  # Step 2: Calculate keep regions
  video_duration = FFmpeg.get_duration(@input_file)
  keep_regions = calculate_keep_regions(freeze_regions, video_duration)

  if keep_regions.empty?
    raise "All video content would be removed!"
  end

  # Step 3: Print summary
  print_summary(freeze_regions, keep_regions, video_duration)

  # Step 4: Process or dry-run
  if dry_run?
    output ""
    output "DRY RUN - No files were modified"
  else
    output ""
    progress_bar = Progress.create(title: "Processing video", total: 100, quiet: @quiet)
    FFmpeg.extract_and_concat(@input_file, @output_file, keep_regions, quiet: @quiet) do |progress|
      progress_bar&.progress = progress if progress
    end
    progress_bar&.finish

    output_size = File.size(@output_file)
    output "Complete! Output saved to #{@output_file}"
    output "  File size: #{Formatter.format_filesize(output_size)}"
  end
end