Video Recording¶
Abstract
VideoRecorder films any camera in your model while a run goes and saves the video in the trial's folder. It is the quickest way to check that a run did what you meant, and to show it to someone else.
Recording a Camera¶
A recorder films a camera that is named in the model. The camera can be fixed in the world, as here, or attached to a moving body, and then the video moves with that body. This model has a ball above a tilted wedge, so the ball bounces off sideways, a fixed camera beside the ball's path, and a spotlight overhead:
To film it, make a VideoRecorder inside the runtime function, call setup with the state, and register_to_rm so the RuntimeManager captures a frame as the run goes and saves the file when it finishes. It needs a path, a camera_name and a frame_rate, and the extension of the path picks the format:
H.264 through ffmpeg, which has to be installed and on your PATH. It plays almost everywhere, so it is the safe choice when you do not know where the video will be watched.
VP9 through ffmpeg, so the file is smaller than an MP4, and it is fully seekable in the Dojo viewer. It needs an ffmpeg with VP9 support.
Written with PIL, so it needs no ffmpeg. It loops, and the file is large. Every frame is held in memory until the run ends, and max_frames caps how many are taken.
Only the MP4 records in the example. The WebM and the GIF are there to show their paths, so the example takes them out again with unregister_from_rm.
pathis where the video goes. The trial's folder,mojo_model.trial_dir, keeps each trial's video next to its telemetry, and the Trial Details page of the Dojo plays it.show_tracesdraws the trail behind the ball, which is covered under Overlays.frame_rateis frames per unit of simulation time (seconds, unless your model uses another unit), so the same stretch of simulation is always the same number of frames however fast the run goes.playback_speedchanges only how fast the saved video plays: 0.5 is slow motion, and 2 is double speed.
The recorders in the example also take a recording_trigger and a frame_label. Both are functions of the state, defined in the runtime function just above the recorders:
recording_triggermakes the recorder only take frames while it returnsTrue. This one latches on when the ball first comes within 0.6 m of the floor and stays on. A trigger that followed the ball's height instead would skip the top of every bounce, and the video would jump forward over each gap.- This is helpful to programmatically control when your recorder is active.
Tip: The Video Does Not Start at t = 0
The trigger stays off until the ball first comes within 0.6 m of the floor, so the first frame of the example video is at t = 0.43 s. The label shows the simulation time, so the start is easy to see.
frame_labelis a function of the state that returns the text to burn into the frame, along with optionalposition,color,background_colorandfont_size(aLabelConfig). Here it is the time and the height.
The recorder is made only in the nominal trial, mojo_model.is_nominal. A Monte Carlo job runs many trials, and filming every one would cost time and disk for videos nobody watches. The Reloaded record command also turns recording off (see Reloaded).
Overlays¶
A recorder can draw more than the camera sees. show_proximities draws the lines of your Proximity pairs, and show_loads draws the custom force arrows from your runtime script (see the runtime script guide).
show_traces draws trails. A Tracer follows a body, site or geom and keeps its recent path, and any recorder with show_traces=True draws that path behind it. The example traces the ball:
| Python | |
|---|---|
Warning: Tracers Are Expensive
Avoid tracers if you can.
A tracer records a point on every step, and a recorder redraws the whole trail on every frame. The cost grows with the length of the trail, which is its duration divided by the timestep.
Filming 2 s of simulation in this example took:
| Trail | Time | Compared with no trail |
|---|---|---|
| None | 0.22 s | baseline |
| 1 s | 0.60 s | 2.7 times slower |
| 5 s | 0.72 s | 3.3 times slower |
1 s, with record_decimation=10 |
0.27 s | 1.2 times slower |
If you need a trail, keep duration short and raise record_decimation. These times are from a small scene, so yours will differ.
duration is how far back the trail reaches, and the trail fades toward its oldest end. color and width style it, and record_decimation records a point only every few steps, which keeps the trail short on a fine timestep. The default color is the visualization.trace_line setting.
Picture Size¶
width and height set the size of the picture, and they default to 640 by 480. MuJoCo renders into an offscreen buffer that is also 640 by 480 unless the model says otherwise, so a recorder that asks for a larger picture raises a ValueError when setup runs, naming both sizes. If a higher resolution than standard definition is desired, you need to raise the buffer size in the generator like this:
| Python | |
|---|---|
This model makes room for 1280 by 720, so a recorder here can ask for width=1280 and height=720.
Size, Speed and Cost¶
A larger picture (see Picture Size) costs more to render and makes a larger file. quality and encode_speed tune the ffmpeg encoder for .mp4 and .webm: a lower quality number is a better picture and a larger file, and a higher encode_speed is a faster encode and a worse compression. .mp4 and .webm frames are sent to the encoder as they are captured, so a long run does not fill the memory.
Every frame is rendered on the thread that steps the simulation, so it costs wall time. On this small scene a frame took about 1.4 ms, and starting the encoder took about 50 ms once, while a physics step took about 15 microseconds. A bigger scene costs more per frame. The levers are frame_rate, the size, and recording only when you need to, with a trigger or only the nominal trial.
All Code
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
Success
You can now film any camera in your model, choose when it records and what the frames say, and keep the cost down.