17

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

4
  • 2
    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, 2015 at 21:09
  • see also YIFY-quality encoding
    – milahu
    Jan 16, 2023 at 18:05
  • the only LAME question when it comes to ffmpeg is how to encode mp3s! Nov 23, 2023 at 7:47
  • ...I'll see myself out 😅 Nov 23, 2023 at 7:47

3 Answers 3

29

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.

2
  • 3
    Old habits die hard.
    – Ely
    Jun 28, 2015 at 12:02
  • 2
    veryslow doesn't give the smallest size possible. Very slow will improve quality at the cost of encoding time and file size. I often find veryslow actually results in larger file sizes than veryfast.
    – CamHart
    Jan 11, 2022 at 7:56
8

2021

H265 / HEVC version :

ffmpeg -i input.mp4 -vcodec libx265 -crf 18 -tag:v hvc1 -preset veryslow output.mp4
2
  • 2
    Note: -an will discard the audio. Don't use this flag if you want audio in your output. Aug 8, 2023 at 13:21
  • 1
    Oh good catch thanks, that wasn't supposed to be there I guess. Removed it 👍
    – Xys
    Aug 9, 2023 at 9:44
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, 2015 at 15:43
  • 2
    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, 2015 at 1:33

You must log in to answer this question.

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