Ubuntu – Optimize video filesize without quality loss

command linecompressionvideo

Is there a simple way (on the command line – I want to write a script which compresses all videos in a folder) to reduce the filesize of a video (almost) without quality loss? Is there a method which works equally well for different video format (mp4, flv, m4v, mpg, mov, avi)?

I should mention that most of the videos I would like to compress are downloaded web-videos (mp4, flv), so it's not clear if there is much room for further compression.

Best Answer

In the mystical land of the PNG, the most effective way of near-lossless compression is to compress the image with almost every possible combination of settings and compare the output. This is what applications like pngcrush do.

There's no reason why a similar process wouldn't work, on paper. In practice, there are a few issues:

  • Compressing video once is already a much longer process than encoding a PNG image.
  • There are about 1000 times as many configuration combinations.
  • Those two conspire to make the process exponentially longer by several powers.
  • It would also use an unreasonable amount of disk space, memory and CPU time.
  • Frames blend if there's a framerate change and obviously change if you change the frame size or crop, making comparison process even harder

To top it all off, you're dealing with content that's already poisoned with artefacts and encoding sludge. My simple answer for "Is there a simple way" has to be No.

But if you have a shed load of low-compression videos like FLV (a fairly rubbish compression in my experience with the format), it might be worth having a go with ffmpeg or mencoder for the FLVs. Something like this might work:

find -iname '*.flv' -exec \
    mencoder {} -o {}.recomp.avi -ovc x264 -x264encopts threads=9:bitrate=400 -oac mp3lame -lameopts abr:br=52 \;

But there will be loss. You just have to judge how much is acceptable.