From b1546eeaa2e80f6d6a655c598764201c26cd850f Mon Sep 17 00:00:00 2001 From: Leni Kadali <52788034+lenikadali@users.noreply.github.com> Date: Sat, 14 Sep 2024 12:44:41 +0300 Subject: [PATCH] Add release date to track metadata 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 --- app/models/track.rb | 5 ++++- app/models/transcode_command.rb | 6 ++++-- test/models/track_test.rb | 10 ++++++++++ test/models/transcode_command_test.rb | 8 ++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/models/track.rb b/app/models/track.rb index 245d76a1..788ccd74 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -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 diff --git a/app/models/transcode_command.rb b/app/models/transcode_command.rb index 5854fde5..3c42d8c6 100644 --- a/app/models/transcode_command.rb +++ b/app/models/transcode_command.rb @@ -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) diff --git a/test/models/track_test.rb b/test/models/track_test.rb index 3782c642..b9342dd0 100644 --- a/test/models/track_test.rb +++ b/test/models/track_test.rb @@ -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 diff --git a/test/models/transcode_command_test.rb b/test/models/transcode_command_test.rb index 26a3593d..11508446 100644 --- a/test/models/transcode_command_test.rb +++ b/test/models/transcode_command_test.rb @@ -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']) @@ -56,6 +57,7 @@ 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 @@ -63,13 +65,15 @@ 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, :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