Class: TranscodingMachine::Server::Transcoder

Inherits:
Object
  • Object
show all
Defined in:
lib/transcoding_machine/server/transcoder.rb

Constant Summary collapse

@@options =
{:work_directory => '/tmp', :storage => FileStorage.new}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file_name, media_formats, event_handler = nil, storage_options = {}) ⇒ Transcoder

Returns a new instance of Transcoder.



20
21
22
23
24
25
# File 'lib/transcoding_machine/server/transcoder.rb', line 20

def initialize(source_file_name, media_formats, event_handler = nil, storage_options = {})
  @source_file_name = source_file_name
  @event_handler = event_handler
  @media_formats = media_formats
  @storage_options = storage_options
end

Class Method Details

.optionsObject



8
9
10
# File 'lib/transcoding_machine/server/transcoder.rb', line 8

def self.options
  @@options
end

Instance Method Details

#analyze_source_file(media_players) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/transcoding_machine/server/transcoder.rb', line 63

def analyze_source_file(media_players)
  get_source_file
  @event_handler.analyzing_source_file if @event_handler

  source_file_attributes = MediaFileAttributes.new(source_file_path)

  source_media_format = @media_formats.find {|mf| mf.matches(source_file_attributes)}

  target_media_formats = media_players.map {|mp| mp.best_format_for(source_file_attributes)}.compact.uniq
  target_media_formats -= [source_media_format]
  
  @event_handler.analyzed_source_file(source_file_attributes, source_media_format, target_media_formats) if @event_handler
  
  thumbnail_file_path = generate_thumbnail(source_file_attributes)
  storage.put_thumbnail_file(thumbnail_file_path, @source_file_name, @storage_options) if thumbnail_file_path
  
  [source_file_attributes, source_media_format, target_media_formats]
end

#clearObject



126
127
128
# File 'lib/transcoding_machine/server/transcoder.rb', line 126

def clear
  FileUtils.rm(source_file_path)
end

#command_for(source_file_attributes, media_format, destination_file_path) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/transcoding_machine/server/transcoder.rb', line 130

def command_for(source_file_attributes, media_format, destination_file_path)
  command_variables = {
    'in_file_name' => "\"#{source_file_path}\"",
    'in_directory' => "#{source_file_directory}",
    'out_file_name' => "\"#{destination_file_path}\"",
    'out_directory' => "#{source_file_directory}"
  }

  command_variables['fps'] = target_fps(source_file_attributes) if source_file_attributes.video_fps

  cmd = media_format.command.strip

  command_variables.each {|key, value| cmd.gsub!("{{#{key}}}", value.to_s) }

  cmd
end

#destination_file_name(media_format) ⇒ Object



35
36
37
# File 'lib/transcoding_machine/server/transcoder.rb', line 35

def destination_file_name(media_format)
  @source_file_name + media_format.suffix
end

#destination_file_path(media_format) ⇒ Object



39
40
41
# File 'lib/transcoding_machine/server/transcoder.rb', line 39

def destination_file_path(media_format)
  File.expand_path(destination_file_name(media_format), work_directory)
end

#generate_thumbnail(source_file_attributes) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/transcoding_machine/server/transcoder.rb', line 82

def generate_thumbnail(source_file_attributes)
  if source_file_attributes.video?
    @event_handler.generating_thumbnail_file if @event_handler
    file = source_file_attributes.thumbnail_file
    @event_handler.generated_thumbnail_file if @event_handler
    file
  else
    nil
  end
end

#get_source_fileObject



54
55
56
57
58
59
60
61
# File 'lib/transcoding_machine/server/transcoder.rb', line 54

def get_source_file
  unless has_source_file?
    prepare_working_directory
    @event_handler.getting_source_file if @event_handler
    storage.get_file(@source_file_name, source_file_path, @storage_options)
    @event_handler.got_source_file if @event_handler
  end
end

#has_source_file?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/transcoding_machine/server/transcoder.rb', line 43

def has_source_file?
  File.exist?(source_file_path)
end

#prepare_working_directoryObject



47
48
49
50
51
52
# File 'lib/transcoding_machine/server/transcoder.rb', line 47

def prepare_working_directory
  if !File.exist?(source_file_directory)
    puts "creating directory #{source_file_directory}"
    FileUtils.mkdir_p(source_file_directory)
  end
end

#put_destination_file(file_path, media_format) ⇒ Object



119
120
121
122
123
124
# File 'lib/transcoding_machine/server/transcoder.rb', line 119

def put_destination_file(file_path, media_format)
  dst_name = destination_file_name(media_format)
  @event_handler.putting_destination_file(dst_name, media_format) if @event_handler
  storage.put_file(destination_file_path(media_format), dst_name, media_format, @storage_options)
  @event_handler.put_destination_file(dst_name, media_format) if @event_handler
end

#source_file_directoryObject



31
32
33
# File 'lib/transcoding_machine/server/transcoder.rb', line 31

def source_file_directory
  @source_file_directory ||= File.dirname(source_file_path)
end

#source_file_pathObject



27
28
29
# File 'lib/transcoding_machine/server/transcoder.rb', line 27

def source_file_path
  @source_file_path ||= File.expand_path(@source_file_name, work_directory)
end

#storageObject



16
17
18
# File 'lib/transcoding_machine/server/transcoder.rb', line 16

def storage
  self.class.options[:storage]
end

#target_fps(source_file_attributes) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/transcoding_machine/server/transcoder.rb', line 147

def target_fps(source_file_attributes)
  if fps = source_file_attributes.video_fps
    fps > 30 ? 25 : fps
  else
    nil
  end
end

#transcode(source_file_attributes, media_format) ⇒ Object



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
# File 'lib/transcoding_machine/server/transcoder.rb', line 93

def transcode(source_file_attributes, media_format)
  get_source_file
  media_format = @media_formats[media_format] unless media_format.is_a?(MediaFormat)
  @event_handler.transcoding(media_format) if @event_handler

  dst_file_path = destination_file_path(media_format)
  cmd = command_for(source_file_attributes, media_format, dst_file_path)
  commands = cmd.split(' && ')
  puts "Number of commands to run: #{commands.size}"

  commands.each do |command|
    puts "Running command: #{command}"
    result = begin
      timeout(1000 * 60 * 55) do
        puts IO.popen(command).read
      end
    rescue Timeout::Error => e
      puts "TIMEOUT REACHED WHEN RUNNING COMMAND"
    end
  end

  @event_handler.transcoded(media_format) if @event_handler
  put_destination_file(dst_file_path, media_format)
  dst_file_path
end

#work_directoryObject



12
13
14
# File 'lib/transcoding_machine/server/transcoder.rb', line 12

def work_directory
  self.class.options[:work_directory]
end