{"library":"av","code":"import av\nimport os\n\n# Create a dummy video file for demonstration\noutput_filename = \"dummy_video.mp4\"\n\n# Encode a simple video (e.g., 1 second of black frames)\n# This part requires numpy, but it's a common dependency for video processing\ntry:\n    import numpy as np\n    duration = 1  # seconds\n    fps = 24      # frames per second\n    total_frames = duration * fps\n    width, height = 640, 480\n\n    with av.open(output_filename, mode=\"w\") as container:\n        stream = container.add_stream(\"mpeg4\", rate=fps)\n        stream.width = width\n        stream.height = height\n        stream.pix_fmt = \"yuv420p\"\n\n        for frame_i in range(total_frames):\n            img = np.zeros((height, width, 3), dtype=np.uint8) # Black frame\n            frame = av.VideoFrame.from_ndarray(img, format=\"rgb24\")\n            for packet in stream.encode(frame):\n                container.mux(packet)\n\n        # Flush stream\n        for packet in stream.encode():\n            container.mux(packet)\n\n    print(f\"Successfully created dummy video: {output_filename}\")\n\n    # --- Decoding and processing part of the quickstart ---\n    container = av.open(output_filename)\n\n    for frame in container.decode(video=0):\n        print(f\"Decoded frame {frame.index} with PTS {frame.pts}\")\n        # Example: Save the first frame\n        if frame.index == 0:\n            frame.to_image().save(f\"frame-{frame.index:04d}.jpg\")\n            print(f\"Saved frame-0000.jpg\")\n        break # Only process the first frame for this quickstart\n\n    container.close()\n    print(\"Container closed.\")\n\nexcept ImportError:\n    print(\"NumPy not found. Skipping video creation. To run the full quickstart, install numpy (pip install numpy).\")\n    print(f\"Please ensure '{output_filename}' exists for the decoding example, or create it manually.\")\n    # Attempt to decode if a file exists, otherwise skip\n    if os.path.exists(output_filename):\n        container = av.open(output_filename)\n        for frame in container.decode(video=0):\n            print(f\"Decoded frame {frame.index} with PTS {frame.pts}\")\n            break\n        container.close()\n    else:\n        print(\"No video file to decode without NumPy.\")\nfinally:\n    # Clean up the dummy video file\n    if os.path.exists(output_filename):\n        os.remove(output_filename)\n        print(f\"Cleaned up {output_filename}\")\n    if os.path.exists(\"frame-0000.jpg\"):\n        os.remove(\"frame-0000.jpg\")\n        print(f\"Cleaned up frame-0000.jpg\")","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}