Skip to content

Commit

Permalink
Simplify methods in FFprobe
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Colvard <[email protected]>
  • Loading branch information
masaball and cjcolvar committed Sep 13, 2024
1 parent 3b2cf57 commit 4333359
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
36 changes: 21 additions & 15 deletions lib/avalon/ffprobe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,42 @@

module Avalon
class FFprobe
attr_reader :json_output, :video_stream, :audio_stream
# @param [FileLocator] media_file a file locator instance for the media file
def initialize(media_file)
return @json_output = {} unless valid_content_type?(media_file)
@media_file = media_file
end

def json_output
return @json_output unless @json_output.nil?
return @json_output = {} unless valid_content_type?(@media_file)
ffprobe = Settings&.ffprobe&.path || 'ffprobe'
raw_output = `#{ffprobe} -i "#{media_file.location}" -v quiet -show_format -show_streams -of json`
raw_output = `#{ffprobe} -i "#{@media_file.location}" -v quiet -show_format -show_streams -of json`
# $? is a variable for the exit status of the last executed process.
# Success == 0, any other value means the command failed in some way.
unless $?.exitstatus == 0
@json_output = {}
Rails.logger.error "File processing failed. Please ensure that FFprobe is installed and that the correct path is configured."
return
return @json_output = {}
end
@json_output = JSON.parse(raw_output).deep_symbolize_keys
@json_output
end

@video_stream = json_output[:streams].select { |stream| stream[:codec_type] == 'video' }.first
@audio_stream = json_output[:streams].select { |stream| stream[:codec_type] == 'audio' }.first
def video_stream
return unless json_output.present?
@video_stream ||= json_output[:streams].select { |stream| stream[:codec_type] == 'video' }.first
end

def video?
return true if video_stream
def audio_stream
return unless json_output.present?
@audio_stream ||= json_output[:streams].select { |stream| stream[:codec_type] == 'audio' }.first
end

false
def video?
video_stream.present?
end

def audio?
return true if audio_stream

false
audio_stream.present?
end

def duration
Expand Down Expand Up @@ -70,9 +77,8 @@ def valid_content_type? media_file
extension = File.extname(media_file.location)&.gsub(/[\?#].*/, '')
# Fall back on file extension if magic bytes fail to identify file
content_type = Marcel::MimeType.for media_file.reader, extension: extension
return true if ['audio', 'video'].any? { |type| content_type.include?(type) }

false
['audio', 'video'].any? { |type| content_type.include?(type) }
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/avalon/ffprobe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
it 'logs an error if ffprobe is misconfigured' do
allow(Settings.ffprobe).to receive(:path).and_return('misconfigured/path')
expect(Rails.logger).to receive(:error)
subject
subject.json_output
end
end

Expand Down

0 comments on commit 4333359

Please sign in to comment.