Class: TranscodingMachine::Server::Transcoder
- Inherits:
-
Object
- Object
- TranscodingMachine::Server::Transcoder
- 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
- #analyze_source_file ⇒ Object
- #clear ⇒ Object
- #command_for(source_file_attributes, media_format, destination_file_path) ⇒ Object
- #destination_file_name(media_format) ⇒ Object
- #destination_file_path(media_format) ⇒ Object
- #generate_thumbnail(source_file_attributes) ⇒ Object
- #get_source_file ⇒ Object
-
#initialize(source_file_name, media_players, media_formats, event_handler = nil, storage_options = {}) ⇒ Transcoder
constructor
A new instance of Transcoder.
- #prepare_working_directory ⇒ Object
- #put_destination_file(file_path, media_format) ⇒ Object
- #source_file_directory ⇒ Object
- #source_file_path ⇒ Object
- #start ⇒ Object
- #storage ⇒ Object
- #target_fps(source_file_attributes) ⇒ Object
- #transcode(source_file_attributes, media_format) ⇒ Object
- #work_directory ⇒ Object
Constructor Details
#initialize(source_file_name, media_players, media_formats, event_handler = nil, storage_options = {}) ⇒ Transcoder
Returns a new instance of Transcoder.
20 21 22 23 24 25 26 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 20 def initialize(source_file_name, media_players, media_formats, event_handler = nil, = {}) @source_file_name = source_file_name @event_handler = event_handler @media_players = media_players @media_formats = media_formats @storage_options = end |
Class Method Details
.options ⇒ Object
8 9 10 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 8 def self. @@options end |
Instance Method Details
#analyze_source_file ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 72 def analyze_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 [source_file_attributes, source_media_format, target_media_formats] end |
#clear ⇒ Object
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
36 37 38 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 36 def destination_file_name(media_format) @source_file_name + media_format.suffix end |
#destination_file_path(media_format) ⇒ Object
40 41 42 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 40 def destination_file_path(media_format) File.(destination_file_name(media_format), work_directory) end |
#generate_thumbnail(source_file_attributes) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 86 def generate_thumbnail(source_file_attributes) if source_file_attributes.video? @event_handler. if @event_handler file = source_file_attributes.thumbnail_file @event_handler.generated_thumbnail_file if @event_handler file else nil end end |
#get_source_file ⇒ Object
66 67 68 69 70 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 66 def get_source_file @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 |
#prepare_working_directory ⇒ Object
59 60 61 62 63 64 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 59 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
120 121 122 123 124 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 120 def put_destination_file(file_path, media_format) @event_handler.putting_destination_file(file_path, media_format) if @event_handler storage.put_file(destination_file_path(media_format), destination_file_name(media_format), media_format, @storage_options) @event_handler.put_destination_file(file_path, media_format) if @event_handler end |
#source_file_directory ⇒ Object
32 33 34 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 32 def source_file_directory @source_file_directory ||= File.dirname(source_file_path) end |
#source_file_path ⇒ Object
28 29 30 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 28 def source_file_path @source_file_path ||= File.(@source_file_name, work_directory) end |
#start ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 44 def start prepare_working_directory get_source_file source_file_attributes, source_media_format, target_media_formats = analyze_source_file thumbnail_file_path = generate_thumbnail(source_file_attributes) storage.put_thumbnail_file(thumbnail_file_path, @source_file_name, @storage_options) if thumbnail_file_path target_media_formats.each do |media_format| destination_file_path = transcode(source_file_attributes, media_format) put_destination_file(destination_file_path, media_format) end clear end |
#storage ⇒ Object
16 17 18 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 16 def storage self.class.[: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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 97 def transcode(source_file_attributes, media_format) @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 dst_file_path end |
#work_directory ⇒ Object
12 13 14 |
# File 'lib/transcoding_machine/server/transcoder.rb', line 12 def work_directory self.class.[:work_directory] end |