mkvtoolnix/tests/test-0730track_selection_by_language_matching.rb
Moritz Bunkus 0394a674bd
track selection: use language tag matching instead of verbatim equality
When using language tags for selecting which tracks to keep or
discard, mkvmerge was so far comparing the given language tag with the
ones in the file (after normalizing each). This meant that in order to
always keep all Spanish tracks but discard others, `--stracks !es`
would not work reliably as a track in the file might be specified as
`es-ES` — and verbatim comparison simply didn't treat `es` and `es-ES`
as the same.

For users this is somewhat counterintuitive. The idea behind allowing
languages for track selection has always been to provide an easy to
remember, easy to use way to select tracks for human beings without
having to look through file identification first. Verbatim comparison
worked fine until support for IETF BCP 47 language tags came along as
until that point languages in Matroska files only ever contained a
language component but not e.g. a region or a variant.

This commit changes the selection to use a matching algorithm similar
to how IETF BCP 47 describes language tag matching. Basically it takes
a track's existing language, normalizes it & splits it into its
components. Then the same is done with all the languages mentioned
with the track selection option currently evaluated.

For each language listed in the track selection all components that
are actually set are compared with the track's language's
corresponding components. If all of them are equal, the track is
considered to be matched. Components set in the track's language but
not in the selection's language are simply ignored.

This means that specifying `--stracks !es` in the example above will
now match all tracks whose language is some kind of Spanish, no matter
if the track's language tag contains a region, variants or
whatever (e.g. it would drop tracks marked as `es`, `es-MX`,
`es-Latn-ES` etc.).
2021-08-29 21:33:13 +02:00

35 lines
1.1 KiB
Ruby
Executable File

#!/usr/bin/ruby -w
# T_730track_selection_by_language_matching
describe "mkvmerge / track selection by language matching"
def track_languages file
identify_json(file)["tracks"].map { |track| track["properties"]["language_ietf"] }
end
def test_remux src, languages_args, languages_result = nil
out = "#{tmp}-2"
args = languages_args.join(',')
test_merge "--stracks #{args} #{src}", :output => out, :keep_tmp => true
test("languages #{args}") { track_languages(out) == (languages_result || languages_args) }
end
src1 = "data/subtitles/srt/ven.srt"
src2 = "#{tmp}-1"
all_languages = %w{de de-CH es es-ES es-MX es-US}
args = all_languages.
map { |language| "--language 0:#{language} #{src1}" }.
join(" ")
test_merge args, :output => src2, :keep_tmp => true
test("languages orig") { track_languages(src2) == all_languages }
test_remux src2, all_languages
test_remux src2, %w{es-ES es-US}
test_remux src2, %w{es-MX}
test_remux src2, %w{!es-MX}, %w{de de-CH es es-ES es-US}
test_remux src2, %w{es}, %w{es es-ES es-MX es-US}
test_remux src2, %w{de}, %w{de de-CH}
test_remux src2, %w{es de}, all_languages