I was hoping to render blender using the command line mode on linux. However, I do not no how to set the output resolution and H.264 as the encoding. Both these options work in the GUI mode but I wish to use them from the command line
1 Answer
Command line
One way is to set the properties from the gui, save the file, then render. The settings will be preserved for rendering from the command line.
Note that (as already mentioned by iKlsR) you can set the render output format from the command line with -F
:
Format Options:
-F or --render-format <format>
Set the render format, Valid options are...
TGA IRIS JPEG MOVIE IRIZ RAWTGA
AVIRAW AVIJPEG PNG BMP FRAMESERVER
(formats that can be compiled into blender, not available on all systems)
HDR TIFF EXR MULTILAYER MPEG AVICODEC QUICKTIME CINEON DPX DDS
However, it seems H264
is not a valid setting for this argument.
Further reading: https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html#python-options
Python
Another way is to use blender's python API, and run a python script in blender from the command line (see this post), e.g.:
blender --background /path/to/file.blend --python /path/to/script.py --render-anim
or
blender -b /path/to/file.blend -P /path/to/script.py -a
The render resolution can be set with bpy.context.scene.render.resolution_x
and bpy.context.scene.render.resolution_y
.
The output format can be set to H.264 with
bpy.context.scene.render.image_settings.file_format = 'H264'
.
Note on scene values:
You may want to assign the settings for all scenes to avoid confusion with complex setups which use multiple scenes.
for scene in bpy.data.scenes:
scene.render.resolution_x = 1920
scene.render.resolution_y = 1080
scene.render.image_settings.file_format = 'H264'
Note invoking render:
When using a Python script to render you have the choice to...
- Pass the
--render-anim
argument. - Call
bpy.ops.render.render(animation=True)
directly from the Python script.
Both do the same thing however calling the operator from Python means if the script fails to run, render won't execute, This may be good/bad depending on your use-case.
Calling from Python also gives some flexibility - for example you could call render multiple times with different frame ranges.
-
2$\begingroup$ Note: to avoid creating a script file just for this, a python expression can be passed directly at the command line (the \n should be replaced with actual newlines):
blender -b path/to/file.blend --python-expr "import bpy\n for scene in bpy.data.scenes:\n scene.render.resolution_x = 1920\n scene.render.resolution_y = 1080\n scene.render.image_settings.file_format = 'H264'" -a
$\endgroup$ Commented Oct 27, 2016 at 4:44 -
$\begingroup$ instead of /n you can use semicolons ; $\endgroup$ Commented Apr 27, 2023 at 8:51
H.264
to-F
. $\endgroup$render-batch
script in this repo does exactly that. $\endgroup$