Skip to content

Instantly share code, notes, and snippets.

@dkam
Last active December 17, 2015 06:49
Show Gist options
  • Save dkam/5568178 to your computer and use it in GitHub Desktop.
Save dkam/5568178 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: UTF-8
###
# Looks for all files in your root media directory with an extension listed in *accepted_formats* array
# then passes them to ffmepg to report back resolution. Reports file name, resolution, resolution name
#
# EG: ./media_size_report.rb /mnt/my_nas/movies
#
# List media sizes in order of most common:
# ./media_size_report.rb /mnt/my_nas/movies | awk -F: '{ print $2 }' | sort | uniq -c | sort -n
###
require 'debugger'
def usage_message
puts "Usage: media_size_report [root media directory]"
puts "Arg: #{ARGV[0]}"
exit
end
usage_message if ARGV[0].nil?
usage_message unless Dir.exists?(ARGV[0])
def get_res_name(resolution)
raise ArgumentException unless resolution.is_a? String
#http://en.wikipedia.org/wiki/List_of_common_resolutions
res = resolution.gsub(/\s/, '')
hres = resolution.gsub(/\s/, '')[/(\d{1,})x(\d{1,})/, 1].to_i
case
when hres >= 7680 then '4320p (UHDTV)'
when hres >= 3840 then '2160p (UHDTV)'
when hres >= 1920 then '1080p'
when hres >= 1280 then '720p'
when res == '720x576' then 'DVD (PAL)'
when res == '720x480' then 'DVD (NTSC)'
when hres >= 720 then 'DVD'
when hres >= 640 then 'SDTV'
when hres >= 480 then 'SVDC'
when hres >= 352 then 'VDC'
when hres >= 320 then 'QVGA'
else "unknown"
end
# res_name = case resolution.gsub(/\s/, '')
# when '1280x720' then '720p'
# when '1920x1080' then '1080'
# when '720x576' then 'DVD (PAL)'
# when '720x480' then 'DVD (NTSC)'
# when '3840x2160' then '2160p (UHDTV)'
# when '7680x4320' then '4320p (UHDTV)'
# else "unknown"
# end
end
def get_res(file)
return nil unless File.exists?(file)
output1=`ffprobe "#{file}" 2>&1 | grep Video`
output2=`ffprobe "#{file}" 2>&1 | grep bitrate`
raw_res = output1[/\d{3,}+?x\d{3,}/]
bit_rate = output2[/bitrate: (.*)\n/, 1]
unless raw_res.valid_encoding?
puts "Got encoing of #{raw_res.encoding.name}"
raw_res.encode!("UTF-8", "ISO-8859-1", :invalid => :replace, :undef => :replace, :replace => "")
end
res_name = get_res_name(raw_res)
res_string = raw_res.split("x").join(" x ")
return sprintf("%10s : %5s (%s)", res_string, res_name, bit_rate )
end
accepted_formats = [".avi", ".m4v", ".mp4", ".mkv"]
Dir.glob("#{ARGV[0]}/**/*").select {|f| accepted_formats.include? File.extname(f) }.each {|ff| printf( "%-100s : %s\n", File.basename(ff), get_res(ff)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment