Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot flush AudioResampler before it is used #537

Closed
aspen1135 opened this issue Jun 27, 2019 · 3 comments
Closed

Cannot flush AudioResampler before it is used #537

aspen1135 opened this issue Jun 27, 2019 · 3 comments

Comments

@aspen1135
Copy link

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?

@SunDoge
Copy link

SunDoge commented Oct 5, 2019

I had the same problem when copying audio from one video to another video.

image

I followed the instructions in https://github.com/mikeboers/PyAV/blob/develop/examples/basics/remux.py
by replacing the video stream with an audio stream.

@mikeboers
Copy link
Member

Looking at the code for a minute, it seems like a pretty pointless exception. It should just return None and move on with life.

I'll try to to dig a little deeper in the near future, and remove it.

@mikeboers
Copy link
Member

I removed the exception and just return None now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants