Skip to content

Commit

Permalink
Merge pull request #5754 from avalonmediasystem/precise_duration
Browse files Browse the repository at this point in the history
Set duration of MasterFile from ActiveEncode instead of relying on the imprecise mediainfo duration
  • Loading branch information
cjcolvar authored Mar 29, 2024
2 parents 2e8034e + d608f13 commit df6e04d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
10 changes: 7 additions & 3 deletions app/models/master_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,13 @@ def update_progress_on_success!(encode)
#TODO pull this from the encode
self.date_digitized ||= Time.now.utc.iso8601

# Set duration after transcode if mediainfo fails to find.
# e.x. WebM files missing technical metadata
self.duration ||= encode.input.duration
# Update the duration detected by ActiveEncode
# because it has higher precision than mediainfo
# Set for the first time for files without duration
# e.g. WebM files missing technical metadata
# ActiveEncode returns duration in milliseconds which
# is stored as an integer string
self.duration = encode.input.duration.to_i.to_s if encode.input.duration.present?

outputs = Array(encode.output).collect do |output|
{
Expand Down
18 changes: 17 additions & 1 deletion spec/factories/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,44 @@
state { :running }
percent_complete { 50.5 }
current_operations { ['encoding'] }
input { FactoryBot.build(:encode_output) }
end

trait :succeeded do
state { :completed }
percent_complete { 100 }
current_operations { ['DONE'] }
input { FactoryBot.build(:encode_output) }
output { [ FactoryBot.build(:encode_output) ] }
end

trait :failed do
state { :failed }
percent_complete { 50.5 }
current_operations { ['FAILED'] }
input { FactoryBot.build(:encode_output) }
errors { ['Out of disk space.'] }
end
end

factory :encode_input, class: ActiveEncode::Input do
id { SecureRandom.uuid }
label { 'quality-high' }
url { 'file://path/to/output.mp4' }
duration { '21575.0' }
audio_bitrate { '163842.0' }
audio_codec { 'AAC' }
video_bitrate { '4000000.0' }
video_codec { 'AVC' }
width { '1024' }
height { '768' }
end

factory :encode_output, class: ActiveEncode::Output do
id { SecureRandom.uuid }
label { 'quality-high' }
url { 'file://path/to/output.mp4' }
duration { '21575' }
duration { '21575.0' }
audio_bitrate { '163842.0' }
audio_codec { 'AAC' }
video_bitrate { '4000000.0' }
Expand Down
29 changes: 14 additions & 15 deletions spec/models/master_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,21 +539,6 @@
end
end

describe '#update_progress_on_success!' do
subject(:master_file) { FactoryBot.create(:master_file) }
let(:encode) { double("encode", :output => []) }
before do
allow(master_file).to receive(:update_ingest_batch).and_return(true)
end

it 'should set the digitized date' do
master_file.update_progress_on_success!(encode)
master_file.reload
expect(master_file.date_digitized).to_not be_empty
end

end

describe "#structural_metadata_labels" do
subject(:master_file) { FactoryBot.create(:master_file, :with_structure) }
it 'should return correct list of labels' do
Expand Down Expand Up @@ -816,11 +801,25 @@
let(:master_file) { FactoryBot.build(:master_file) }
let(:encode_succeeded) { FactoryBot.build(:encode, :succeeded) }

before do
allow(master_file).to receive(:update_derivatives)
allow(master_file).to receive(:run_hook)
end

it 'calls update_derivatives' do
expect(master_file).to receive(:update_derivatives).with(array_including(hash_including(label: 'quality-high')))
expect(master_file).to receive(:run_hook).with(:after_transcoding)
master_file.update_progress_on_success!(encode_succeeded)
end

it 'updates duration' do
expect { master_file.update_progress_on_success!(encode_succeeded) }.to change { master_file.duration }.from("200000").to("21575")
end

it 'should set the digitized date' do
master_file.update_progress_on_success!(encode_succeeded)
expect(master_file.date_digitized).to_not be_empty
end
end

describe 'update_derivatives' do
Expand Down

0 comments on commit df6e04d

Please sign in to comment.