47

I would like to use ffmpeg to transform an mp4 video file into avi but with the same quality, even if it takes up more space. If I simply do:

ffmpeg -i file.mp4 file.mp4.avi

The resulting avi file is very low-quality and pixelated. How can I do this transformation while keeping the video and audio quality?

Braiam
  • 67,791
  • 32
  • 179
  • 269
719016
  • 6,217
  • 3
    You can use -sameq switch, which will put same video codec (assuming H264) in AVI container. If your player does not support H264 in AVI, then you need to transcode best possible video format (H264) to some mediocre format, which is no good. Then you assign higher video bitrate XVID (let's say 2000 Kb/s). Audio doesn't matter that much IMHO, but better to convert to MP3 then AC3 – zetah Nov 27 '11 at 14:16

3 Answers3

26

My solution:

ffmpeg -i filename.mp4 -vcodec copy -acodec copy filename.avi

Enjoy!!

kuba
  • 277
  • 3
    Be aware that embedding AVC streams in AVI containers requires a hack that is supported by many community-driven video players (e. g. VLC, MPlayer, anything FFmpeg/Libav or GStreamer, Media Player Classic) but not by many commercial players (e. g. Windows Media Player and QuickTime/iTunes don't support it). – David Foerster Nov 27 '16 at 10:07
  • This requires additional codecs – mLstudent33 Mar 05 '20 at 08:50
  • Does it mean that doing that command i am reducing chances of playing that file on some devices like older SMART TVs? – 16851556 Feb 18 '21 at 18:15
24

You always lose a little quality whenever you transcode from one codec to another, video or audio, but perhaps you can avoid it if you only want to change from MP4 container type to an AVI container. codec:copy may be helpful if you don't have a reason to transcode (and it should be faster).

If you must transcode because you need to use a particular video codec I hope you can use zetah's suggestion above in his comment and use the -sameq switch. Alternatively, you may want to use the qscale option to set the quality manually. The lower the number the better the quality, but the more space your file will take.

The last time I transcoded video I used qscale=8.0, but I'd suggest that you experiment to find the optimum to match the quality of your input.

Marty Jay helpfully mentions that sameq means same quantizer as in the input, which may not result in the same quality. The article he quotes mentions using multi-pass conversion, which is a good way to achieve better compression without sacrificing quality.

John S Gruber
  • 13,336
  • Thanks for the tips, I'll try -sameq see how it goes. – 719016 May 19 '12 at 14:53
  • 9
    -sameq does NOT mean same quality; http://ffmpeg.org/trac/ffmpeg/wiki/Option%20%27-sameq%27%20does%20NOT%20mean%20%27same%20quality%27 –  Oct 23 '12 at 19:58
  • 3
    It seems to be recommended to use the -qscale 0 option – user989762 Mar 06 '15 at 02:08
  • Fairly old, but I got this warning trying to do this today: Option 'sameq' was removed. If you are looking for an option to preserve the quality (which is not what -sameq was for), use -qscale 0 or an equivalent quality factor option. – Dave Lugg Apr 17 '18 at 04:37
4

Here is my 2-pass (Advanced Simple Profile) I use now and then.

pass 1:

ffmpeg -i file.mp4 -vcodec mpeg4 -vtag XVID -b 990k -bf 2 -g 300 -s 640x360 -pass 1 -an -threads 0 -f rawvideo -y /dev/null

pass 2:

ffmpeg -i file.mp4 -vcodec mpeg4 -vtag XVID -b 990k -bf 2 -g 300 -s 640x360 -acodec libmp3lame -ab 128k -ar 48000 -ac 2 -pass 2 -threads 0 -f avi file.avi
David Foerster
  • 36,264
  • 56
  • 94
  • 147
duffydack
  • 7,344