rss

Mar
10

FFMPEG get video file duration: Ruby

Posted (Amit) in Ruby on Rails on 10-03-2009

3

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def get_video_file_duration(inputFilename)
            command = "ffmpeg -i "
                             + inputFilename.to_s + " 
                             2>&1 | grep 'Duration' 
                             | cut -d ' ' -f 4 | sed s/,//"
                             output = `#{command}`
 
          if output =~ /([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
               duration = (($1.to_i * 60 + $2.to_i) * 60 + $3.to_i) * 10 + $4.to_i
          end
 
          #return duration.to_s
          return "#{$2}:#{$3}"
 end

 

Share

Comments (3)

I’ve been looking for this..
thank you..

Thanks a lot Amit. One note though. I think it should be times a hundred, instead of ten, if you want the result in milliseconds, like this:
 
duration = (($1.to_i * 60 + $2.to_i) * 60 + $3.to_i) * 100 + $4.to_i

# …

return duration.to_s
#return "#{$2}:#{$3}"
If you rather want the result in seconds, it should be:

duration = (($1.to_i * 60 + $2.to_i) * 60 + $3.to_i) + ($4.to_i / 100)

return duration.to_s

#return "#{$2}:#{$3}"

Amit

This is nice, thanks for sharing :)

Post a comment



Amit Yadav is Stephen Fry proof thanks to caching by WP Super Cache