Sizing Up My Queue

I have a folder named “queue” that I download podcasts, videos, and books to. It occurred to me that it hasn’t been empty for years. That’s OK, the world is a very interesting place, and I care that I produce things, not just consume them.

But I wondered how big the queue is. Not in terms of disk space, that doesn’t tell me much because the different encoding rates and formats result in very different file sizes. (The disk space command is df -h, if you need it.)

So I wrote a script. I hacked it all out for a first pass, then tidied it into functions. It depends on having ffmpeg installed, and here’s the source:

 
#!/bin/ruby
 
MEDIA_EXTENSIONS = ['*.mp3', '*.ogg', '*.ogv', '*.avi', '*.mp4', '*.webm', '*.flv', '*.mov', '*.mpeg', '*.mkv', '*.mov']
 
MINUTE = 60
HOUR = MINUTE * 60
DAY = HOUR * 24
 
def seconds_to_duration seconds
  days = seconds / DAY
  seconds -= days * DAY
 
  hours = seconds / HOUR
  seconds -= hours * HOUR
 
  minutes = seconds / MINUTE
  seconds -= minutes * MINUTE
 
  "%dd:%02dh:%02dm:%02ds" % [days, hours, minutes, seconds]
end
 
def duration_of_file filename
  raw_duration = `ffprobe "#{filename}" 2>&1 | grep Duration | cut -d' ' -f 4 | cut -d',' -f 1`
  h, m, s, x = raw_duration.split(/[:\.]/)
  h, m, s = h.to_i, m.to_i, s.to_i
  seconds = s + m * 60 + h * 60 * 60
  seconds
end
 
def duration_of_files path, spec
  spec.map! { |s| "**/**/#{s}" }
  previous = Dir.getwd
  Dir.chdir path
  count = 0
  total = 0
  Dir[*spec].each do |filename|
    count += 1
    seconds = duration_of_file filename
    total += seconds
  end
  Dir.chdir previous
  return count, total
end
 
def report_on path, spec
  count, total = duration_of_files path, spec
  puts "#{path}: #{count} files, #{total} seconds = #{seconds_to_duration total} total duration"
end
 
dirs = ARGV.empty? ? [Dir.pwd] : ARGV
dirs.each do |dir|
  report_on dir, MEDIA_EXTENSIONS
end

Running it on my queue directory took a minute or so, then reported:

queue: 877 files, 674007 seconds = 7d:19h:13m:27s total duration

Looks like I have a bit of material ahead of me…

Want more? I'm not as good at forgetting to update @pushcx on Twitter.