103

I have a set of video frames saved as images in a directory, and I'm trying to encode these to a good quality video, however every setting and every format I try produces very noticeable artifacts.

The basic command is this:

ffmpeg -r 25 -i %4d.png myvideo.mpg

and I've tried the minrate and maxrate flags. Any of {mpg, avi, mov, flv} formats will do.

Any suggestions for settings? Final file size is not an issue.

4
  • 7
    On my Mac, I use the following command: ffmpeg -r 20 -f image2 -i myImage%04d.png -f mp4 -q:v 0 -vcodec mpeg4 -r 20 myVideo.mp4. I found that to force the framerate, I had to specify it for both the input and the output files. This codec seems to work for me -- you may not need to specify it. I just wanted to include what I did because I've spent a lot of time working on it. The only downside is that my input images are high-contrast (blue and red balls against a white background) and later in the movie, the balls begin to all become the same dark color -- an unwanted contrast effect.
    – jvriesem
    Nov 22, 2013 at 19:28
  • I suggest to ask this question in StackOverflow for Multimedia Systems
    – Hamed
    Sep 19, 2015 at 10:05
  • c++ version stackoverflow.com/a/43464269/6180077 visit this link for working FFMPEG c++ mp4 format screen recorder application. May 9, 2017 at 4:38
  • check this link which demonstrates the writing video file using FFMPEG libs programatically : stackoverflow.com/a/43464269/6180077 Aug 9, 2017 at 4:36

4 Answers 4

70

A couple of things:

  • You need to set the video bitrate. I have never used minrate and maxrate so I don't know how exactly they work, but by setting the bitrate using the -b switch, I am able to get high quality video. You need to come up with a bitrate that offers a good tradeoff between compression and video quality. You may have to experiment with this because it all depends on the frame size, frame rate and the amount of motion in the content of your video. Keep in mind that DVD tends to be around 4-5 Mbit/s on average for 720x480, so I usually start from there and decide whether I need more or less and then just experiment. For example, you could add -b 5000k to the command line to get more or less DVD video bitrate.

  • You need to specify a video codec. If you don't, ffmpeg will default to MPEG-1 which is quite old and does not provide near the amount of compression as MPEG-4 or H.264. If your ffmpeg version is built with libx264 support, you can specify -vcodec libx264 as part of the command line. Otherwise -vcodec mpeg4 will also do a better job than MPEG-1, but not as well as x264.

  • There are a lot of other advanced options that will help you squeeze out the best quality at the lowest bitrates. Take a look here for some examples.

2
  • 8
    Constant bitrate is bad for quality. Using constant rate factor is superior (it's one of x264 modes of operation). For example: ffmpeg -i … -c:a copy -c:v libx264 -crf 18 -preset veryslow …. 18 is the CRF with very marginal quality loss, but bitrate will be probably low. And you can try bigger CRF values if you need smaller file size. And as you see, this is pretty simple. Jun 23, 2015 at 10:23
  • note that -b has to be before the video but after the input -i
    – mateuszb
    Sep 18, 2017 at 15:22
53

You need to specify the -vb option to increase the video bitrate, otherwise you get the default which produces smaller videos but with more artifacts.

Try something like this:

ffmpeg -r 25 -i %4d.png -vb 20M myvideo.mpg

1
  • I used the command in this answer for making time-lapse videos for a few years, but I found that occasionally, especially with night scenes, the video would have really bad quality. I found this answer again while looking up why, and I tried using the answer in the comment by Display Name, and it works MUCH better than this. All I changed was to put -crf 18 in the command in place of -vb 20M.
    – htmlcat
    Feb 24 at 7:35
19

Make sure the PNGs are fully opaque before creating the video

e.g. with imagemagick, give them a black background:

convert 0.png -background black -flatten +matte 0_opaque.png

From my tests, no bitrate or codec is sufficient to make the video look good if you feed ffmpeg PNGs with transparency

3
  • This solved the problem for me. Nov 30, 2019 at 15:38
  • This hint solved a really annoying low video quality. Thanks a lot.
    – dasmy
    Oct 13, 2020 at 13:09
  • 1
    What if one is producing a video with transparency?
    – Majiick
    Aug 6, 2021 at 10:19
0

Unless you do some kind of post-processing work, the video will never be better than the original frames. Also just like a flip-book, if you have a big "jump" between keyframes it will look funny. You generally need enough "tweens" in between the keyframes to give smooth animation. HTH

2
  • The images are from a video sequence originally. They've just had a bit of cropping, scaling and whatnot done with imagemagick. The effects are definitely compression artifacts. I've now seen this post, which seems to have an answer: stackoverflow.com/questions/3158235/…
    – CakeMaster
    Aug 24, 2010 at 23:26
  • @CakeMaster I didn't mention lossy compression because you have ".png" images in your example. .png's can be lossy, but I find they usually are not. .jpg's are almost always lossy therefore the artifacts when using them. If you can, you might resave your .png's with lossless compression.
    – JustBoo
    Aug 24, 2010 at 23:30

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