Skip to content

How do I encode custom opus stream? #1142

@vinnitu

Description

@vinnitu

Hello!

I want to get opus packet (60ms, 16000, mono) stream from any url source, trying to do something like this

import av

SAMPLE_RATE = 16000
CHUNK_DURATION = 0.06
CHUNK_SIZE = int(CHUNK_DURATION * SAMPLE_RATE)

resampler = av.AudioResampler('s16', 'mono', SAMPLE_RATE)

container = av.open('file.avi')
stream = container.streams.audio[0]
audio_buffer = []

for packet in container.demux(stream):
    for frame in packet.decode():
         for f in resampler.resample(frame):

here I want accumulate samples in audio_buffer, but don't understand how to do it properly

first version is to use f.to_ndarray()
second version is use bytes()

 for p in f.planes:
     audio_buffer.extend(bytes(p))

maybe you can help me to choose?

In result I want get AudioFrame('s16', 'mono', SAMPLE_RATE) with duration 60ms

and make encode to opus format

I am planning do it with something like

audio_encoder = av.AudioEncoder('opus')

and I need this encoded data without container and in bytes format

Can you get me direction?

thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions