25

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.

Jensej
  • 1,255
  • 6
  • 21
  • 33
  • Answer http://stackoverflow.com/a/15052662/3102264 explain how to copy without re-encoding (ffmpeg -i input.mkv -c copy -map 0 output.mp4) – mpromonet Aug 30 '14 at 08: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 Jun 08 '19 at 17:39

5 Answers5

28

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

A. Attia
  • 1,630
  • 3
  • 20
  • 29
  • 10
    `-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 Jan 05 '18 at 18:18
  • @LordNeckbeard Wait, so what if you are converting from mpeg4 (e.g. mp4v) to H.264, which involves both? – Michael Jan 20 '18 at 01: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 Jan 22 '18 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 Sep 12 '19 at 19:00
27

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
llogan
  • 121,796
  • 28
  • 232
  • 243
  • 8
    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](https://trac.ffmpeg.org/wiki/Encode/H.264), [Understanding Rate Control Modes](https://slhck.info/video/2017/03/01/rate-control.html) – mosov.a Nov 08 '18 at 09:53
  • @AlexanderMosov There's a link to the wiki you quoted in my answer. – llogan Nov 08 '18 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. – Vladimir Nikishkin Jan 03 '22 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? – Tuupertunut Aug 29 '23 at 00:57
6

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"
Opal
  • 81,889
  • 28
  • 189
  • 210
emetrue
  • 77
  • 1
  • 2
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.

Jon Martins
  • 65
  • 1
  • 4
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

Nicolas
  • 2,684
  • 1
  • 16
  • 22