取得影音檔的時間長度
由命令列
$ ffmpeg -i 0304.mp4 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//
或是
$ ffmpeg -i 0304.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ,
使用 ruby
def get_video_file_duration(inputFilename)
cmd = "ffmpeg -i " + inputFilename.to_s
cmd += " 2>&1 | grep 'Duration' "
cmd += " | cut -d ' ' -f 4 | sed s/,//"
output = `#{cmd}`
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 "#{$2}:#{$3}"
end
fname = "2004100321.wmv"
dur = get_video_file_duration(fname)
puts dur