Transcoding results not consistent in repeated calls
Hello, I am using gstreamer for a transcoding service that converts audio/video files to WAV files, and use python to do the same. The following is the transcode function that is called repeatedly. We use a custom appsrc/appsink for our cloud object storage I/O, and _loop.quit() is called whenever we need to abandon the execution in the callbacks we defined. This is the code (with some minor modifications).
def transcode(response, input_oss, output_oss, support8_khz=True):
setup_gstreamer_path()
Gst.init(None)
_loop = GLib.MainLoop()
_appsrc_gen = AppSrcGenerator(response, input_oss)
_appsrc = _appsrc_gen.appsrc
_audioconvert = Gst.ElementFactory.make('audioconvert', 'audio_convert')
_audio_resample = Gst.ElementFactory.make('audioresample', 'audio_resample')
_caps_filter = Gst.ElementFactory.make('capsfilter', 'caps_filter')
_audio_cap_string = f"{AUDIO_STRING} , format={AUDIO_FORMAT} , rate={DEFAULT_SAMPLE_RATE}, channels={CHANNELS}"
_audio_caps = Gst.Caps(_audio_cap_string)
_caps_filter.set_property('caps', _audio_caps)
_encoder = Gst.ElementFactory.make('wavenc', 'wav_enc')
_appsink_gen = AppSinkGenerator(response, output_oss)
_appsink = _appsink_gen.appsink
_decodebin = generate_decodebin(response, _audio_resample, _caps_filter, _loop, support8_khz)
_pipe = generate_pipeline(response, _loop, output_oss)
_pipe.add(_appsrc, _decodebin, _audioconvert,
_audio_resample, _caps_filter, _encoder, _appsink)
_appsrc.link(_decodebin)
_audio_resample.link(_audioconvert)
_audioconvert.link(_caps_filter)
_caps_filter.link(_encoder)
_encoder.link(_appsink)
# start play back and listen to events
_pipe.set_state(Gst.State.PLAYING)
_loop.run()
# cleanup
_pipe.set_state(Gst.State.NULL)
The problem we have is that the files that are transcoded are not exactly the same, which is probably happening because we are not resetting gstreamer properly, because the output files are exactly the same when we restart the python process. It's only when we run the same function more than once (like in a for loop) in the same python process that we see the problem. I tried adding _pipe.run_dispose(), tried doing Gst.init(None) only once but that didn't work either. What is the proper way to ensure that the results of the transcoding pipeline are repeatable? Most sources I saw online only seem to set the pipeline state to null, but that clearly isn't enough. Thanks