Skip to content

Commit

Permalink
Add track number to ID3 metadata
Browse files Browse the repository at this point in the history
I've reused the formatted track position that was already being used for
the filename prefix in `ZipDownloadJob#track_filename`, because I
thought it was worth being consistent.

We're using ID3v2.3 and it seems as if the `TRCK` tag is the one we
want. See the earlier commit [1] where we added the other metadata for
more details. As in that commmit, `Track#number` will never be blank, so
there's no need to add it to the metadata hash individually.

We'll need to re-transcode all tracks in order to apply this to existing
tracks, but I suggest we tackle that as a separate manual task where
we'll need to run `Artist.all.each(&:transcode_albums)` in a Rails
console.

[1]: 1851e27
  • Loading branch information
floehopper committed Dec 23, 2023
1 parent 9103f79 commit 1d58300
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/jobs/zip_download_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def download_all_tracks(album, format, dir)
end

def track_filename(track, format)
Zaru.sanitize!("#{track.position.to_s.rjust(2, '0')} - #{track.title}.#{extension(format)}")
Zaru.sanitize!("#{track.number} - #{track.title}.#{extension(format)}")
end

def extension(format)
Expand Down
5 changes: 5 additions & 0 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def preview_duration
def metadata
{
track_title: title,
track_number: number,
album_title: album.title,
artist_name: artist.name
}
Expand All @@ -43,4 +44,8 @@ def transcode
def metadata_or_original_changed?
title_previously_changed? || attachment_changes['original'].present?
end

def number
position.to_s.rjust(2, '0')
end
end
1 change: 1 addition & 0 deletions app/models/transcode_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class TranscodeCommand
METADATA_KEYS_VS_ID3V23_TAGS = {
track_title: 'TIT2',
track_number: 'TRCK',
album_title: 'TALB',
artist_name: 'TPE1'
}.freeze
Expand Down
7 changes: 7 additions & 0 deletions test/models/track_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ class TrackTest < ActiveSupport::TestCase
metadata = track.metadata

assert_equal track.title, metadata[:track_title]
assert_equal track.number, metadata[:track_number]
assert_equal track.album.title, metadata[:album_title]
assert_equal track.album.artist.name, metadata[:artist_name]
end

test '#number' do
album = create(:album_with_tracks)

assert_equal %w[01 02], album.tracks.map(&:number)
end
end
8 changes: 7 additions & 1 deletion test/models/transcode_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ class TranscodeCommandTest < ActiveSupport::TestCase
end

test 'adds metadata using ID3v2.3 format' do
metadata = { track_title: 'track-title', album_title: 'album-title', artist_name: 'artist-name' }
metadata = {
track_title: 'track-title',
track_number: 'track-number',
album_title: 'album-title',
artist_name: 'artist-name'
}
command_string = TranscodeCommand.new(@input, @output, :mp3v0, metadata).generate
assert_contains_pair(command_string, ['-write_id3v2', '1'])
assert_contains_pair(command_string, ['-id3v2_version', '3'])
assert_contains_pair(command_string, ['-metadata', 'TIT2="track-title"'])
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"'])
end
Expand Down

0 comments on commit 1d58300

Please sign in to comment.