Skip to content

Commit baa1bce

Browse files
committed
Update recording examples
1 parent bc0ca08 commit baa1bce

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

examples/basics/record_facecam.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
)
1919
output = av.open("out.mkv", "w")
2020

21-
output_stream = output.add_stream("h264", rate=30)
21+
# Prefer x264, but use Apple hardware if not available.
22+
try:
23+
encoder = av.Codec("libx264", "w").name
24+
except av.FFmpegError:
25+
encoder = "h264_videotoolbox"
26+
27+
output_stream = output.add_stream(encoder, rate=30)
2228
output_stream.width = input_.streams.video[0].width
2329
output_stream.height = input_.streams.video[0].height
2430
output_stream.pix_fmt = "yuv420p"

examples/basics/record_screen.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,25 @@
1313
input_ = av.open("1", format="avfoundation")
1414
output = av.open("out.mkv", "w")
1515

16-
output_stream = output.add_stream("h264", rate=30)
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)
1723
output_stream.width = input_.streams.video[0].width
1824
output_stream.height = input_.streams.video[0].height
1925
output_stream.pix_fmt = "yuv420p"
2026

2127
try:
22-
for frame in input_.decode(video=0):
23-
packet = output_stream.encode(frame)
24-
output.mux(packet)
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
2535
except KeyboardInterrupt:
2636
print("Recording stopped by user")
2737

0 commit comments

Comments
 (0)