Skip to content

Commit fd08638

Browse files
committed
Add method to record a video
1 parent 631eb3c commit fd08638

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

examples/example_4_physics_simulation.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
import argparse
3+
import subprocess
34
from pathlib import Path
45

56
import numpy as np
@@ -33,6 +34,46 @@
3334
)
3435

3536

37+
def process_video(simulation_duration: float, start_time: float = 0.0):
38+
cmd = [
39+
"ffprobe",
40+
"-i",
41+
"talos_run.mp4",
42+
"-show_entries",
43+
"format=duration",
44+
"-v",
45+
"quiet",
46+
"-of",
47+
"csv=p=0",
48+
]
49+
50+
out = subprocess.check_output(cmd).decode().strip()
51+
video_duration = float(out)
52+
53+
ratio = simulation_duration / video_duration
54+
55+
cmd = [
56+
"ffmpeg",
57+
"-ss",
58+
str(start_time / ratio),
59+
"-i",
60+
"talos_run.mp4",
61+
"-filter:v",
62+
f"setpts={ratio}*PTS",
63+
"-r",
64+
"60",
65+
"-c:v",
66+
"libx264",
67+
"-crf",
68+
"18",
69+
"-preset",
70+
"slow",
71+
"talos_run_fixed.mp4",
72+
]
73+
74+
subprocess.run(cmd, check=True)
75+
76+
3677
def main():
3778
p = argparse.ArgumentParser()
3879
p.add_argument("--path-talos-data", type=Path, help="Path to talos_data root")
@@ -221,6 +262,8 @@ def main():
221262
rf_forces = np.zeros((len(phases), 1))
222263
lf_forces = np.zeros((len(phases), 1))
223264

265+
log_id = pb.startStateLogging(pb.STATE_LOGGING_VIDEO_MP4, "talos_run.mp4")
266+
224267
# We start the walking phase
225268
for k, _ in enumerate(phases[:-2]):
226269
# Get the current configuration of the robot from the simulator
@@ -301,6 +344,10 @@ def main():
301344

302345
rf_forces[k], lf_forces[k] = simulator.get_contact_forces()
303346

347+
pb.stopStateLogging(log_id)
348+
349+
process_video(t[-1], t_init + 2.0)
350+
304351
if args.plot_results:
305352

306353
zmp_ref_plot = np.zeros((zmp_ref.shape[0], 3))

0 commit comments

Comments
 (0)