Skip to content

Commit

Permalink
Use FLAC tag names in FLAC metadata
Browse files Browse the repository at this point in the history
In #183 @graue pointed out that FLAC has its own tag names [1,2]. This
changes the ffmpeg transcoding for FLAC tracks to use those names.
However, it's unclear to me whether we also need to change the encoding
of the metadata - currently I've left the `-write_id3v2 1` &
`-id3v2_version 3` ffmpeg options in place on the basis that @graue is
already able to view the tags on a FLAC track; it was just that the tag
names were incorrect.

An initial search of the internet suggests that it might be better to
use the `metaflac` tool [3] to add metadata to a track, but that would
be a moderately significant change and it would probably be worth
changing MP3 tagging to work in a similar way, i.e. with a specialist
command line tool rather than just using ffmpeg.

Fixes #183.

[1]: https://xiph.org/flac/faq.html#general__tagging
[2]: https://xiph.org/vorbis/doc/v-comment.html
[3]: https://xiph.org/flac/documentation_tools_metaflac.html
  • Loading branch information
floehopper committed Apr 5, 2024
1 parent 96cc420 commit ddf92b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
22 changes: 20 additions & 2 deletions app/models/transcode_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class TranscodeCommand
artist_name: 'TPE1'
}.freeze

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

def initialize(input, output, format, metadata = {}, image = nil)
@input = input
@output = output
Expand Down Expand Up @@ -69,10 +76,21 @@ def transcoding_options(format)
end

def id3v23_keys
METADATA_KEYS_VS_ID3V23_TAGS.keys
metadata_lookup.keys
end

def id3v23_key_for(key)
METADATA_KEYS_VS_ID3V23_TAGS[key]
metadata_lookup[key]
end

def metadata_lookup
case @format.to_s
when /^mp3/
METADATA_KEYS_VS_ID3V23_TAGS
when /^flac/
METADATA_KEYS_VS_FLAC_TAGS
else
raise ArgumentError, "unsupported format: #{@format}"
end
end
end
18 changes: 17 additions & 1 deletion test/models/transcode_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TranscodeCommandTest < ActiveSupport::TestCase
assert_contains_pair(command_string, ['-c', 'copy'])
end

test 'adds metadata using ID3v2.3 format' do
test 'adds ID3 tags using ID3v2.3 format for MP3 formats' do
metadata = {
track_title: 'track-title',
track_number: 'track-number',
Expand All @@ -58,6 +58,22 @@ class TranscodeCommandTest < ActiveSupport::TestCase
assert_contains_pair(command_string, ['-metadata', 'TPE1="artist-name"'])
end

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

test 'transcodes audio to mp3 using libmp3lame codec highest audio quality' do
command_string = TranscodeCommand.new(@input, @output, :mp3v0).generate
assert_contains_pair(command_string, ['-codec:a', 'libmp3lame'])
Expand Down

0 comments on commit ddf92b9

Please sign in to comment.