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
6.1.0
  • Loading branch information
bajankristof authored Oct 24, 2024
2 parents a2da2cc + 69c2054 commit 781774e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
== 6.1.0 2024-10-24
* Added new `default?` and `attached_pic?` helper methods to FFMPEG::Stream objects
* Added new `audio_with_attached_pic?` helper method to FFMPEG::Media objects

== 6.0.2 2024-06-19

Fixes:
Expand Down
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
2 changes: 1 addition & 1 deletion lib/ffmpeg/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module FFMPEG
VERSION = '6.0.3'
VERSION = '6.1.0'
end
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 781774e

Please sign in to comment.