Skip to content

Commit

Permalink
feat: add methods to detect default streams and attachment pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
bajankristof committed Oct 24, 2024
1 parent a2da2cc commit a8e76bd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ffmpeg/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def audio_only?
audio? && !video?
end

def audio_with_attached_pic?
audio? && streams.any?(&:attached_pic?)
end

def silent?
!audio?
end
Expand Down
8 changes: 8 additions & 0 deletions lib/ffmpeg/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ def audio?
codec_type == CodecType::AUDIO
end

def default?
metadata.dig(:disposition, :default) == 1
end

def attached_pic?
metadata.dig(:disposition, :attached_pic) == 1
end

def width
@rotation.nil? || @rotation == 180 ? @width : @height
end
Expand Down
17 changes: 17 additions & 0 deletions spec/ffmpeg/media_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ module FFMPEG
end

it 'should return false if the media has video streams' do
subject = described_class.new("#{fixture_path}/sounds/napoleon.mp3")
expect(subject.audio_only?).to be(false)
end

Expand All @@ -256,6 +257,22 @@ module FFMPEG
end
end

describe '#audio_with_attached_pic?' do
it 'should return true if the media has audio streams with attached pictures' do
subject = described_class.new("#{fixture_path}/sounds/napoleon.mp3")
expect(subject.audio_with_attached_pic?).to be(true)
end

it 'should return false if the media does not have audio streams with attached pictures' do
expect(subject.audio_with_attached_pic?).to be(false)
end

it 'should return false if the media does not have attached pictures' do
subject = described_class.new("#{fixture_path}/sounds/hello.wav")
expect(subject.audio_with_attached_pic?).to be(false)
end
end

%i[
index
profile
Expand Down
36 changes: 36 additions & 0 deletions spec/ffmpeg/stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,42 @@ module FFMPEG
end
end

describe '#default?' do
context 'when marked as default' do
let(:metadata) { { disposition: { default: 1 } } }

it 'should return true' do
expect(subject.default?).to be(true)
end
end

context 'when not marked as default' do
let(:metadata) { { disposition: { default: 0 } } }

it 'should return false' do
expect(subject.default?).to be(false)
end
end
end

describe '#attached_pic?' do
context 'when marked as an attached picture' do
let(:metadata) { { disposition: { attached_pic: 1 } } }

it 'should return true' do
expect(subject.attached_pic?).to be(true)
end
end

context 'when not marked as an attached picture' do
let(:metadata) { { disposition: { attached_pic: 0 } } }

it 'should return false' do
expect(subject.attached_pic?).to be(false)
end
end
end

describe '#width' do
context 'when the rotation is nil' do
let(:metadata) { { width: 100, height: 200 } }
Expand Down
Binary file modified spec/fixtures/sounds/napoleon.mp3
Binary file not shown.

0 comments on commit a8e76bd

Please sign in to comment.