Class: TranscodeVideoJob

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generic_file_id, datastream_in) ⇒ TranscodeVideoJob

Returns a new instance of TranscodeVideoJob.



15
16
17
18
# File 'lib/sufia/jobs/transcode_video_job.rb', line 15

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

Instance Attribute Details

#datastream_inObject

Returns the value of attribute datastream_in.



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

def datastream_in
  @datastream_in
end

#generic_file_idObject

Returns the value of attribute generic_file_id.



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

def generic_file_id
  @generic_file_id
end

Class Method Details

.encode(path, options, output_file) ⇒ Object

TODO tmp file for output



64
65
66
67
68
69
70
71
72
73
# File 'lib/sufia/jobs/transcode_video_job.rb', line 64

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



75
76
77
# File 'lib/sufia/jobs/transcode_video_job.rb', line 75

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

Instance Method Details

#audio_attributesObject



47
48
49
# File 'lib/sufia/jobs/transcode_video_job.rb', line 47

def audio_attributes 
  "-ac 2 -ab 96k -ar 44100"
end

#encode_datastream(dest_dsid, mime_type, options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sufia/jobs/transcode_video_job.rb', line 51

def encode_datastream(dest_dsid, mime_type, options)
  file_suffix = dest_dsid
  out_file = nil
  output_file = Dir::Tmpname.create('sufia'){} + ".#{file_suffix}"
  @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

#encode_mp4Object



37
38
39
40
# File 'lib/sufia/jobs/transcode_video_job.rb', line 37

def encode_mp4
  opts = "#{size_attributes} -b:v 345k -vcodec libx264 -acodec libfaac #{audio_attributes} "
  encode_datastream('mp4', 'video/mp4', opts)
end

#encode_webmObject



32
33
34
35
# File 'lib/sufia/jobs/transcode_video_job.rb', line 32

def encode_webm
  opts = "#{size_attributes}  -b:v 345k -acodec libvorbis #{audio_attributes}"
  encode_datastream('webm', 'video/webm', opts)
end

#queue_nameObject



9
10
11
# File 'lib/sufia/jobs/transcode_video_job.rb', line 9

def queue_name
  :video
end

#runObject



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

def run
  @generic_file = GenericFile.find(generic_file_id)
  @datastream = @generic_file.datastreams[datastream_in]
  if @datastream
    encode_mp4()
    encode_webm()
    @generic_file.save!
  else
    logger.warn "No datastream for transcoding!!!!! pid: #{generic_file_id} dsid: #{datastream_in}"
  end
end

#size_attributesObject



43
44
45
# File 'lib/sufia/jobs/transcode_video_job.rb', line 43

def size_attributes
  "-s 320x240"
end