Skip to content

Commit

Permalink
Add release date to track metadata
Browse files Browse the repository at this point in the history
Fixes #185

This commit adds the DATE metadata field to the transcoded MP3 and
FLAC files. The FLAC documentation we can find seems to suggest using
DATE for this purpose[1]. For MP3, `date` also seems to be an
acceptable field name[2]. We've verified that the data shows up in the
metadata displayed by VLC[3] for both formats.

[1] https://xiph.org/vorbis/doc/v-comment.html#fieldnames
[2] https://docs.mp3tag.de/format/
[3] https://www.videolan.org/vlc/

Co-authored-by: Chris Lowis <[email protected]>
  • Loading branch information
lenikadali and chrislo committed Oct 2, 2024
1 parent e6559a0 commit b1546ee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ def preview_duration
end

def metadata
{
data = {
track_title: title,
track_number: number,
album_title: album.title,
artist_name: artist.name
}

data[:release_date] = album.released_on if album.released_on
data
end

def transcode
Expand Down
6 changes: 4 additions & 2 deletions app/models/transcode_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ class TranscodeCommand
track_title: 'TIT2',
track_number: 'TRCK',
album_title: 'TALB',
artist_name: 'TPE1'
artist_name: 'TPE1',
release_date: 'DATE'
}.freeze

METADATA_KEYS_VS_FLAC_TAGS = {
track_title: 'TITLE',
track_number: 'TRACKNUMBER',
album_title: 'ALBUM',
artist_name: 'ARTIST'
artist_name: 'ARTIST',
release_date: 'DATE'
}.freeze

def initialize(input, output, format, metadata = {}, image = nil)
Expand Down
10 changes: 10 additions & 0 deletions test/models/track_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ class TrackTest < ActiveSupport::TestCase
assert_equal track.number, metadata[:track_number]
assert_equal track.album.title, metadata[:album_title]
assert_equal track.album.artist.name, metadata[:artist_name]
assert_not metadata.key?(:release_date)
end

test '#metadata when album has released_on' do
album = create(:album, released_on: Time.zone.today)
track = create(:track, album:)

metadata = track.metadata

assert_equal track.album.released_on, metadata[:release_date]
end

test '#number' do
Expand Down
8 changes: 6 additions & 2 deletions test/models/transcode_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class TranscodeCommandTest < ActiveSupport::TestCase
track_title: 'track-title',
track_number: 'track-number',
album_title: 'album-title',
artist_name: 'artist-name'
artist_name: 'artist-name',
release_date: 'release-date'
}
command_string = TranscodeCommand.new(@input, @output, :mp3v0, metadata).generate
assert_contains_pair(command_string, ['-write_id3v2', '1'])
Expand All @@ -56,20 +57,23 @@ class TranscodeCommandTest < ActiveSupport::TestCase
assert_contains_pair(command_string, ['-metadata', 'TRCK="track-number"'])
assert_contains_pair(command_string, ['-metadata', 'TALB="album-title"'])
assert_contains_pair(command_string, ['-metadata', 'TPE1="artist-name"'])
assert_contains_pair(command_string, ['-metadata', 'DATE="release-date"'])
end

test 'adds metadata tags for FLAC format' do
metadata = {
track_title: 'track-title',
track_number: 'track-number',
album_title: 'album-title',
artist_name: 'artist-name'
artist_name: 'artist-name',
release_date: 'release-date'
}
command_string = TranscodeCommand.new(@input, @output, :flac, metadata).generate
assert_contains_pair(command_string, ['-metadata', 'TITLE="track-title"'])
assert_contains_pair(command_string, ['-metadata', 'TRACKNUMBER="track-number"'])
assert_contains_pair(command_string, ['-metadata', 'ALBUM="album-title"'])
assert_contains_pair(command_string, ['-metadata', 'ARTIST="artist-name"'])
assert_contains_pair(command_string, ['-metadata', 'DATE="release-date"'])
end

test 'transcodes audio to mp3 using libmp3lame codec highest audio quality' do
Expand Down

0 comments on commit b1546ee

Please sign in to comment.