Class: Eikon::VideoProcessor

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/eikon/video_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ VideoProcessor

Returns a new instance of VideoProcessor.



14
15
16
# File 'lib/eikon/video_processor.rb', line 14

def initialize(file_name)
  @file_name = T.let(file_name, String)
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



11
12
13
# File 'lib/eikon/video_processor.rb', line 11

def file_name
  @file_name
end

Instance Method Details

#get_frames_dhash(folder_path) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/eikon/video_processor.rb', line 78

def get_frames_dhash(folder_path)
  # Filter the '.' and '..' directories from the current directory's file list
  file_names = Dir.entries(folder_path).filter { |entry| entry.start_with?(".") == false }

  dhashes = file_names.map do |file_name|
    dhash = Eikon.dhash_for_image("#{folder_path}/#{file_name}")
    { file_name: file_name, dhash: dhash }
  end
  dhashes
end

#split_video_into_images(number_of_frames = 0) ⇒ Object



20
21
22
23
24
25
26
27
28
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
# File 'lib/eikon/video_processor.rb', line 20

def split_video_into_images(number_of_frames = 0)
  file_name = get_file_name(@file_name)

  setup_tmp_folder

  output_folder_path = "tmp/videos/#{file_name}_#{Time.now.utc.strftime("%Y%m%d%H%M%S")}"
  begin
    make_dir_command = Terrapin::CommandLine.new("mkdir", ":folder_name")
    make_dir_command.run(folder_name: output_folder_path)
  rescue Terrapin::ExitStatusError
    raise "Folder already exists for this export"
  end

  # Get length of video
  line = Terrapin::CommandLine.new("ffmpeg", "-i :file_name 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//")
  time = line.run(file_name: @file_name).chomp

  # Turn the time into total seconds (with two points of precision)
  time_parts =  time.split(":")
  total_time =  time_parts[0].to_i * 60 * 60 # Hours
  total_time += time_parts[1].to_i * 60 # Minutes
  total_time += time_parts[2].to_f # Seconds

  # Figure out the number of frames per minute given the number of frames we want, default to every ten seconds out of sixty
  if number_of_frames > 2
    # number_of_frames = number_of_frames - 2
    # number_of_frames = 0 if number_of_frames.negative?

    # NOTE: This has to be a fraction, not a whole number, but also, never 0
    fps = (number_of_frames - 2) / total_time # We subtract two because we're taking the start and end
    line = Terrapin::CommandLine.new("ffmpeg", "-i :file_name -vf fps=#{fps} :folder_name")
    line.run(file_name: @file_name, folder_name: "#{output_folder_path}/#{file_name}_%05d.png")
  else
    line = Terrapin::CommandLine.new("ffmpeg", "-i :file_name -filter:v \"select='gt(scene,0.4)',showinfo\" -vsync 0 :output_directory/:output_file_name")
    line.run(file_name: @file_name, output_directory: output_folder_path, output_file_name: "#{file_name}_%05d.png")
  end

  # Screenshot the second second and second to last second
  # ffmpeg -ss 01:23:45 -i input -frames:v 1 -q:v 2 output.jpg
  last_time = "#{time_parts[0]}:#{time_parts[1]}:#{(time_parts[2].to_f - 1).abs.floor}"
  ffmpeg_command = "-ss 00:00:02 -i :file_name " +
                   "-ss :last_time -i :file_name " +
                   "-map 0:v -vframes 1 :output_directory/:output_file_name_start " +
                   "-map 1:v -vframes 1 :output_directory/:output_file_name_end"
  line = Terrapin::CommandLine.new("ffmpeg", ffmpeg_command)
  line.run(
    file_name: @file_name,
    last_time: last_time,
    output_directory: output_folder_path,
    output_file_name_start: "#{file_name}_%05d.png",
    output_file_name_end: "#{file_name}_%05d.png"
  )
  remove_blank_shots(output_folder_path)

  output_folder_path
end