Debian Codegraph @

examples/basics/record_screen.py

raw
1 import av
2
3 av.logging.set_level(av.logging.VERBOSE)
4
5 """
6 This is written for MacOS. Other platforms will need a different file, format pair.
7 You may need to change the file "1". Use this API to list all devices:
8
9 av.enumerate_input_devices("avfoundation")
10
11 """
12
13 input_ = av.open("1", format="avfoundation")
14 output = av.open("out.mkv", "w")
15
16 # Prefer x264, but use Apple hardware if not available.
17 try:
18 encoder = av.Codec("libx264", "w").name
19 except av.FFmpegError:
20 encoder = "h264_videotoolbox"
21
22 output_stream = output.add_stream(encoder, rate=30)
23 output_stream.width = input_.streams.video[0].width
24 output_stream.height = input_.streams.video[0].height
25 output_stream.pix_fmt = "yuv420p"
26
27 try:
28 while True:
29 try:
30 for frame in input_.decode(video=0):
31 packet = output_stream.encode(frame)
32 output.mux(packet)
33 except av.BlockingIOError:
34 pass
35 except KeyboardInterrupt:
36 print("Recording stopped by user")
37
38 packet = output_stream.encode(None)
39 output.mux(packet)
40
41 input_.close()
42 output.close()