Debian Codegraph @

scripts/python/video.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 '''
4 Test script for video encoding.
5
6 Example usage:
7 scripts/python/video.py -f dac/run_fgfs.sh
8
9 '''
10
11 import recordreplay
12
13 import os
14 import sys
15 import time
16
17 def main():
18 fgfs = f'./build-walk/fgfs.exe-run.sh'
19
20 args = iter(sys.argv[1:])
21 while 1:
22 try:
23 arg = next(args)
24 except StopIteration:
25 break
26 if arg == '-f':
27 fgfs = next(args)
28 else:
29 raise Exception(f'Unrecognised arg: {arg}')
30
31 fg = recordreplay.Fg(
32 'harrier-gr3',
33 f'{fgfs}'
34 + f' --state=vto --airport=egtk'
35 + f' --prop:/sim/replay/record-main-view=1'
36 + f' --prop:bool:/sim/replay/record-main-window=0'
37 ,
38 )
39
40 fg.waitfor('/sim/fdm-initialized', 1, timeout=45)
41 fg.fg['/sim/current-view/view-number-raw'] = 1 # helicopter
42
43 # Rotation speed.
44 fg.fg['/controls/auto-hover/rotation-speed-target'] = 4
45 fg.fg['/controls/auto-hover/rotation-mode'] = 'speed'
46
47 # These will have been set by --state=vto
48 # Sideways speed.
49 fg.fg['/controls/auto-hover/x-speed-target'] = '0'
50 fg.fg['/controls/auto-hover/x-mode'] = 'speed'
51
52 # Vertical speed.
53 fg.fg['/controls/auto-hover/y-speed-target'] = '0'
54 fg.fg['/controls/auto-hover/y-mode'] = 'speed'
55
56 # Forwards speed.
57 fg.fg['/controls/auto-hover/z-speed-target'] = '0'
58 fg.fg['/controls/auto-hover/z-mode'] = 'speed'
59
60 results = []
61
62 def make_recording(codec, container, quality, speed, fixed_dt=None):
63 '''
64 Create recording using specified codec etc.
65 '''
66 fg.fg['/sim/video/container'] = container
67 fdm_time_begin = fg.fg['/sim/time/simple-time/fdm']
68 if fixed_dt:
69 fg.fg['/sim/time/simple-time/enabled'] = True
70 fg.fg['/sim/time/fixed-dt'] = fixed_dt
71 name = f'video-test-c={codec}-q={quality}-s={speed}.{container}'
72 frames_start = fg.fg['/sim/frame-number']
73 fg.run_command( f'run video-start name={name} quality={quality} speed={speed} codec={codec}')
74 dt = 10
75 time.sleep(dt)
76 frames = fg.fg['/sim/frame-number'] - frames_start
77 fdm_time_end = fg.fg['/sim/time/simple-time/fdm']
78 fg.run_command( f'run video-stop')
79 fdm_time = fdm_time_end - fdm_time_begin
80 e = fg.fg['/sim/video/error']
81 e = f'error e={e:3}' if e else 'success'
82 e = f'{e:12}'
83 if not e:
84 if not os.path.isfile(name):
85 e = 'error no output file'
86 result = f'Video encoding result: {e}: codec={codec:12} container={container:6} quality={quality:6} speed={speed:6} frames={frames:6} frame_rate={frames/dt:6} fdm_time={fdm_time:6} size={os.path.getsize(name):9}: {name}'
87 results.append( result)
88 print( result)
89
90 if 1:
91 # Create Continuous recording, replay and create video, check video is
92 # new.
93
94 # Create Continuous recording.
95 video_suffix = 'mkv'
96 tstart = time.time()
97 fg.fg['/sim/video/container'] = video_suffix
98 fg.fg['/sim/video/codec'] = 'libx265'
99 fg.fg['/sim/video/quality'] = 0.75
100 fg.fg['/sim/video/speed'] = 1.0
101 fg.fg['/sim/replay/record-continuous-compression'] = 1
102 fg.fg['/sim/replay/record-continuous'] = 1
103 fg.fg['/sim/replay/record-main-view'] = 1
104 endtime = tstart + 10
105 while 1:
106 if time.time() > endtime:
107 break
108 time.sleep(1)
109 fg.run_command('run view-step step=1')
110 fg.fg['/sim/replay/record-continuous'] = 0
111
112 # Replay to create video.
113 tstart = time.time()
114 fg.fg['sim/replay/replay-main-view'] = 1
115 fg.fg['sim/replay/replay-windows-position'] = 0
116 fg.fg['sim/replay/replay-windows-size'] = 0
117 fg.fg['/sim/video/container'] = 'mkv'
118 fg.fg['/sim/video/codec'] = 'libx265'
119 fg.fg['/sim/video/quality'] = 0.75
120 fg.fg['/sim/video/speed'] = 1.0
121 fg.run_command( f'run load-tape tape={fg.aircraft}-continuous create-video=1 fixed-dt=0.04')
122 fg.waitfor('/sim/replay/replay-state', 1) # Wait for replay to start.
123 fg.waitfor('/sim/replay/replay-state-eof', 1) # Wait for replay eof.
124
125 # Check video looks ok.
126 video_path = f'fgvideo-harrier-gr3.{video_suffix}'
127 video_path2 = recordreplay.readlink( video_path)
128 print(f'*** video_path={video_path} video_path2={video_path2}')
129 t = os.path.getmtime(video_path2)
130 assert t > tstart, f'Video file too old: {video_path2}'
131
132 elif 1:
133 make_recording('libtheora', 'ogv', quality=1, speed=1, fixed_dt=0.02)
134 make_recording('libx265', 'mkv', quality=1, speed=1, fixed_dt=0.02)
135
136 else:
137 for codec in (
138 'libtheora',
139 'libx265',
140 'mpeg2video',
141 'libx264',
142 'libvpx',
143 ):
144 fg.fg['/sim/video/codec'] = codec
145 for container in (
146 'mpeg',
147 'ogv',
148 'mkv',
149 ):
150 # High quality can semi-freeze Flightgear.
151 for quality, speed in [
152 ('0.4', '0.5'),
153 ('0.4', '0.9'),
154 ('0.1', '0.5'),
155 ('0.1', '0.9'),
156 ]:
157 # This ordering should give increasing frame rate.
158 make_recording(codec, container, quality, speed)
159
160 print('Results:')
161 for result in results:
162 print( f' {result}')
163
164 if __name__ == '__main__':
165 main()