Skip to content

Commit

Permalink
Add docs for recording
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Oct 12, 2024
1 parent fba30be commit 7dc4000
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/cookbook/audio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion docs/cookbook/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

41 changes: 41 additions & 0 deletions examples/basics/record_facecam.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions examples/basics/record_screen.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 7dc4000

Please sign in to comment.