Skip to content

Cannot flush AudioResampler before it is used #537

Closed
@aspen1135

Description

@aspen1135

I am trying to convert a short .WAV file into another .WAV file but slightly modified. I have about 403 other files that successfully export using this function:

def convert(input, output, filename, codec, channel_layout, frame_rate):

    # set input/output locations
    inp = av.open(f"{input}", 'r')
    out = av.open(f"{output}\\{filename}", 'w')
    out_stream = out.add_stream(f"{codec}")


    # resampler object details how we want to change frame information
    resampler = av.AudioResampler(
        # format=av.AudioFormat(format).packed,
        layout=channel_layout,
        rate=frame_rate
    )


    # decode frames and start re-encoding into new file
    for frame in inp.decode(audio=0):
        frame.pts = None  # pts is presentation time-stamp. Not relevant here.

        frame = resampler.resample(frame)  # get current working frame and re-sample it for encoding

        for p in out_stream.encode(frame):  # encode the re-sampled frame
            out.mux(p)

    out.close()

Until it runs into this exception:

File "C:\Users\Aspen\Google Drive\1. Software Engineering\Python\Samplify (Desktop Workspace)\database\av_handler.py", line 63, in convert
    for p in out_stream.encode(frame):  # encode the re-sampled frame
  File "av/stream.pyx", line 130, in av.stream.Stream.encode
  File "av/codec/context.pyx", line 311, in av.codec.context.CodecContext.encode
  File "av/audio/codeccontext.pyx", line 40, in av.audio.codeccontext.AudioCodecContext._prepare_frames_for_encode
  File "av/audio/resampler.pyx", line 58, in av.audio.resampler.AudioResampler.resample
ValueError: Cannot flush AudioResampler before it is used.

Here is the data being passed to the function before the function, documented from my log:

Filename: BT-Snare CR78 Rim.wav, selected_codec: pcm_s16le, selected_frame_rate: 6000, channel_layout: stereo

It seems other files have no problems using the resampler except this one. All test files were taken from the same audio libraries as well. Any idea what's going on?

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