27

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.

2
  • Answer stackoverflow.com/a/15052662/3102264 explain how to copy without re-encoding (ffmpeg -i input.mkv -c copy -map 0 output.mp4)
    – mpromonet
    Commented Aug 30, 2014 at 8:28
  • @mpromonet if you want to preserve the codec, -c copy is fine but to convert the codec and keep the same quality, I had to use -qscale 0 like I read in the answers.
    – baptx
    Commented Jun 8, 2019 at 17:39

5 Answers 5

29

Instead of -sameq (removed by FFMpeg), use -qscale 0 : the file size will increase but it will preserve the quality.

4
  • 12
    -qscale, or more accurately -qscale:v, should only be used with the legacy MPEG family: mjpeg, mpeg1video, mpeg2video, mpeg4, etc. It is ignored by libx264 and libx265 which use -crf instead.
    – llogan
    Commented Jan 5, 2018 at 18:18
  • @LordNeckbeard Wait, so what if you are converting from mpeg4 (e.g. mp4v) to H.264, which involves both?
    – Michael
    Commented Jan 20, 2018 at 1:26
  • 2
    @Michael The input format does not matter: only the encoder being used to make the output matters. This is because ffmpeg will decompose the input into raw formats no matter the input format. Then the encoder will process the raw formats into the output. (Unless you are stream copying with -c copy of course then no re-encoding occurs).
    – llogan
    Commented Jan 22, 2018 at 18:52
  • 5
    ffmpeg 4.2.1 throws a yellow "Please use -q:a or -q:v, -qscale is ambiguous" message when using -qscale 0
    – Don Wilson
    Commented Sep 12, 2019 at 19:00
28

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.

4
  • 9
    It would be nice to clarify what CRF is, what it do and how to use this parameter. CRF stands for Constant Rate Factor. Quote from ffmpeg docs: "The range of the CRF scale is 0–51, where 0 is lossless, 23 is the default, and 51 is worst quality possible. A lower value generally leads to higher quality, and a subjectively sane range is 17–28. Consider 17 or 18 to be visually lossless or nearly so." Useful links: ffmpeg docs, Understanding Rate Control Modes
    – mosov.a
    Commented Nov 8, 2018 at 9:53
  • @AlexanderMosov There's a link to the wiki you quoted in my answer.
    – llogan
    Commented Nov 8, 2018 at 20:32
  • 4
    But this "answer" completely misses the point. The point is not to create a "lossless" video, as the original video might have been very lossy anyway. The point is to create a video with the same degree of lossiness as the original video. Commented Jan 3, 2022 at 13:30
  • So do you have any tips on which CRF factor should I choose to match the quality of the old video? For example can I somehow find the CRF of the old video and if I encode a new one with the same CRF does it look like the same quality? Commented Aug 29, 2023 at 0:57
7

convert all mkv to mp4 without quality loss (actually it is only re-packaging):

for %a in ("*.mkv") do ffmpeg.exe -i "%a" -vcodec copy -acodec copy -scodec mov_text "%~na.mp4"
2
  • 2
    For me a simple ffmpeg -i input_file.mkv output_file.mp4 did the trick. Commented Nov 21, 2016 at 12:14
  • 2
    @ThiagoNegri that will re-encode everything, however; utilizing 'copy' you can potentially avoid doing that Commented Jul 9, 2018 at 16:13
4

For me that was the best way to convert it.

ffmpeg -i {input} -vcodec copy {output}

I am writing a script in python that appends multiple .webm files to one .mp4. It was taking me 10 to 20 seconds to convert one chunk of 5 seconds using:

ffmpeg -i {input} -qscale 0 copy {output}

There's some folders with more than 500 chunks.

Now it takes less than a second per chunk. It took me 5 minutes to convert a 1:20:00 long video.

1

For MP3, the best is to use -q:a 0 (same as '-qscale 0'), but MP3 has always loss quality.

To have less loss quality, use FLAC

See this documentation link

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