Registry / serialization / av

av

JSON →
library17.0.0pypypi✓ verified 52d ago

PyAV is a Pythonic binding for FFmpeg's libraries, providing direct and precise access to media via containers, streams, packets, codecs, and frames. It aims to expose the full power and control of the underlying FFmpeg library while managing lower-level details where possible. The current version is 17.0.0, and releases generally follow significant FFmpeg updates or major feature additions.

serializationdata
pip install av
Install & Compatibility
Where this runs
tested against v17.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.930 runs
installs and imports cleanly · install 0.0s · import 0.164s · 121.8MB
glibc
py 3.103.930 runs
installs and imports cleanly · install 2.5s · import 0.151s · 127MB
120MB installed
● package 120MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

av
import av

This quickstart demonstrates how to create a simple video file (requires NumPy) and then open, decode, and extract a frame using PyAV. It highlights the basic `av.open()` for container management and `container.decode()` for frame iteration. Ensure that FFmpeg is properly installed and discoverable by PyAV for full functionality. The example creates a 1-second black video and then decodes its first frame.

import av import os # Create a dummy video file for demonstration output_filename = "dummy_video.mp4" # Encode a simple video (e.g., 1 second of black frames) # This part requires numpy, but it's a common dependency for video processing try: import numpy as np duration = 1 # seconds fps = 24 # frames per second total_frames = duration * fps width, height = 640, 480 with av.open(output_filename, mode="w") as container: stream = container.add_stream("mpeg4", rate=fps) stream.width = width stream.height = height stream.pix_fmt = "yuv420p" for frame_i in range(total_frames): img = np.zeros((height, width, 3), dtype=np.uint8) # Black frame frame = av.VideoFrame.from_ndarray(img, format="rgb24") for packet in stream.encode(frame): container.mux(packet) # Flush stream for packet in stream.encode(): container.mux(packet) print(f"Successfully created dummy video: {output_filename}") # --- Decoding and processing part of the quickstart --- container = av.open(output_filename) for frame in container.decode(video=0): print(f"Decoded frame {frame.index} with PTS {frame.pts}") # Example: Save the first frame if frame.index == 0: frame.to_image().save(f"frame-{frame.index:04d}.jpg") print(f"Saved frame-0000.jpg") break # Only process the first frame for this quickstart container.close() print("Container closed.") except ImportError: print("NumPy not found. Skipping video creation. To run the full quickstart, install numpy (pip install numpy).") print(f"Please ensure '{output_filename}' exists for the decoding example, or create it manually.") # Attempt to decode if a file exists, otherwise skip if os.path.exists(output_filename): container = av.open(output_filename) for frame in container.decode(video=0): print(f"Decoded frame {frame.index} with PTS {frame.pts}") break container.close() else: print("No video file to decode without NumPy.") finally: # Clean up the dummy video file if os.path.exists(output_filename): os.remove(output_filename) print(f"Cleaned up {output_filename}") if os.path.exists("frame-0000.jpg"): os.remove("frame-0000.jpg") print(f"Cleaned up frame-0000.jpg")
Debug
Known issues
breakingAs of v17.0.0, when an FFmpeg C function indicates an error, PyAV now raises `av.ArgumentError` instead of `ValueError`/`av.ValueError`. This helps to more precisely distinguish the source of an exception.
fix
Update exception handling blocks to catch `av.ArgumentError` specifically for FFmpeg-originated argument errors, or `av.FFmpegError` for all FFmpeg-related errors. You can still catch `ValueError` if broader compatibility is needed, as `av.ArgumentError` inherits from it.
affects: >=17.0.0
gotchaContainers (`av.Container`) and streams (`av.Stream`) must be explicitly closed to ensure all data is flushed and resources are properly released, preventing potential data loss or memory leaks. Using them as context managers (`with av.open(...) as container:`) is the recommended practice.
fix
Always use `with av.open(...) as container:` for opening containers. For streams obtained from output containers, ensure you call `.close()` on the stream after encoding all frames, and then call `.close()` on the container.
affects: all
gotchaPyAV disables FFmpeg's verbose logging by default, which can obscure detailed error messages. While this reduces console noise, it can make debugging challenging when issues arise.
fix
For development and debugging, enable verbose logging: `import av; av.logging.set_level(av.logging.VERBOSE)`. This will provide more descriptive error messages and operational details from the underlying FFmpeg libraries.
affects: all
gotchaWhen decoding, a single input packet does not always guarantee an output frame, and multiple packets might be required to produce a single frame. Additionally, it's crucial to 'flush' the decoder by sending `None` or empty packets at the end of the input stream to retrieve any remaining buffered frames.
fix
When iterating through `container.decode()` or `codec.decode()`, continue processing until no more frames are yielded. After processing all input packets, ensure a final flush by iterating `codec.decode()` or `stream.encode()` with `None` or no arguments until no more packets/frames are returned.
affects: all
gotchaBuilding PyAV from source, especially on Windows or with specific Python versions (e.g., Python 3.10 historically), can lead to `ImportError` or `ModuleNotFoundError` due to issues with dynamic library linking (DLLs on Windows) or internal Python API changes.
fix
Prefer installing PyAV via pre-built wheels (`pip install av`) or `conda-forge` (`conda install -c conda-forge pyav`) as these typically include all necessary FFmpeg dependencies and handle linking. If building from source is unavoidable, carefully follow the official documentation's instructions for setting up the FFmpeg development environment and addressing system-specific linking requirements.
affects: Installation from source on certain platforms/Python versions (e.g., Python 3.10 and earlier on Windows)
gotchaThe PyAV quickstart and many examples rely on external dependencies like NumPy for full functionality (e.g., video creation and decoding). If these dependencies are not installed, examples may skip functionality or fail.
fix
Ensure all required dependencies for the specific examples you intend to run (e.g., `pip install numpy`) are installed. Refer to the example scripts or documentation for a complete list of prerequisites.
affects: all
gotchaSome PyAV examples or advanced functionalities (e.g., those involving array manipulation or complex image processing) rely on optional dependencies like NumPy. If these dependencies are not installed, relevant features or quickstart scripts might be skipped or fail with `ModuleNotFoundError` or other runtime errors, even if PyAV itself is correctly installed.
fix
Ensure all necessary optional dependencies (e.g., `numpy` for video processing examples) are installed alongside PyAV. For convenience, you can often install PyAV with common optional dependencies using `pip install pyav[full]` or `pip install pyav numpy imageio` if specific dependencies are known.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'av'
This error occurs when the PyAV library is not installed in the Python environment.
fix
Install PyAV using pip: `pip install av`.
ImportError: libavdevice-67a93a2b.so.58.10.100: cannot open shared object file: No such file or directory
This error indicates that a required shared library for PyAV is missing or not found.
fix
Reinstall PyAV to ensure all dependencies are correctly installed: `pip install av`.
AttributeError: 'ImportError' object has no attribute 'open'
This error occurs when PyAV is not installed, and the code attempts to use it, leading to an ImportError being assigned to a variable expected to be the PyAV module.
fix
Ensure that PyAV is installed: `pip install av`.
cv2.imshow() hangs when importing av
Importing both OpenCV and PyAV in the same script can cause conflicts leading to cv2.imshow() hanging.
fix
Avoid importing PyAV and OpenCV together, or use them in separate scripts to prevent conflicts.
Failed building wheel for av
PyAV is a wrapper around FFmpeg and requires FFmpeg's development libraries and a C/C++ compiler to build from source, which might be missing in your environment. This often manifests as `error: Microsoft Visual C++ 14.0 is required` on Windows.
fix
Ensure FFmpeg development headers and a C/C++ compiler (like build-essential on Linux, Xcode command line tools on macOS, or Microsoft Visual C++ Build Tools on Windows) are installed before running `pip install av`. For Windows, install the 'Desktop development with C++' workload from Visual Studio Installer. Using Conda (`conda install pyav -c conda-forge`) is often recommended as it bundles dependencies.
Upgrade
Version history
17.1.0latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
90 hits · last 30 days
node
6
seranking-bot
4
ahrefsbot
3
Amazon
1
amazonbot
1
bytedance
1
Resources