Skip to content

Commit

Permalink
Merge pull request #223 from freerange/add-release-date-to-track-meta…
Browse files Browse the repository at this point in the history
…data

Add release date to track metadata
  • Loading branch information
chrislo authored Oct 3, 2024
2 parents e6559a0 + 633df3f commit 980f2e5
Show file tree
Hide file tree
Showing 5 changed files with 60 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_year] = album.released_on.year 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_year: 'TORY'
}.freeze

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

def initialize(input, output, format, metadata = {}, image = nil)
Expand Down
36 changes: 36 additions & 0 deletions test/jobs/transcode_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,40 @@ class TranscodeJobTest < ActiveJob::TestCase

TranscodeJob.perform_now(build(:track))
end

test 'it adds metadata to the generated MP3 file' do
album = create(:album, released_on: Date.parse('2024-10-03'))
track = create(:track, album:)
TranscodeJob.perform_now(track)

track.transcodes.mp3v0.first.file.open do |file|
cmd = "ffprobe -i #{file.path} -v quiet -print_format json -show_format"
std_out, _status = Open3.capture2(cmd)
metadata = JSON.parse(std_out)

assert_equal track.title, metadata['format']['tags']['title']
assert_equal '01', metadata['format']['tags']['track']
assert_equal album.title, metadata['format']['tags']['album']
assert_equal album.artist.name, metadata['format']['tags']['artist']
assert_equal '2024', metadata['format']['tags']['TORY']
end
end

test 'it adds metadata to the generated flac file' do
album = create(:album, released_on: Date.parse('2024-10-03'))
track = create(:track, album:)
TranscodeJob.perform_now(track, format: :flac)

track.transcodes.flac.first.file.open do |file|
cmd = "ffprobe -i #{file.path} -v quiet -print_format json -show_format"
std_out, _status = Open3.capture2(cmd)
metadata = JSON.parse(std_out)

assert_equal track.title, metadata['format']['tags']['TITLE']
assert_equal '01', metadata['format']['tags']['track']
assert_equal album.title, metadata['format']['tags']['ALBUM']
assert_equal album.artist.name, metadata['format']['tags']['ARTIST']
assert_equal '2024', metadata['format']['tags']['DATE']
end
end
end
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.year, metadata[:release_year]
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_year: 'release-year'
}
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', 'TORY="release-year"'])
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_year: 'release-year'
}
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-year"'])
end

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

0 comments on commit 980f2e5

Please sign in to comment.