diff --git a/docs/cookbook/audio.rst b/docs/cookbook/audio.rst index 39ccc62b8..90d6bf0da 100644 --- a/docs/cookbook/audio.rst +++ b/docs/cookbook/audio.rst @@ -5,4 +5,6 @@ Audio Filters ------- +Increase the audio speed by applying the atempo filter. The speed is increased by 2. + .. literalinclude:: ../../examples/audio/atempo.py diff --git a/docs/cookbook/basics.rst b/docs/cookbook/basics.rst index 2896519de..d7c1d6320 100644 --- a/docs/cookbook/basics.rst +++ b/docs/cookbook/basics.rst @@ -15,7 +15,7 @@ If you just want to look at keyframes, you can set :attr:`.CodecContext.skip_fra Remuxing -------- -Remuxing is copying audio/video data from one container to the other without transcoding it. By doing so, the data does not suffer any generational loss, and is the full quality that it was in the source container. +Remuxing is copying audio/video data from one container to the other without transcoding. By doing so, the data does not suffer any generational loss, and is the full quality that it was in the source container. .. literalinclude:: ../../examples/basics/remux.py @@ -40,3 +40,16 @@ Also enabling :data:`~av.codec.context.ThreadType.FRAME` (or :data:`~av.codec.co .. literalinclude:: ../../examples/basics/thread_type.py On the author's machine, the second pass decodes ~5 times faster. + + +Recording the Screen +-------------------- + +.. literalinclude:: ../../examples/basics/record_screen.py + + +Recording a Facecam +------------------- + +.. literalinclude:: ../../examples/basics/record_facecam.py + diff --git a/examples/basics/record_facecam.py b/examples/basics/record_facecam.py new file mode 100644 index 000000000..2200bc546 --- /dev/null +++ b/examples/basics/record_facecam.py @@ -0,0 +1,41 @@ +import av + +av.logging.set_level(av.logging.VERBOSE) + + +""" +This is written for MacOS. Other platforms will need to init `input_` differently. +You may need to change the file "0". Use this command to list all devices: + + ffmpeg -f avfoundation -list_devices true -i "" + +""" + +input_ = av.open( + "0", + format="avfoundation", + container_options={"framerate": "30", "video_size": "1920x1080"}, +) +output = av.open("out.mkv", "w") + +output_stream = output.add_stream("h264", rate=30) +output_stream.width = input_.streams.video[0].width +output_stream.height = input_.streams.video[0].height +output_stream.pix_fmt = "yuv420p" + +try: + 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") + +packet = output_stream.encode(None) +output.mux(packet) + +input_.close() +output.close() diff --git a/examples/basics/record_screen.py b/examples/basics/record_screen.py new file mode 100644 index 000000000..92818e931 --- /dev/null +++ b/examples/basics/record_screen.py @@ -0,0 +1,32 @@ +import av + +av.logging.set_level(av.logging.VERBOSE) + +""" +This is written for MacOS. Other platforms will need a different file, format pair. +You may need to change the file "1". Use this command to list all devices: + + ffmpeg -f avfoundation -list_devices true -i "" + +""" + +input_ = av.open("1", format="avfoundation") +output = av.open("out.mkv", "w") + +output_stream = output.add_stream("h264", 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) +except KeyboardInterrupt: + print("Recording stopped by user") + +packet = output_stream.encode(None) +output.mux(packet) + +input_.close() +output.close()