Module: MediaOrganizer::Music
- Defined in:
- lib/scrapers/music.rb
Defined Under Namespace
Classes: FileNotFoundError
Constant Summary collapse
- SUPPORTED_FILETYPES =
%w{.mp3 .m4a .mp4 .flac .m4a .ogg .aiff .asf .wav}
Class Method Summary collapse
-
.availableMetadata(filepath = "", args = {}) ⇒ Object
availableMetadata(file, args): returns list of fields available as metadata for the given file.
- .getMusicData(file) ⇒ Object
- .is_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.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/scrapers/music.rb', line 65 def Music.availableMetadata(filepath = "", args = {}) attrs = getMusicData(filepath) unless args[:include_null] == false attrs.each do |field, value| if value == nil || value == "" attrs.delete(field) end end end return attrs end |
.getMusicData(file) ⇒ Object
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 |
# File 'lib/scrapers/music.rb', line 10 def Music.getMusicData(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 return attributes end |
.is_music?(uri) ⇒ Boolean
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/scrapers/music.rb', line 41 def Music.is_music?(uri) unless !uri.nil? && uri.is_a?(String) && File.exists?(uri) raise FileNotFoundError, "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
37 38 39 |
# File 'lib/scrapers/music.rb', line 37 def Music.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”)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/scrapers/music.rb', line 91 def Music.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.to_s}=", value) elsif properties.respond_to?(field) properties.send("#{field.to_s}=", value) end end successflag = fileref.save end end return successflag end |