Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FLAC tag names in FLAC metadata #184

Merged
merged 5 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 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 All @@ -22,8 +29,9 @@ def generate
global_options,
input_options,
image_options,
muxer_options,
metadata_options,
transcoding_options(@format),
transcoding_options,
@output.path
].compact.join(' ')
end
Expand All @@ -47,16 +55,21 @@ def image_options
"-i #{@image.path} -map 0:0 -map 1:0 -c copy"
end

def muxer_options
return unless @format.to_s =~ /^mp3/

'-write_id3v2 1 -id3v2_version 3'
end

def metadata_options
supported_keys = @metadata.slice(*id3v23_keys)
supported_keys = @metadata.slice(*tag_keys)
return if supported_keys.empty?

entries = supported_keys.map { |k, v| %(-metadata #{id3v23_key_for(k)}="#{v}") }
"-write_id3v2 1 -id3v2_version 3 #{entries.join(' ')}"
supported_keys.map { |k, v| %(-metadata #{tag_key_for(k)}="#{v}") }.join(' ')
end

def transcoding_options(format)
case format
def transcoding_options
case @format
when :mp3v0
'-codec:a libmp3lame -q:a 0 -f mp3'
when :mp3128k
Expand All @@ -68,11 +81,22 @@ def transcoding_options(format)
end
end

def id3v23_keys
METADATA_KEYS_VS_ID3V23_TAGS.keys
def tag_keys
metadata_lookup.keys
end

def id3v23_key_for(key)
METADATA_KEYS_VS_ID3V23_TAGS[key]
def tag_key_for(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
16 changes: 15 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 metadata tags using ID3v2.3 format for MP3 formats' do
metadata = {
track_title: 'track-title',
track_number: 'track-number',
Expand All @@ -58,6 +58,20 @@ class TranscodeCommandTest < ActiveSupport::TestCase
assert_contains_pair(command_string, ['-metadata', 'TPE1="artist-name"'])
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'
}
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"'])
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