Module: MediaOrganizer::Music
- Included in:
- Filescanner
- Defined in:
- lib/scrapers/music.rb
Constant Summary collapse
- SUPPORTED_FILETYPES =
%w(.mp3 .m4a .mp4 .flac .m4a .ogg .aiff .asf .wav).freeze
Class Method Summary collapse
-
.availableMetadata(filepath = '', args = {}) ⇒ Object
availableMetadata(file, args): returns list of fields available as metadata for the given file.
- .get_music_data(file) ⇒ Object
- .music?(uri) ⇒ Boolean
- .supported_filetypes ⇒ Object
-
.writeMetadata(filepath, meta = {}) ⇒ Object
writeMetadata(file = “”, meta = {}): returns list of fields available as metadata for the given file.
Class Method Details
.availableMetadata(filepath = '', args = {}) ⇒ Object
availableMetadata(file, args): returns list of fields available as metadata for the given file.
Inputs
*(1) filepath: full/absolute URI of the file to be analyzed. Required input. *(2) args: optional arguments passed as hash. Set “:include_null” to false to only return populated metadata fields.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/scrapers/music.rb', line 66 def self.availableMetadata(filepath = '', args = {}) attrs = get_music_data(filepath) unless args[:include_null] == false attrs.each do |field, value| attrs.delete(field) if value.nil? || value == '' end end attrs end |
.get_music_data(file) ⇒ Object
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 |
# File 'lib/scrapers/music.rb', line 11 def self.get_music_data(file) attributes = {} TagLib::FileRef.open(file) do |fileref| unless fileref.null? # sign tags to local variables tag = fileref.tag properties = fileref.audio_properties # load tags into attributes attribute attributes[:title] = tag.title attributes[:track] = tag.track attributes[:genre] = tag.genre attributes[:year] = tag.year attributes[:album] = tag.album attributes[:artist] = tag.artist attributes[:comment] = tag.comment attributes[:length] = properties.length attributes[:bitrate] = properties.bitrate attributes[:channels] = properties.channels attributes[:sample_rate] = properties.sample_rate end end attributes end |
.music?(uri) ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/scrapers/music.rb', line 42 def self.music?(uri) unless !uri.nil? && uri.is_a?(String) && File.exist?(uri) raise StandardError, "Directory given (#{uri}) could not be accessed." end if SUPPORTED_FILETYPES.include?(File.extname(uri).downcase) return true else return false end rescue FileNotFoundError => e puts e. puts e.backtrace.inspect return false end |
.supported_filetypes ⇒ Object
38 39 40 |
# File 'lib/scrapers/music.rb', line 38 def self.supported_filetypes reutrn SUPPORTED_FILETYPES end |
.writeMetadata(filepath, meta = {}) ⇒ Object
writeMetadata(file = “”, meta = {}): returns list of fields available as metadata for the given file.
Inputs
*(1) filepath: full/absolute URI of the file to be analyzed. Required input. *(2) meta: metadata to be written, passed as a hash in the format :metadata_field => metadata_value
Outputs
Returns true if the file was successfully saved. Note: true status does not necessarily indicate each field was successfully written.
Examples
Music.writeMetadata(“/absolute/path/to/file.mp3”, => “NewArtistName”, :year => “2019”)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/scrapers/music.rb', line 90 def self.writeMetadata(filepath, = {}) attributes = {} successflag = false TagLib::FileRef.open(filepath) do |fileref| unless fileref.null? # sign tags to local variables tag = fileref.tag properties = fileref.audio_properties .each do |field, value| if tag.respond_to?(field) tag.send("#{field}=", value) elsif properties.respond_to?(field) properties.send("#{field}=", value) end end successflag = fileref.save end end successflag end |