Created
July 5, 2023 14:58
-
-
Save giavac/f50b6b51f5d4ba96ed8849a8fd25ecee to your computer and use it in GitHub Desktop.
Extracts the RTP payload and decodes it into a wav file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyshark | |
import opuslib | |
import wave | |
import argparse | |
FRAME_SIZE = 960 | |
SAMPLE_RATE = 48000 | |
CHANNEL_COUNT = 1 | |
SAMPLE_WIDTH = 2 | |
def extract_opus_from_pcapng(pcapng_file): | |
opus_packets = [] | |
cap = pyshark.FileCapture(pcapng_file) | |
for pkt in cap: | |
# Skip UDP header (8) and RTP header (16) | |
opus_payload = bytes.fromhex(pkt.data.data[24:]) | |
opus_packets.append(opus_payload) | |
cap.close() | |
return opus_packets | |
def decode_opus_to_wav(opus_packets, output_file): | |
opus_decoder = opuslib.Decoder(SAMPLE_RATE, CHANNEL_COUNT) | |
with wave.open(output_file, 'wb') as wav_file: | |
wav_file.setnchannels(CHANNEL_COUNT) | |
wav_file.setsampwidth(SAMPLE_WIDTH) | |
wav_file.setframerate(SAMPLE_RATE) | |
for packet in opus_packets: | |
decoded_pcm = opus_decoder.decode(packet, FRAME_SIZE) | |
wav_file.writeframes(decoded_pcm) | |
def main(): | |
parser = argparse.ArgumentParser(description='Decode opus from pcap file into wav') | |
parser.add_argument('pcap_file', type=str, help='Path to the pcap file') | |
parser.add_argument('wav_file', type=str, help='Path to the wav file') | |
args = parser.parse_args() | |
pcap_file = args.pcap_file | |
wav_file = args.wav_file | |
opus_packets = extract_opus_from_pcapng(pcap_file) | |
decode_opus_to_wav(opus_packets, wav_file) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment