Created
April 15, 2013 12:55
-
-
Save vderyagin/5387850 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
=begin | |
Determines total duration of all media files, passed as arguments. | |
Shells out to ffmpeg(1). | |
=end | |
require 'open3' | |
require 'shellwords' | |
def len(media_file) | |
_, stderr_str, _ = Open3.capture3("ffmpeg -i #{media_file.shellescape}") | |
/^\s*Duration: (?<hours>\d+):(?<minutes>\d+):(?<seconds>[\d\.]+), / =~ stderr_str | |
stats = Hash.new(0) | |
stats[:hours] = hours.to_i | |
stats[:minutes] = minutes.to_i | |
stats[:seconds] = seconds.to_f | |
stats | |
end | |
if __FILE__ == $PROGRAM_NAME | |
weeks, days, hours, minutes, seconds = 0, 0, 0, 0, 0 | |
if ARGV.empty? | |
puts "Usage:\t#{File.basename($PROGRAM_NAME)} FILENAME..." | |
exit 0 | |
end | |
ARGV.each do |f| | |
stats = len(f) | |
hours += stats[:hours] | |
minutes += stats[:minutes] | |
seconds += stats[:seconds] | |
end | |
seconds = seconds.round | |
if seconds >= 60 | |
minutes += seconds / 60 | |
seconds = seconds % 60 | |
end | |
if minutes >= 60 | |
hours += minutes / 60 | |
minutes = minutes % 60 | |
end | |
days, hours = hours.divmod(24) if hours >= 24 | |
weeks, days = days.divmod(7) if days >= 7 | |
message = "Total duration: " | |
message << "#{weeks} weeks, " unless weeks.zero? | |
message << "#{days} days, " unless days.zero? | |
message << "#{hours} hours, " unless hours.zero? | |
message << "#{minutes} minutes, " unless minutes.zero? | |
message << "#{seconds} seconds." | |
puts message | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment