Skip to content

Commit

Permalink
Merge pull request #1469 from sul-dlss/vtt
Browse files Browse the repository at this point in the history
Use VTT transcript
  • Loading branch information
jcoyne authored May 16, 2023
2 parents 33e3bdc + 2b0a1d8 commit e119456
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/embed/purl/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def primary_file
def thumbnail
files.find(&:thumbnail?)
end

# @return [ResourceFile]
def vtt
files.find(&:vtt?)
end
end
end
end
8 changes: 8 additions & 0 deletions app/models/embed/purl/resource_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def thumbnail?
Settings.resource_types_that_contain_thumbnails.include?(resource.type)
end

def vtt
resource.files.find(&:vtt?)
end

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

def pdf?
mimetype == 'application/pdf'
end
Expand Down
11 changes: 11 additions & 0 deletions lib/embed/media_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Embed
# Utility class to handle generating HTML <video> and <audio> tags
# Currently, MPEG-DASH is used at the <video> element level (in a data attribute to be picked up by javascript)
# and HLS is used as a <source> within the <video> or <audio> tag.
# rubocop:disable Metrics/ClassLength
class MediaTag
SUPPORTED_MEDIA_TYPES = %i[audio video].freeze

Expand Down Expand Up @@ -51,6 +52,7 @@ def media_element(file, type)
class="sul-embed-media-file #{'sul-embed-many-media' if many_primary_files?}"
height="100%">
#{enabled_streaming_sources(file)}
#{transcript(file)}
</#{type}>
HTML
end
Expand Down Expand Up @@ -91,6 +93,14 @@ def enabled_streaming_sources(file)
end.join
end

def transcript(file)
return unless file.vtt

<<~HTML
<track default src="#{viewer.stacks_url}/#{file.vtt.title}" />
HTML
end

def many_primary_files?
primary_files_count > 1
end
Expand Down Expand Up @@ -165,4 +175,5 @@ def authentication_url(file)
Settings.streaming.auth_url % attributes
end
end
# rubocop:enable Metrics/ClassLength
end
26 changes: 26 additions & 0 deletions spec/fixtures/purl_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,32 @@ def single_video_purl
XML
end

def video_purl_with_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="abc_123_cap.webvtt" mimetype="text/vtt" size="176218"></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
11 changes: 10 additions & 1 deletion spec/lib/embed/media_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
let(:purl) { video_purl }
let(:viewer) do
instance_double(Embed::Viewer::Media,
purl_object: Embed::Purl.new('druid'))
purl_object: Embed::Purl.new('druid'),
stacks_url: '/file/druid')
end

let(:subject_klass) { described_class.new(viewer) }
Expand Down Expand Up @@ -231,6 +232,14 @@
end
end

describe '#transcript' do
before { stub_purl_response_with_fixture(video_purl_with_vtt) }

it 'has a track element' do
expect(subject).to have_css('track[src="/file/druid/abc_123_cap.webvtt"]')
end
end

describe '#previewable_element' do
before { stub_purl_response_with_fixture(purl) }

Expand Down
20 changes: 20 additions & 0 deletions spec/models/embed/purl/resource_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,24 @@
end
end
end

describe '#vtt?' do
subject { file.vtt? }

context 'when it is a vtt transcript' do
let(:file) { Embed::Purl.new('12345').contents.first.files.second }

before { stub_purl_response_with_fixture(video_purl_with_vtt) }

it { is_expected.to be true }
end

context 'when it is not a vtt transcript' do
let(:file) { Embed::Purl.new('12345').contents.first.files.first }

before { stub_purl_response_with_fixture(single_video_purl) }

it { is_expected.to be false }
end
end
end
20 changes: 20 additions & 0 deletions spec/models/embed/purl/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@
expect(Embed::Purl.new('12345').contents.first.files.all?(Embed::Purl::ResourceFile)).to be true
end
end

describe '#vtt' do
let(:resource) { Embed::Purl.new('12345').contents.first }

context 'when it has a vtt transcript' do
subject { resource.vtt.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 }

before { stub_purl_response_with_fixture(single_video_purl) }

it { is_expected.to be_nil }
end
end
end

0 comments on commit e119456

Please sign in to comment.