Skip to content

Commit

Permalink
handle multiple caption files
Browse files Browse the repository at this point in the history
  • Loading branch information
hudajkhan committed Nov 17, 2023
1 parent 53a6a8c commit 785d5a3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
22 changes: 21 additions & 1 deletion app/components/embed/media_tag_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,27 @@ def enabled_streaming_sources
def transcript
return unless render_captions?

tag.track(src: file.vtt.file_url, kind: 'captions', srclang: 'en', label: 'English')
# A video clip may have multiple caption files in different languages.
# We want to enable the user to select from any of these options.
# We also want the different language options to be listed alphabetically.
safe_join(
file.vtt.sort_by { |cfile| caption_language(cfile.language) }.map do |caption_file|
lang_code = caption_file.language || 'en'
lang_label = caption_language(lang_code)
tag.track(src: caption_file.file_url, kind: 'captions', srclang: lang_code, label: lang_label)
end
)
end

def caption_language(language_code)
case language_code
when 'en'
'English'
when 'ru'
'Russian'
else
'Caption'
end
end

def render_captions?
Expand Down
4 changes: 2 additions & 2 deletions app/models/embed/purl/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def thumbnail
files.find(&:thumbnail?)
end

# @return [ResourceFile]
# @return [Array<ResourceFile>]
def vtt
files.find(&:vtt?)
files.select(&:vtt?)
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion app/models/embed/purl/resource_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ def thumbnail?
Settings.resource_types_that_contain_thumbnails.include?(resource.type)
end

# Returns array of VTT files
def vtt
resource.files.find(&:vtt?)
resource.files.select(&:vtt?)
end

def vtt?
mimetype == 'text/vtt'
end

# Returns language if specified by language tag
def language
@file.attributes['language'].try(:value)
end

def pdf?
mimetype == 'application/pdf'
end
Expand Down
10 changes: 10 additions & 0 deletions spec/components/embed/media_tag_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,14 @@
expect(page).to have_css('track[src="https://stacks.stanford.edu/file/druid:bc123df4567/abc_123_cap.webvtt"]')
end
end

context 'with multiple captions with different languages' do
let(:purl) { video_purl_with_multiple_vtt }
let(:include_transcripts) { true }

it 'has track elements with multiple languages' do
expect(page).to have_css('track[srclang="en"][label="English"]')
expect(page).to have_css('track[srclang="ru"][label="Russian"]')
end
end
end
27 changes: 27 additions & 0 deletions spec/fixtures/purl_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,33 @@ def video_purl_with_vtt
XML
end

def video_purl_with_multiple_vtt
<<-XML
<publicObject>
<identityMetadata>
<objectLabel>Title of the single video</objectLabel>
</identityMetadata>
<contentMetadata type="media">
<resource sequence="1" id="abc123_1" type="video">
<file id="abc_123.mp4" mimetype="video/mp4" size="152000000"></file>
<file id="english_caption.webvtt" mimetype="text/vtt" size="176218" role="caption" language="en"></file>
<file id="russian_caption.webvtt" mimetype="text/vtt" size="176218" role="caption" language="ru"></file>
</resource>
</contentMetadata>
<rightsMetadata>
<access type="read">
<machine>
<location>spec</location>
</machine>
</access>
</rightsMetadata>
<oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>title of video with vtt</dc:title>
</oai_dc>
</publicObject>
XML
end

def video_purl
<<-XML
<publicObject>
Expand Down
4 changes: 2 additions & 2 deletions spec/models/embed/purl/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
let(:resource) { Embed::Purl.new('12345').contents.first }

context 'when it has a vtt transcript' do
subject { resource.vtt.title }
subject { resource.vtt[0].title }

before { stub_purl_response_with_fixture(video_purl_with_vtt) }

it { is_expected.to eq 'abc_123_cap.webvtt' }
end

context 'when it does not have a vtt transcript' do
subject { resource.vtt }
subject { resource.vtt[0] }

before { stub_purl_response_with_fixture(single_video_purl) }

Expand Down

0 comments on commit 785d5a3

Please sign in to comment.