Sub topic 11
Rendering straight to video with FFmpeg
Blender can encode each rendered frame into a video file as the render runs, using the FFmpeg library built into it. This chapter explains the settings that control that encoding and the trade-offs involved. It also covers the cases where a single video file is the wrong target.
This chapter assumes you can render a still image and an image sequence. It also assumes you know the frame range, frame rate and output path settings from the earlier chapters, and that you can run Python in Blender's Scripting workspace.
How direct video output works
When the file format is an image type such as PNG, Blender writes one file per frame. When you set the file format to FFmpeg Video, Blender opens one video file at the first frame. It then sends every finished frame to the encoder, and it closes the file after the last frame.
Three consequences follow from this design.
- The output is one file, so a crash or cancel near the end leaves a damaged or incomplete file.
- Frames are not stored as separate images. If you want to fix frame 87 only, you must re-render the whole animation.
- The encoder is lossy for most codec choices, so each re-encode of the result costs quality.
Container and codec are two choices
A container is the file wrapper. It holds the video stream, optional audio streams and metadata, and it sets the file extension. A codec is the algorithm that compresses the picture data. Blender asks for both, and not every pair is valid.
| Container (`ffmpeg.format`) | Extension | Common codecs | Typical use |
|---|---|---|---|
| `MPEG4` | .mp4 | `H264` | Sharing, web, most players |
| `MKV` | .mkv | `H264`, `VP9` | Robust to interrupted writes, flexible audio |
| `WEBM` | .webm | `WEBM` (VP9) | Browsers |
| `QUICKTIME` | .mov | `H264`, `PRORES` | Handoff to editing software |
| `AVI` | .avi | `H264`, `PNG` | Legacy workflows |
The exact list of codecs depends on your Blender version and build. Check the dropdown in Output properties > Output > Encoding, or print bpy.types.FFmpegSettings.bl_rna.properties['codec'].enum_items.keys() in the Python console.
Quality settings
For H.264 and similar codecs, the main quality control is the Output Quality setting. In Python it is constant_rate_factor. It sets a target visual quality and lets the file size vary with scene complexity. Its values are LOSSLESS, PERC_LOSSLESS, HIGH, MEDIUM, LOW, VERYLOW and LOWEST. MEDIUM is a reasonable default for review clips. Use HIGH or PERC_LOSSLESS for final deliveries.
The alternative is a fixed bitrate. You set it by assigning constant_rate_factor = 'NONE' and then setting video_bitrate, in kilobits per second. A fixed bitrate gives predictable file sizes, which matters for streaming limits. The cost is that complex scenes look worse and simple scenes waste bits.
Two further settings affect speed and size:
ffmpeg_presettakesBEST,GOODorREALTIME. Slower presets spend more encoding time to produce a smaller file at the same quality. Rendering time is usually dominated by the render engine, soGOODis a sensible choice.gop_sizeis the keyframe interval, the number of frames between full reference frames. The default is 18. Lower values make scrubbing in a player smoother but make the file larger.
A complete scripted example
This script renders frames 1 to 120 at 24 fps to an H.264 video in an MP4 container, with no audio.
import bpy
scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 120
scene.render.fps = 24
scene.render.image_settings.file_format = 'FFMPEG'
scene.render.ffmpeg.format = 'MPEG4'
scene.render.ffmpeg.codec = 'H264'
scene.render.ffmpeg.constant_rate_factor = 'MEDIUM'
scene.render.ffmpeg.audio_codec = 'NONE'
scene.render.filepath = '//renders/clip_'
bpy.ops.render.render(animation=True)Setting file_format to 'FFMPEG' must come first. The ffmpeg settings still exist when another format is selected, but they have no effect until the format is FFMPEG.
The // prefix means "relative to the folder of the saved .blend file". An unsaved file has no such folder, so save the file before rendering with a relative path.
Output file naming
You gave the path //renders/clip_, with no # characters and no extension. Blender adds the extension for the container, and for movies it normally also adds the frame range. The result here is a file such as renders/clip_0001-0120.mp4. The exact name can vary a little between versions, so look in the folder after the first render and do not depend on the name in scripts that process the output.
A video render does not create a numbered file per frame, so frame-number padding with # does not apply to the video itself. If the file already exists, a new render replaces it. Include a version number in the path (clip_v02_) to keep earlier takes.
Color and size limits
Most H.264 players expect the pixel format YUV 4:2:0. This format stores color at half the resolution in each direction, so the width and height must be even numbers. An odd resolution such as 1921×1081 can cause an encoding error or a one-pixel crop. Check the Resolution X/Y and % values together. For example, 1081 × 50% gives 540.5, which Blender rounds, so confirm the final size shown in the render.
Standard H.264 video has no alpha channel. Set image_settings.color_mode to 'RGB'. If you need transparency, use an image sequence with PNG (RGBA), or a codec and container that support alpha, such as ProRes 4444 in QuickTime where your build offers it.
Choosing between direct video and an image sequence
| Situation | Better choice | Reason |
|---|---|---|
| Quick preview of motion | Direct video | One step, small file |
| Long or overnight render | Image sequence | You can resume and fix single frames |
| Several final formats needed | Image sequence | Encode each format from the same frames |
| Compositing in another program | Image sequence (EXR or PNG) | No compression loss between steps |
| Render farm with several machines | Image sequence | Machines cannot write to one video together |
Sequence-to-video assembly is covered in the next chapter.
Audio
Direct video output can include sound from the Video Sequencer. Set audio_codec to a codec such as 'AAC' and add a sound strip to the scene's sequencer. Without a strip, the file has no audio track, even with an audio codec selected. For MP4 with H.264, AAC is the standard choice. Setting 'NONE' writes a silent video.
Running from the command line
The same settings can be used from a terminal. The format flag selects the movie output, and the codec settings come from the .blend file.
blender -b scene.blend -o //renders/clip_ -F FFMPEG -x 1 -s 1 -e 120 -aPut -o before -a, because Blender reads its options in order and renders when it reaches the render option. Set the container, codec and quality in the .blend file before running this, because the command line has no flags for them.
Common problems
- The file is created but will not play. The render was interrupted, or the codec and container do not fit each other. Re-render to completion, or use MKV, which tolerates interruption better.
- The video is the wrong speed. The scene frame rate (
fps) is wrong. Frame rate is stored in the file, so change it before rendering. - The video is longer or shorter than expected. Check
frame_startandframe_end. They are both inclusive, so 1 to 120 gives 120 frames, which is 5 seconds at 24 fps. - Colors look washed out or too saturated in players. The view transform (Filmic, AgX or Standard) affects the output. For video that goes to the web, use Standard or AgX deliberately and compare against a player.
Exercise
You need a 10-second H.264 MP4 at 30 fps, starting at frame 1. Which values do you set for frame_end and fps? Which file_format and ffmpeg.format do you use? Why is a 1281×721 resolution a problem?
Show answer
Set fps to 30 and frame_end to 300, since 10 s × 30 fps = 300 frames, and both ends of the range are inclusive. Set image_settings.file_format = 'FFMPEG', ffmpeg.format = 'MPEG4' and ffmpeg.codec = 'H264'. A 1281×721 resolution has odd dimensions, and H.264 with YUV 4:2:0 color needs even width and height, so the encoder can fail or crop a pixel. Use 1280×720 instead.
Exercise
A 40-minute animation renders to a single MP4 overnight and Blender crashes at minute 35. What workflow change avoids losing the work?
Show answer
Render to an image sequence (PNG or OpenEXR) instead. The finished frames stay on disk after a crash, so you can restart from the first missing frame, and then encode the whole sequence to MP4 afterwards.