Skip to content

Commit

Permalink
issues/38 & issues/28
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Mar 6, 2019
1 parent b8f0f2c commit 6d1e4cf
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pkg
!/spec/fixtures/video/.keep
/tmp/
.idea

.vscode
# rspec failure tracking
.rspec_status

Expand Down
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
language: ruby
rvm:
- 2.5.3
- 2.6.1
before_install:
- sudo apt-get install -y mediainfo
env:
- MEDIAINFO_PATH=/usr/bin/mediainfo
install:
# Install bundler 2
- gem update --no-document --system
- gem install bundler --no-document
# Install gems
- bundle install
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"search.location": "panel"
}
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'

git_source(:github) {|repo_name| "https://github.com/greatseth/#{repo_name}" }

gem 'pry'
gemspec
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ MediaInfo is a class wrapping [the mediainfo CLI](http://mediainfo.sourceforge.n
#### Handling a URL
media_info = MediaInfo.from('http://techslides.com/demos/sample-videos/small.mp4')

You can specify an alternate path for the MediaInfo Binary:
Ensure mediainfo is installed and within your PATH. You can specify an alternate path for the mediainfo binary if needed:

ENV['MEDIAINFO_PATH'] = "/opt/local/bin/mediainfo"
ENV['MEDIAINFO_PATH'] = "/usr/bin/mediainfo"

Once you have an MediaInfo object, you can start inspecting tracks:

Expand Down
19 changes: 17 additions & 2 deletions lib/mediainfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@

module MediaInfo

def self.which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
}
end
return nil
end

# Allow user to set custom mediainfo_path with ENV['MEDIAINFO_PATH']
def self.location
ENV['MEDIAINFO_PATH'].nil? ? mediainfo_location = '/usr/local/bin/mediainfo' : mediainfo_location = ENV['MEDIAINFO_PATH']
if ENV['MEDIAINFO_PATH'].nil?
mediainfo_location = which('mediainfo')
else
mediainfo_location = ENV['MEDIAINFO_PATH']
end
raise EnvironmentError, "#{mediainfo_location} cannot be found. Are you sure mediainfo is installed?" unless ::File.exist? mediainfo_location
return mediainfo_location
end
Expand Down Expand Up @@ -57,7 +72,7 @@ def self.from(input)

def self.from_string(input)
return from_xml(input) if input.include?('<?xml')
return from_link(input) if input =~ URI::regexp
return from_link(input) if input.include?('://') && input =~ URI::regexp
return from_local_file(input) if input.match(/[^\\]*\.\w+$/)
raise BadInputError
end
Expand Down
4 changes: 2 additions & 2 deletions mediainfo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ Gem::Specification.new do |s|
end

# Since Ruby 1.8, REXML is included in the standard Ruby distribution.
s.add_development_dependency 'bundler', '~> 1.16'
s.add_development_dependency 'bundler', '~> 2'
s.add_development_dependency 'rake', '~> 12.3'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'nokogiri', '~> 1.8' # Ability to parse XML response # Optional and should be included by user in their own gemfile
s.add_development_dependency 'nokogiri', '>= 1.8', '< 2.0' # Ability to parse XML response # Optional and should be included by user in their own gemfile

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
Expand Down
117 changes: 117 additions & 0 deletions spec/fixtures/xml/multiple_streams_with:id-2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<Mediainfo>
<File>
<track type="General">
<Complete_name>/Users/seth/diversion/hdcloud/test/videos/brightcove/VzbWkxMTrgYv8UoOiXZrbzowe0CV8wIr-me2-grunt.mov</Complete_name>
<Format>MPEG-4</Format>
<Format_profile>QuickTime</Format_profile>
<Codec_ID>qt </Codec_ID>
<File_size>101 MiB</File_size>
<Duration>1mn 25s</Duration>
<Overall_bit_rate>9 872 Kbps</Overall_bit_rate>
<Encoded_date>UTC 2009-08-18 16:42:50</Encoded_date>
<Tagged_date>UTC 2009-08-18 16:42:55</Tagged_date>
<Writing_application>Sorenson Squeeze 5.0</Writing_application>
<Writing_library>Apple QuickTime</Writing_library>
<com.apple.quicktime.player.movie.audio.mute>(Binary)</com.apple.quicktime.player.movie.audio.mute>
</track>
<track type="Video" streamid="1">
<ID>2</ID>
<Format>AVC</Format>
<Format_Info>Advanced Video Codec</Format_Info>
<Format_profile>[email protected]</Format_profile>
<Format_settings__CABAC>No</Format_settings__CABAC>
<Format_settings__ReFrames>4 frames</Format_settings__ReFrames>
<Codec_ID>avc1</Codec_ID>
<Codec_ID_Info>Advanced Video Coding</Codec_ID_Info>
<Duration>1mn 25s</Duration>
<Bit_rate_mode>Variable</Bit_rate_mode>
<Bit_rate>9 392 Kbps</Bit_rate>
<Width>1 280 pixels</Width>
<Height>720 pixels</Height>
<Display_aspect_ratio>16:9</Display_aspect_ratio>
<Frame_rate_mode>Variable</Frame_rate_mode>
<Frame_rate>29.970 fps</Frame_rate>
<Minimum_frame_rate>29.940 fps</Minimum_frame_rate>
<Maximum_frame_rate>29.970 fps</Maximum_frame_rate>
<Standard>NTSC</Standard>
<Resolution>24 bits</Resolution>
<Colorimetry>4:2:0</Colorimetry>
<Scan_type>Progressive</Scan_type>
<Bits__Pixel_Frame_>0.340</Bits__Pixel_Frame_>
<Stream_size>95.6 MiB (95%)</Stream_size>
<Encoded_date>UTC 2009-08-17 05:05:13</Encoded_date>
<Tagged_date>UTC 2009-08-18 16:42:55</Tagged_date>
</track>
<track type="Video" streamid="2">
<ID>6</ID>
<Format>AVC</Format>
<Format_Info>Advanced Video Codec</Format_Info>
<Format_profile>[email protected]</Format_profile>
<Format_settings__CABAC>No</Format_settings__CABAC>
<Format_settings__ReFrames>4 frames</Format_settings__ReFrames>
<Codec_ID>avc1</Codec_ID>
<Codec_ID_Info>Advanced Video Coding</Codec_ID_Info>
<Duration>4s 170ms</Duration>
<Bit_rate_mode>Variable</Bit_rate_mode>
<Bit_rate>656 Kbps</Bit_rate>
<Width>1 280 pixels</Width>
<Height>720 pixels</Height>
<Display_aspect_ratio>16:9</Display_aspect_ratio>
<Frame_rate_mode>Constant</Frame_rate_mode>
<Frame_rate>29.970 fps</Frame_rate>
<Standard>NTSC</Standard>
<Resolution>24 bits</Resolution>
<Colorimetry>4:2:0</Colorimetry>
<Scan_type>Progressive</Scan_type>
<Bits__Pixel_Frame_>0.024</Bits__Pixel_Frame_>
<Stream_size>334 KiB (0%)</Stream_size>
<Encoded_date>UTC 2009-08-18 16:42:20</Encoded_date>
<Tagged_date>UTC 2009-08-18 16:42:56</Tagged_date>
</track>
<track type="Audio" streamid="1">
<ID>1</ID>
<Format>AAC</Format>
<Format_Info>Advanced Audio Codec</Format_Info>
<Format_version>Version 4</Format_version>
<Format_profile>LC</Format_profile>
<Format_settings__SBR>No</Format_settings__SBR>
<Codec_ID>40</Codec_ID>
<Duration>1mn 25s</Duration>
<Bit_rate_mode>Variable</Bit_rate_mode>
<Bit_rate>256 Kbps</Bit_rate>
<Channel_s_>2 channels</Channel_s_>
<Channel_positions>L R</Channel_positions>
<Sampling_rate>48.0 KHz</Sampling_rate>
<Resolution>16 bits</Resolution>
<Stream_size>2.61 MiB (3%)</Stream_size>
<Encoded_date>UTC 2009-08-17 05:05:13</Encoded_date>
<Tagged_date>UTC 2009-08-18 16:42:55</Tagged_date>
</track>
<track type="Audio" streamid="2">
<ID>5</ID>
<Format>AAC</Format>
<Format_Info>Advanced Audio Codec</Format_Info>
<Format_version>Version 4</Format_version>
<Format_profile>LC</Format_profile>
<Format_settings__SBR>No</Format_settings__SBR>
<Codec_ID>40</Codec_ID>
<Duration>4s 156ms</Duration>
<Bit_rate_mode>Constant</Bit_rate_mode>
<Bit_rate>256 Kbps</Bit_rate>
<Channel_s_>2 channels</Channel_s_>
<Channel_positions>L R</Channel_positions>
<Sampling_rate>44.1 KHz</Sampling_rate>
<Resolution>16 bits</Resolution>
<Stream_size>130 KiB (0%)</Stream_size>
<Encoded_date>UTC 2009-08-18 16:42:20</Encoded_date>
<Tagged_date>UTC 2009-08-18 16:42:56</Tagged_date>
</track>
<track type="Menu">
<ID>3</ID>
<Encoded_date>UTC 2009-08-18 16:42:20</Encoded_date>
<Tagged_date>UTC 2009-08-18 16:42:56</Tagged_date>
<Delay>3733</Delay>
</track>
</File>
</Mediainfo>
4 changes: 0 additions & 4 deletions spec/mediainfo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
describe 'location class method' do

context 'when the mediainfo bin path (MEDIAINFO_PATH) is valid' do
include_context 'sets MEDIAINFO_PATH to default value'

it 'does not raise an error' do
expect{MediaInfo.location}.not_to raise_error
Expand All @@ -29,7 +28,6 @@
describe 'version class method' do

context 'when the mediainfo bin path (MEDIAINFO_PATH) is valid' do
include_context 'sets MEDIAINFO_PATH to default value'

it 'does not raise an error' do
expect{MediaInfo.version}.to_not raise_error
Expand All @@ -48,7 +46,6 @@
describe 'xml_parser class method' do

context 'when the chosen parser (MEDIAINFO_XML_PARSER) is the default one' do
include_context 'sets MEDIAINFO_XML_PARSER to default value'

it 'does not raise an error' do
expect{MediaInfo.version}.to_not raise_error
Expand Down Expand Up @@ -78,7 +75,6 @@
describe 'from class method' do

context 'when the chosen parser (MEDIAINFO_XML_PARSER) is the default one' do
include_context 'sets MEDIAINFO_XML_PARSER to default value'

it_behaves_like 'expected from class method for a file'
it_behaves_like 'expected from class method for a url'
Expand Down
1 change: 0 additions & 1 deletion spec/mediainfo_tracks_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
describe '.sanitize_element_value class method' do

context 'when the chosen parser (MEDIAINFO_XML_PARSER) is the default one' do
include_context 'sets MEDIAINFO_XML_PARSER to default value'

it 'standardizes the names correctly' do
expect(MediaInfo.from(xml_files_content[:sample_mov]).video.bit_rate).to be(nil)
Expand Down
3 changes: 0 additions & 3 deletions spec/mediainfo_tracks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
describe '*? for each types instance methods' do

context 'when the chosen parser (MEDIAINFO_XML_PARSER) is the default one' do
include_context 'sets MEDIAINFO_XML_PARSER to default value'

it_behaves_like 'for a valid collection of file path of videos' do
let(:videos) { videos_xml_files_content }
Expand Down Expand Up @@ -57,7 +56,6 @@
describe '.count for each types instance method' do

context 'when the chosen parser (MEDIAINFO_XML_PARSER) is the default one' do
include_context 'sets MEDIAINFO_XML_PARSER to default value'

context 'when the specified type is included in these Tracks' do
it 'returns the number of occurrences of this type' do
Expand Down Expand Up @@ -87,7 +85,6 @@
describe '.extra method for \'general\' type' do

context 'when the chosen parser (MEDIAINFO_XML_PARSER) is the default one' do
include_context 'sets MEDIAINFO_XML_PARSER to default value'

context 'when the submitted file has been generated by an Apple device' do
it 'does not return nil' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'mediainfo'
require 'spec_shared_contexts'
require 'spec_shared_examples'
require 'pry'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down
19 changes: 2 additions & 17 deletions spec/spec_shared_contexts.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Shared variables

RSpec.shared_context 'Shared variables' do
# Set ENVs back to default
ENV['MEDIAINFO_PATH'] = nil
ENV['MEDIAINFO_XML_PARSER'] = nil

# Listing of files path
base_local_path = './spec/fixtures/'

Expand Down Expand Up @@ -64,30 +60,19 @@

# MEDIAINFO_PATH

RSpec.shared_context 'sets MEDIAINFO_PATH to default value' do
before(:all) do
ENV['MEDIAINFO_PATH'] = nil
end
end

RSpec.shared_context 'sets MEDIAINFO_PATH to invalid value' do
mip = ENV['MEDIAINFO_PATH'] # Allows us to set MEDIAINFO_PATH back to what the user/env set it as
before(:all) do
ENV['MEDIAINFO_PATH'] = '/invalid/path/to/mediablinfo'
end

after(:all) do
ENV['MEDIAINFO_PATH'] = nil
ENV['MEDIAINFO_PATH'] = mip
end
end

# MEDIAINFO_XML_PARSER

RSpec.shared_context 'sets MEDIAINFO_XML_PARSER to default value' do
before(:all) do
ENV['MEDIAINFO_XML_PARSER'] = nil
end
end

RSpec.shared_context 'sets MEDIAINFO_XML_PARSER to nokogiri' do
before(:all) do
ENV['MEDIAINFO_XML_PARSER'] = 'nokogiri'
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
expect{MediaInfo.from(input)}.not_to raise_error
end

it 'colon in path does not raise an error' do
expect{MediaInfo.from('./spec/fixtures/xml/multiple_streams_with:id-2.xml')}.not_to raise_error
end

it 'returns an instance of MediaInfo::Tracks' do
expect(MediaInfo.from(input)).to be_an_instance_of(MediaInfo::Tracks)
end

it 'returns an object with a valid xml output' do
expect(MediaInfo.from(input).xml.include?('?xml')).to be true
end

end

context 'when submitted an invalid file path' do
Expand Down

0 comments on commit 6d1e4cf

Please sign in to comment.