Class: FfmpegTranscodeJob

Inherits:
Object
  • Object
show all
Extended by:
Open3
Defined in:
lib/sufia/jobs/ffmpeg_transcode_job.rb

Direct Known Subclasses

TranscodeAudioJob, TranscodeVideoJob

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generic_file_id, datastream_in) ⇒ FfmpegTranscodeJob

Returns a new instance of FfmpegTranscodeJob.



12
13
14
15
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 12

def initialize(generic_file_id, datastream_in)
  self.generic_file_id = generic_file_id
  self.datastream_in = datastream_in
end

Instance Attribute Details

#datastreamObject

Returns the value of attribute datastream.



10
11
12
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 10

def datastream
  @datastream
end

#datastream_inObject

Returns the value of attribute datastream_in.



10
11
12
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 10

def datastream_in
  @datastream_in
end

#generic_fileObject

Returns the value of attribute generic_file.



10
11
12
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 10

def generic_file
  @generic_file
end

#generic_file_idObject

Returns the value of attribute generic_file_id.



10
11
12
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 10

def generic_file_id
  @generic_file_id
end

Class Method Details

.encode(path, options, output_file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 45

def self.encode(path, options, output_file)
  command = "#{ffmpeg_path} -y -i \"#{path}\" #{options} #{output_file}"
  stdin, stdout, stderr, wait_thr = popen3(command)
  stdin.close
  out = stdout.read
  stdout.close
  err = stderr.read
  stderr.close
  raise "Unable to execute command \"#{command}\"\n#{err}" unless wait_thr.value.success?
end

.ffmpeg_pathObject



56
57
58
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 56

def self.ffmpeg_path
  Sufia::Engine.config.ffmpeg_path
end

Instance Method Details

#encode_datastream(dest_dsid, mime_type, options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 33

def encode_datastream(dest_dsid, mime_type, options)
  file_suffix = dest_dsid
  out_file = nil
  output_file = Dir::Tmpname.create(['sufia', ".#{file_suffix}"], Sufia::Engine.config.temp_file_base){}
  datastream.to_tempfile do |f|
    self.class.encode(f.path, options, output_file)
  end
  out_file = File.open(output_file, "rb")
  generic_file.add_file_datastream(out_file.read, :dsid=>dest_dsid, :mimeType=>mime_type)
  File.unlink(output_file)
end

#processObject



17
18
19
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 17

def process
  raise "You attempted to call process() on an abstract class.  Implement process() on the concrete class"
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sufia/jobs/ffmpeg_transcode_job.rb', line 21

def run
  return unless Sufia::Engine.config.enable_ffmpeg
  self.generic_file = GenericFile.find(generic_file_id)
  self.datastream = generic_file.datastreams[datastream_in]
  if datastream
    process 
    generic_file.save!
  else
    logger.warn "No datastream for transcoding!!!!! pid: #{generic_file_id} dsid: #{datastream_in}"
  end
end