ffmpeg convert without loss quality

Jensej picture Jensej · Aug 29, 2014 · Viewed 58.5k times · Source

I need convert all videos to my video player (in website) when file type is other than flv/mp4/webm.

When I use: ffmpeg -i filename.mkv -sameq -ar 22050 filename.mp4 :

[h264 @ 0x645ee0] error while decoding MB 22 1, bytestream (8786)

My point is, what I should do, when I need convert file type: .mkv and other(not supported by jwplayer) to flv/mp4 without quality loss.

Answer

llogan picture llogan · Aug 29, 2014

Do not use -sameq, it does not mean "same quality"

This option has been removed from FFmpeg a while ago. This means you are using an outdated build.

Use the -crf option instead when encoding with libx264. This is the H.264 video encoder used by ffmepg and, if available, is the default encoder for MP4 output. See the FFmpeg H.264 Video Encoding Guide for more info on that.

Get a recent ffmpeg

Go to the FFmpeg Download page and get a build there. There are options for Linux, OS X, and Windows. Or you can follow one of the FFmpeg Compile Guides. Because FFmpeg development is so active it is always recommended that you use the newest version that is practical for you to use.

You're going to have to accept some quality loss

You can produce a lossless output with libx264, but that will likely create absolutely huge files and may not be decodeable by the browser and/or be supported by JW Player (I've never tried).

The good news is that you can create a video that is roughly visually lossless. Again, the files may be somewhat large, but you need to make a choice between quality and file size.

With -crf choose a value between 18 to around 29. Choose the highest number that still gives an acceptable quality. Use that value for your videos.

Other things

  • Add -movflags +faststart. This will relocate the moov atom from the end of the file to the beginning. This will allow the video to begin playback while it is still being downloaded. Otherwise the whole video must be completely downloaded before it can begin playing.

  • Add -pix_fmt yuv420p. This will ensure a chroma subsampling that is compatible for all players. Otherwise, ffmpeg, by default and depending on several factors, will attempt to minimize or avoid chroma subsampling and the result is often not playable by non-FFmpeg based players.