9

I know its a "lame" question, but: lets say, I have a wmv, avi, etc BIG file. I want to convert it to .MP4, with the possible smallest file size, and of course with the same quality with audio/video. How to? What codec should I use? Im using FFmpeg

1
  • 1
    You... can't. Shrinking the file will reduce quality. This is how video files are made smaller. H.264 is an encoding that has the least-noticeable quality loss. Jun 26 '15 at 21:09
20

You can try to encode with libx264 CRF 18. The CRF parameter sets the quality and influences the file size. Lower values mean higher quality, and typical values are from 18 to 28. The default is 23.

CRF 18 is well known for producing a (arguably) "visually lossless" result:

ffmpeg -i input.avi -c:v libx264 -crf 18 -preset veryslow -c:a copy out.mp4

Notice that I used a veryslow preset which will give you the smallest file possible. You can leave it out if you want to encode faster, but the file size will be larger, too.

Also, I decided to copy the audio track instead of reencoding it (-c:a copy), mainly because I assume it's already AAC/MP3 stereo in your sample.

If the output is too big for you, try with a higher CRF value (up to 23) but the quality will suffer.

1
  • 3
    Old habits die hard.
    – Ely
    Jun 28 '15 at 12:02
6

Getting the smallest video with the least amount of quality lost is more of a challenge than anything else. There are guides scattered among the Web that will recommend you settings to use on FFmpeg for things like game captures, streams, DVD rips, etc.

If you want lossless lossless, try this:

ffmpeg -i input.mp4 -c:v libx264 -crf 0 output.mp4

The important part is the -crf 0 part which sets it to lossless. However, the output file will generally be very large. You could change it to -crf 18 which would give you something that is acceptable in quality and free from most artifacts.

2
  • 3
    FYI - according to the FFmpeg wiki - using -qp 0 is recommended for lossless libx264 over -crf 0 due to some difference between 8bit and 10bit x264. Probably not a big deal either way, but I've defaulted to using -qp 0 with good results. That all said, it is not truly "lossless"... There are minute differences between the libx264 outputs and the originals that generated them. Impossible to see with the naked eye, but they are measurable differences... - For above wiki reference, see: trac.ffmpeg.org/wiki/Encode/H.264#LosslessH.264
    – occvtech
    Jul 1 '15 at 15:43
  • 1
    I don't know where you got that last part, but x264 lossless is mathematically lossless. Try running an md5sum on a raw input and the decoded lossless H.264 stream, they'll be identical.
    – Ely
    Jul 2 '15 at 1:33
1

2021

H265 / HEVC version :

ffmpeg -i input.mp4 -vcodec libx265 -crf 18 -tag:v hvc1 -preset veryslow -an output.mp4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.