2

How can I delete videos, via terminal, by length of the video?

I have a cutoff point of 30 seconds, and I want to delete anything under that. If I select by size (find . -type f -size +27818098c) it doesn't give me accurate results, as the videos are different bitrates.

1 Answer 1

2

A bit hacky, but:

for f in *.avi; do 
  echo "Testing $f"
  ffmpeg -i "$f" 2>&1 | awk '/Duration/ { split($2,a,":"); exit a[1]*3600+a[2]*60+a[3] < 30 ? 3 : O }'
  [[ $? -eq 3 ]] && rm -i -- "$f"
done

This assumes you have ffmpeg installed. rm will ask you before removing a file, but you can remove the -i if you're sure it does what you want.

A portable Ruby script that will not ask for confirmation – but you can replace f.delete with puts f to check what it would do:

ruby <<"EOF"
  Dir.glob("*.avi").each { |f| 
    ff = %x( ffmpeg -i "#{f}" 2>&1 )
    d = ff.match(/Duration: ([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2})/)[1].split(":").map{|s| s.to_i}
    s = (d[0]*60*60 + d[1]*60 + d[2])
    f.delete if s < 30 
  }
EOF
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .