--- name: pcap-voice-extract version: 1.0.0 author: Walter Fan description: >- Extract RTP/Opus audio payloads from a pcap, reassemble them into a playable Ogg-Opus file, decode, and verify the result against packet-loss and jitter stats. Use when someone needs to hear what was actually said in a captured VoIP/WebRTC call, not just read the packet trace, or when a reproducible demo pcap is needed to practice tshark/ffmpeg extraction without a real capture. Does not decrypt SRTP. license: MIT tags: - voip - rtp - opus - tshark - pcap category: networking platforms: - codex - claude-code - cursor visibility: project metadata: author: Walter Fan scripts: - scripts/make_opus_rtp_demo.py - scripts/assemble_opus.py --- # PCAP Voice Extract Skill (RTP/Opus) A skill for turning a pcap that carries RTP/Opus audio into a playable file: extract the RTP payloads, reassemble them into a valid Ogg-Opus container, decode, and play back. It packages the exact SOP a VoIP engineer runs by hand when someone says "通话有杂音,把 pcap 发你了" — written so an agent (or a new teammate) can execute it the same way every time. ## When to Use Use this skill when: - A `.pcap`/`.pcapng` file contains RTP media on a known UDP port and the payload codec is Opus (dynamic payload type, e.g. `a=rtpmap:111 opus/48000/2` from the call's SDP). - Someone needs to hear what was actually said in a captured call, not just read the packet trace. - There's no real capture yet and you need a reproducible demo pcap to practice on or to regression-test tooling. Do not use this skill when the RTP is encrypted (SRTP/DTLS-SRTP) and no decryption key is available — see **Boundaries** below. ## Contract **Preconditions** - `tshark` (ships with Wireshark) on PATH or at a known path. - `ffmpeg`/`ffprobe` on PATH, built with libopus. - A pcap file, and the UDP port the RTP stream uses (from SDP or from `tshark -z rtp,streams` with the default UDP dissector). - Optional, for the no-capture-yet case: macOS `say` (or any TTS) to synthesize a demo voice sample. **Steps** 1. **Get a pcap.** Real capture: `sudo tcpdump -i any -U -w call.pcap udp port 5004`. No capture available: `python3 scripts/make_opus_rtp_demo.py ` generates a deterministic demo pcap (`demo_rtp_opus.pcap`) by encoding synthesized speech to Opus and wrapping each frame in Ethernet/IPv4/UDP/RTP per RFC 7587 (48 kHz clock, one Opus frame per RTP packet). 2. **Tell tshark the payload is RTP**, then pull the payloads out as hex: ```bash tshark -r -d udp.port==,rtp -T fields -e rtp.payload > payloads.txt ``` 3. **Reassemble into a playable `.opus`:** ```bash python3 scripts/assemble_opus.py payloads.txt out.opus ``` This rebuilds the Ogg container page-by-page. The one non-obvious part: Ogg's CRC-32 is MSB-first with polynomial `0x04c11db7`, not the reflected zlib variant — using `zlib.crc32()` here produces a file ffmpeg rejects with `CRC mismatch!`. `assemble_opus.py` implements the correct table. 4. **Decode and play:** ```bash ffmpeg -i out.opus out.wav afplay out.wav # or ffplay out.wav ``` 5. **Verify, don't just assume it worked:** - `tshark -r -d udp.port==,rtp -q -z rtp,streams` — check the `Lost` column is 0 and jitter is sane. - `sox out.wav -n stat` — RMS amplitude should be well above 0 (silence/noise looks like RMS near 0 or a flat max amplitude); peak amplitude under ~1.0 and not clipped. **Postconditions** - A `.wav` (or `.opus`) file that reproduces the captured audio, byte-identical in payload to what was on the wire. - A packet-loss/jitter report from `rtp,streams` that tells you whether any "杂音" is a decode artifact or a real network problem. ## Script Reference | Script | Purpose | Usage | |---|---|---| | `scripts/make_opus_rtp_demo.py` | Generate a deterministic demo pcap (speech → Opus → RTP) when there's no real capture to practice on | `python3 scripts/make_opus_rtp_demo.py ` | | `scripts/assemble_opus.py` | Reassemble tshark-extracted hex payloads into a playable Ogg-Opus file, with correct Ogg CRC | `python3 scripts/assemble_opus.py payloads.txt out.opus demo.opus` | Both scripts are plain Python 3 standard library plus a call out to `ffmpeg`/`tshark` — no extra packages to install. ## Reading the TOC Byte (why this isn't guesswork) The first byte of every Opus payload is the TOC (Table of Contents): top 5 bits select one of 32 `config` values (RFC 6716 §3.1) covering mode (SILK/Hybrid/CELT), bandwidth, and frame size; the next bit is mono/stereo; the last 2 bits say how many frames are in the packet. `tshark -d rtp.pt==,opus -V` decodes this for you — use it to sanity-check that what you extracted really is Opus and not something else on that port. ## Troubleshooting | Symptom | Likely cause | What to check | |---|---|---| | Playback has gaps or missing words | Real packet loss | `rtp,streams` `Lost` column > 0 — this is a network problem, not a decode bug | | Playback stutters but nothing is "lost" | Jitter / out-of-order delivery | Sequence numbers fine but timestamps non-monotonic; sort by sequence before reassembling | | Two voices overlap in one file | Multiple RTP streams in one pcap | Group by SSRC (`rtp.ssrc`) first; reassemble each stream into its own file | | `tshark -e rtp.payload` prints nothing useful, or payload looks like noise after decode | Payload is SRTP, not RTP | See **Boundaries** — this skill does not decrypt | | `ffmpeg` reports `CRC mismatch!` repeatedly | Hand-rolled Ogg muxing used a reflected CRC32 | Use `assemble_opus.py`'s MSB-first CRC, not `zlib.crc32()` | ## Boundaries - **No SRTP decryption.** If the capture is encrypted (common in WebRTC and most production voice), this skill stops at "payload is ciphertext, can't reassemble." Decryption needs the session key (`SDES` or `DTLS-SRTP`, e.g. via `SSLKEYLOGFILE`) — out of scope here. - **Assumes one Opus frame per RTP packet**, the overwhelmingly common case per RFC 7587. Packets using Opus's multi-frame-per-packet mode need extra unpacking this skill doesn't do. - **Doesn't do codecs other than Opus.** The TOC parsing and container logic are Opus-specific.