Module: FlyAdmin::Convertable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/fly_admin/concerns/convertable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PENDING_STATE =

Constants

0
CONVERTED_STATE =
1
FAILURE_STATE =
2
BEFORE_SPAWNLING =
3
WEBM_LOW_MASK =
0b0001
WEBM_HIGH_MASK =
0b0010
MP4_LOW_MASK =
0b0100
MP4_HIGH_MASK =
0b1000
FORMAT =
{WEBM_LOW_MASK => 'webm 360p', WEBM_HIGH_MASK => 'webm 720p', MP4_LOW_MASK => 'mp4 360p', MP4_HIGH_MASK => 'mp4 720p'}
STATE =
{0 => 'pending', 1 => 'converted', 2 => 'failure', 3 => 'before spawnling'}

Instance Method Summary collapse

Instance Method Details

#alter_high_content_urlObject



119
120
121
# File 'app/models/fly_admin/concerns/convertable.rb', line 119

def alter_high_content_url
  self.url + 'high/movie.webm'
end

#alter_low_content_urlObject



115
116
117
# File 'app/models/fly_admin/concerns/convertable.rb', line 115

def alter_low_content_url
  self.url + 'low/movie.webm'
end

#content_pathObject



99
100
101
# File 'app/models/fly_admin/concerns/convertable.rb', line 99

def content_path
  path + self.content
end

#convert(options, outfile, mask) ⇒ Object



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
# File 'app/models/fly_admin/concerns/convertable.rb', line 36

def convert(options, outfile, mask)
  log_file = "#{Rails.root}/log/ffmpeg_convertation.log"
  pid_file = "#{Rails.root}/tmp/pids/ffmpeg_pids.pid"
  cmd = "ffmpeg -i #{full_source_path.shellescape} #{options} -threads 0 -y #{self.path + outfile}"# >> #{logfile} 2>&1"
  fork do
    Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
      CONVERTATION_LOG.info "video_#{id} #{FORMAT[mask]} started"
      pid = wait_thr.pid
      File.open(pid_file, "a") { |f| f.puts("video_#{id} with pid: #{pid}") }
      captured_stdout = stdout.read
      captured_stderr = stderr.read
      exit_status = wait_thr.value
      if exit_status.success?
        self.reload(:select => "encode_mask")
        self.update_column(:encode_mask, encode_mask + mask)
        CONVERTATION_LOG.info "video_#{id} #{FORMAT[mask]} done!"
      else
        File.open(log_file, "a") do |f| 
          captured_stdout.each_line { |line| f.puts(line) }
          captured_stderr.each_line  { |line| f.puts(line) }
        end
        CONVERTATION_LOG.info "video_#{id} #{FORMAT[mask]} failed!"
      end
    end
  end
  Process.wait
end

#create_dir(dir_path) ⇒ Object



64
65
66
67
68
69
70
# File 'app/models/fly_admin/concerns/convertable.rb', line 64

def create_dir(dir_path)
  dir = File.dirname(dir_path)
  unless File.directory?(dir)
    FileUtils.mkdir_p(dir)
  end
  dir
end

#full_source_pathObject

Path helpers



83
84
85
# File 'app/models/fly_admin/concerns/convertable.rb', line 83

def full_source_path
  SiteConfig['torrents_path'] + source_path
end

#high_content_pathObject



91
92
93
# File 'app/models/fly_admin/concerns/convertable.rb', line 91

def high_content_path
  self.path + 'high/movie.mp4'
end

#high_content_urlObject



111
112
113
# File 'app/models/fly_admin/concerns/convertable.rb', line 111

def high_content_url
  self.url + 'high/movie.mp4'
end

#low_content_pathObject



87
88
89
# File 'app/models/fly_admin/concerns/convertable.rb', line 87

def low_content_path
  self.path + 'low/movie.mp4'
end

#low_content_urlObject



107
108
109
# File 'app/models/fly_admin/concerns/convertable.rb', line 107

def low_content_url
  self.url + 'low/movie.mp4'
end

#pathObject



95
96
97
# File 'app/models/fly_admin/concerns/convertable.rb', line 95

def path
  "#{Rails.root}/public/uploads/video/content/#{self.uuid}/"
end

#processObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/fly_admin/concerns/convertable.rb', line 10

def process
  CONVERTATION_LOG.info "video_#{self.id}: start processing"
  
  begin
    create_dir(self.high_content_path)
    create_dir(self.low_content_path)

    dir = create_dir(Rails.root.to_s + '/tmp/ffmpeg/*')
    Dir.chdir(dir)
    
    convert('-codec:v libvpx -b:v 360k -codec:a libvorbis -b:a 60k -ac 2 -s 640x360 -movflags +faststart -f webm', 'low/movie.webm', WEBM_LOW_MASK ) if (self.encode_mask & WEBM_LOW_MASK == 0)
    convert('-codec:v libvpx -b:v 1000k -codec:a libvorbis -b:a 80k -ac 2 -s 1280x720 -movflags +faststart -f webm', 'high/movie.webm', WEBM_HIGH_MASK ) if (self.encode_mask & WEBM_HIGH_MASK == 0)
    
    convert("-codec:v libx264 -b:v 360k -maxrate 360k -bufsize 720k -movflags +faststart -vprofile high -preset slow -vf 'scale=trunc(iw/2)*2:360' -threads 0 -codec:a libfdk_aac -b:a 96k -ac 2 -f mp4", 'low/movie.mp4', MP4_LOW_MASK) if (self.encode_mask & MP4_LOW_MASK == 0)
    convert("-codec:v libx264 -b:v 1000k -maxrate 1000k -bufsize 2000k -movflags +faststart -vprofile high -preset slow -vf 'scale=trunc(iw/2)*2:720' -threads 0 -codec:a libfdk_aac -b:a 128k -ac 2 -f mp4", 'high/movie.mp4', MP4_HIGH_MASK) if (self.encode_mask & MP4_HIGH_MASK == 0)

    Dir.chdir(Rails.root.to_s)
  rescue Exception => e
    CONVERTATION_LOG.error "#{e.message}\n\t#{e.backtrace}"
    return "#{e.message}\n\t#{e.backtrace.join("\n\t")}"
  end

  return nil
end

#set_durationObject



72
73
74
75
76
# File 'app/models/fly_admin/concerns/convertable.rb', line 72

def set_duration
  movie = FFMPEG::Movie.new(full_source_path)
  self.duration = movie.duration.to_i
  save!
end

#urlObject



103
104
105
# File 'app/models/fly_admin/concerns/convertable.rb', line 103

def url 
  path.split("/public").last
end