diff --git a/examples/basics/record_facecam.py b/examples/basics/record_facecam.py index 2200bc546..0bba6b36a 100644 --- a/examples/basics/record_facecam.py +++ b/examples/basics/record_facecam.py @@ -18,7 +18,13 @@ ) output = av.open("out.mkv", "w") -output_stream = output.add_stream("h264", rate=30) +# Prefer x264, but use Apple hardware if not available. +try: + encoder = av.Codec("libx264", "w").name +except av.FFmpegError: + encoder = "h264_videotoolbox" + +output_stream = output.add_stream(encoder, rate=30) output_stream.width = input_.streams.video[0].width output_stream.height = input_.streams.video[0].height output_stream.pix_fmt = "yuv420p" diff --git a/examples/basics/record_screen.py b/examples/basics/record_screen.py index 92818e931..14fdfc428 100644 --- a/examples/basics/record_screen.py +++ b/examples/basics/record_screen.py @@ -13,15 +13,25 @@ input_ = av.open("1", format="avfoundation") output = av.open("out.mkv", "w") -output_stream = output.add_stream("h264", rate=30) +# Prefer x264, but use Apple hardware if not available. +try: + encoder = av.Codec("libx264", "w").name +except av.FFmpegError: + encoder = "h264_videotoolbox" + +output_stream = output.add_stream(encoder, rate=30) output_stream.width = input_.streams.video[0].width output_stream.height = input_.streams.video[0].height output_stream.pix_fmt = "yuv420p" try: - for frame in input_.decode(video=0): - packet = output_stream.encode(frame) - output.mux(packet) + while True: + try: + for frame in input_.decode(video=0): + packet = output_stream.encode(frame) + output.mux(packet) + except av.BlockingIOError: + pass except KeyboardInterrupt: print("Recording stopped by user")