-
Notifications
You must be signed in to change notification settings - Fork 7
/
audio.rb
executable file
·80 lines (71 loc) · 2.66 KB
/
audio.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# == Schema Information
# Schema version: 20181126085723
#
# Table name: audios
#
# id :integer(4) not null, primary key
# user_id :integer(4)
# title :string(255)
# description :text
# state :string(255) default("initial")
# audio_file_name :string(255)
# audio_content_type :string(255)
# audio_file_size :integer(4)
# audio_updated_at :datetime
# created_at :datetime
# updated_at :datetime
# viewed_number :integer(4) default(0)
# rates_average :integer(4) default(0)
# comments_number :integer(4) default(0)
#
# This class is defining an item object called 'Audio'.
#
# You can use it to publish an audio content from different format like MP3, OGG, ... .
# Your audio file will automatically be converted into MP3 on the server,
# using the FFMPEG encoder (launched through Backgroundrb plugin, 'converter_worker' task).
#
# On the show page, a Flash player will allow you to play this file.
#
# See the ActsAsItem:ModelMethods module to have further informations.
#
require 'friendly_url'
class Audio < ActiveRecord::Base
# Method defined in the ActsAsItem:ModelMethods:ClassMethods (see that library fro more information)
acts_as_item
# Paperclip attachment definition
# Audit activation of the item
acts_as_audited :except => :viewed_number
has_attached_file :audio,
:url => "/uploaded_files/audio/:id/:style/:basename.:extension",
:path => ":rails_root/public/uploaded_files/audio/:id/:style/:basename.:extension"
# Validation of the presence of a attached file
validates_attachment_presence :audio
# Validation of the type of the attached file
#validates_attachment_content_type :audio, :content_type => ['audio/wav','audio/x-wav', 'audio/mpeg', 'audio/x-ms-wma', 'video/mp4' ]
# Validation of the size of the attached file
validates_attachment_size(:audio, :less_than => 25.megabytes)
# Callbacks
#after_save { |record| Delayed::Job.enqueue(EncodingJob.new({:type=>"audio", :id => record.id, :enc=>"mp3"})) }
# Media type used for the MP3 encoding
#
# This method returns a media type used inside the 'converter_worker' task during the encoding.
#
# Usage :
# <tt>object.media_type</tt>
def media_type
audio
end
def path_to_encoded_file
file_ext = self.audio_file_name.split('.').last
File.dirname(self.audio.url) + "/" + self.audio_file_name.delete(file_ext) + 'mp3'
end
# Codec used for the MP3 encoding
#
# This method returns the codec used by FFMPEG for the encoding (inside 'converter worker' task).
#
# Usage :
# <tt>object.codec</tt>
def codec
"-acodec libmp3lame -y"
end
end