Friday, January 24, 2014

Blender + FFMPEG - Notes on Getting Usable Video Output

Everytime I work on a video project after not doing so for a while, I'm eventually reminded of just how sucky the situation with Blender + FFMPEG video output really is. In many ways, it is really quite a frustrating exercise, though admittedly already much better than it was a few years ago. (For starters, it now crashes a hell of a lot less, though I've also only been trying relatively "common" and "sane" format configurations this time).

This time, I had specific requirements for the types of acceptable formats I needed to output to, which helped narrow down the field a bit. Specifically, I needed .mpg, .mp4, or .mov output (with the associated "default" codec/encoding schemes associated with those). To keep things simple, the aspect ratio used was "basically" HD (i.e. 75% HD, to keep the rendertimes on some of the more intensive shots down).

In this post, I list out the results of some experiments I did to find something that would work well enough across various players.



Summary of Key Takeaways
- Blender's default FFMPEG video output defaults/presets are basically still crap. They kindof work, but only in a FFMPEG-centric world, and only if you're willing to accept slow rendering times and crappy resultant video quality for all that effort.

- Use MPEG as the Output format (Output Panel), with MPEG-4 as the "Format" (Encoding Panel)

- Bitrate (Encoding Panel, below Format) should be bumped up to at least 24300k, and the range changed to min=0k, max=30000k to accommodate this.

- AAC for Audio - everything else there at default settings - works good enough.

- It may be necessary to re-encode the resulting video in another piece of software which manages to read it in this state, as only a handful of players can actually cope with this output. Notably, QuickTime doesn't play this output video, but Youtube accepts it happily.
 
  - Windows Movie Maker (using the "HD" option) works well for doing this. However, be aware that doing so results in the file size doubling.

- As usual, Blender's Wiki documentation on this is lacking. In this case, while it mentions the existence of each of these options, little is done in the way of how to go about holistically choosing a format + how to go about fine tuning the parameters (which are only mentioned here - the same as listing out the name + tooltip-description for each only)


A Basic Guide to Video Format Issues (May Not be Totally Correct!)
From what I've seen in various places (particularly a few helpful posts over on BlenderArtists), basically, there are two parts to a video file format:
   1) The "Container" - This is basically the skeleton format used to host the video content, and in most cases simply equates to what gives a video file its proper extension  - *.AVI *.MPG, *.MP4, and *.MOV.

  2) The "Encoding" or "Format" - This is the technique used for compressing the video frames so that they don't take several GB's for just a few seconds of content. For our purposes, the most relevant thing here is the H.264 codec, which is basically the standard format used for most good-to-high-quality video these days.


Bad Configurations - Raw(H264, AVI/H264)
Settings:
This is the default config when choosing "H264" in the Output Presets

Results:
See below (H264, H264) - This config is essentially the same.

Verdict:
Completely and utterly crap/useless

Bad Configurations - Raw(H264, H264)
Settings:
- Output = H264
- Format = H264

Results:
- An .avi  file is produced
- This is only playable in VLC (and by extension, other FFMPEG based players).
- Windows Media Player and QuickTime refuse to play this file, and will just immediately spew errors.

Verdict:
Completely and utterly crap/useless

Bad Configurations - Raw(MPEG, MPEG-2)
Settings:
This is the default config when choosing "MPEG" in the Output Presets

Results:
- A .mpg file is produced, but the video quality is utterly crap
- The video hardly manages to play on anything at all. Even VLC is a bit hard pressed to play it properly.
- Apparently MPEG-2 is the old DVD spec. It simply looks awful.

Verdict:
Avoid!

Bad Configurations - Raw(MPEG, H264)
Settings:
- Output = MPEG
- Format = H264

Results:
- A .avi file is produced
- This is only playable in VLC (and by extension, other FFMPEG based players)
- Windows Media Player and QuickTime complain about this file. WMP will play the file with a black screen (but audio works), while QuickTime just gets glitchy and unresponsive.

Verdict:
Completely and utterly crap/useless


OK Base Configuration - Raw(MPEG, MPEG-4)
Settings:
- Output = MPEG
- Format = MPEG-4

Results:
- A .mp4 file is produced. (The codec used for MPEG-4 is actually H264, according to Wikipedia and when checking the files)
- VLC and FFMPEG players play this perfectly fine as usual
- WMP plays the file, but the colours are all off kilter. Specifically, it looks like someone cranked up the brightness + saturation, then clipped the results to lower the bitrate or so.
- QuickTime as usual refuses to play the file at all.

Verdict:
A promising base configuration. This is already OK for uploading to Youtube as-is, though a few more tweaks would make it better.


Bad Modified Configuration - WMM "For Computer" (MPEG, MPEG-4)
Setup:
- Raw video output as per the "OK Base Configuration" - Output=MPEG, Format=MPEG-4
- Raw video imported into Windows Movie Maker
- Exported again using "For Computer" option

Results:
- A .mp4 file is produced. This file is almost twice as large as the original.
- Playing back this file in VLC, there is now a 2-3 pixel GREEN LINE on the right hand side of the frame. Obviously some kind of encoding glitch!
- Windows Media Player still plays back with bad colours

Verdict:
Avoid. This just makes it worse.


Good Modified Configuration - WMM "HD" (MPEG, MPEG-4)
Setup:
- Raw video output as per the "OK Base Configuration" - Output=MPEG, Format=MPEG-4
- Raw video imported into Windows Movie Maker
- Exported again using the "HD" option

Results:
- A .mp4 file is produced. This file is almost twice as large as the original
- VLC plays back the file perfectly fine
- Windows Media Player still plays with bad colours
- QuickTime works!

Verdict:
This seems to be the best way of getting "generally portable" videos out so far (with sound included).


Bitrate/Encoding Settings
A note should also be made about the bitrate settings used for the encoding process.

The Windows Movie Maker exports all use bitrates of around 24100k, while the default in Blender is 6000k (with a range of min=0, max=9000). If we're going to be passing our output through that, then in theory, we don't want our raw output to end up looking worse than what the final step will produce, which intuition says that a low bitrate should cause.

However, even if we don't need to pass the video through a second tool, we still don't want to use the default bitrates. That's because the default bitrate settings result in rather crappy video quality - with lots of banding and blocky compression artifacts all around the place. Another notable disadvantage is that with the default bitrate settings, rendering out the video TAKES AGES (i.e. on the order of 1-2 seconds per frame).


Good Settings:
- Bitrate = 24300k
- Range - Min = 0k, Max = 30000k

Rationale:
- These settings were chosen to more closely match those used by Windows Movie Maker, which seems to produce acceptable video quality (for basic videos at least)
- The max range needed to be bumped up too (or else you'll find that Blender renders the whole sequence, only for it to then complain when it reaches the end that the bitrate range was too small).
- The final value was taken based on the relationship between these values as used in the defaults. I really don't know what I'm doing here, so it's possible that better results could still be gained with values that are set better here
- Some resources I've read suggest something needing higher bitrates for videos with larger dimensions or something like that. 

Results:
- Video output quality is acceptable. Most banding and artifacts are gone, though not completely
- Video rendering speed sees a massive boost. We get perhaps 2-5 frames rendered per second.


Conclusion
Hopefully this post acts as a good resource/springboard for further systematic investigations of how we can get reliable video exports from Blender. For the more experienced VSE users out there (you know who you are!), some more tips about some "good configurations" that you've found to work wonders in the past are always welcome.

Only when we know what some good settings are can we start to modify those in use in Blender now, which are still somewhat lacking (and honestly quite confusing to anyone who doesn't have intimate knowledge of all the different video format types).

6 comments:

  1. Just a big thanks for your information.
    I'm totally lost with all of this and you helped a lot !
    I love people who spare me some disappointment with my final work so in some kind of way I "love" you ^^ (not sure it doesn't sounds weird but I'm not english sorry).

    I bow before your generosity of sharing your knowledge :).

    ReplyDelete
  2. Yeah this was excellent! I was having issues with jagged egdes, and I figured the was something with the codec settings - but I didn't know what would produce a good result. Then I found your site! Thanks alto and Merry Christmas to you!

    /Magnus
    Sweden

    ReplyDelete
  3. its difficult to get a video out of blender with as good quality as it was when it went in. When I take videos with my sony rx100, and then edit them in blender I can never get them out with the same high quality. My conclusion is the blender VSE, despite many great features, sucks, I mean if you can't get a video out with top quality then all the bells and whistles don't mean a thing. I love Blender, but its really too bad that the developers are addicted to adding more and more new features instead of making whats already there work at an optimal level.

    ReplyDelete
  4. Lawrence D’OliveiroMarch 18, 2015 at 3:17 PM

    I never encode video directly from Blender. Instead, I have it output to a sequence of PNG files. I can then encode these with FFmpeg, which offers a much wider range of options for tweaking than Blender can ever manage.

    UTRTFTJ, as they say....

    ReplyDelete
  5. Informative about something I have toiled over too. I ended up rendering (pngs) to an Xvid container using the H.264 codec. This produces large files but faithfully reproduced video quality from the original pngs.

    Sometimes I render out the pngs from video, (and another source, individual frame Nikon RAW files) then batch adjust them in Raw Therapee. Tuning color, tone-mapping-noise reduction and what-ever else. Then I render them through VSE.

    If you produce large, good quality video, I understand that Handbrake does a very good job at copying to smaller formats while maintaining faithful quality.

    I will try your bit rate settings. Thanks

    ReplyDelete
  6. I have used:
    MPEG MPEG1 MP2 https://www.youtube.com/watch?v=TFeAne1jKR0

    H264 AVI H264 MP2

    H264 AVI H264 MP3 https://www.youtube.com/watch?v=nkJCn7Rsy5M

    I found the H264 to be sharper, but jerky. On testing this I found that Windows Media Player was playing the frames out of order (some frames would repeat) This did not happen if played inside Blender.

    Later I found that Adobe Premiere could not properly load the h264 frames. They were wildly out of order.

    But youtube seemed to manage to run all of the above.

    Constant frustration seeing beautifully clear png frames turned into fuzzy video or beautifully smooth movement becoming jerky video.

    ReplyDelete