PyAV
PyAV copied to clipboard
Segfault when using multiple `add_stream()`s
import av
from fractions import Fraction
def copy_video_stream(container):
input_ = av.open("example.mp4")
video_stream = container.add_stream("h264", rate=30)
video_stream.width = input_.streams.video[0].width
video_stream.height = input_.streams.video[0].height
video_stream.pix_fmt = "yuv420p"
for frame in input_.decode(video=0):
packet = video_stream.encode(frame)
container.mux(packet)
input_.close()
def main():
output_container = av.open("output.mp4", "w")
copy_video_stream(output_container) # must be called to trigger segfault
input_ = av.open("input.wav")
input_stream = input_.streams.audio[0]
audio_codec = "aac"
global_sr = 48000
assert input_stream.sample_rate == global_sr
audio_stream = output_container.add_stream(audio_codec, rate=global_sr)
audio_stream.layout = "stereo"
audio_stream.format = "fltp"
audio_stream.time_base = Fraction(1, global_sr)
assert isinstance(audio_stream, av.audio.AudioStream)
resampler = av.audio.resampler.AudioResampler(
format="fltp", layout="stereo", rate=global_sr
)
pts = 0
for frame in input_.decode(audio=0):
for reframe in resampler.resample(frame): # SEGFAULT around here
if reframe.pts is None or reframe.pts == 0:
reframe.pts = pts
reframe.time_base = audio_stream.time_base
for packet in audio_stream.encode(reframe):
if packet.pts is None or packet.pts == 0:
packet.pts = pts
output_container.mux_one(packet)
pts += reframe.samples
print("flushing")
output_container.mux(audio_stream.encode(None))
input_.close()
output_container.close()
main()
tested with PyAV 13.1.0 14.0.0rc1
MacOS Arm:
zsh: segmentation fault python3 test.py
MacOS Intel:
zsh: floating point exception python3 test.py
Windows: succeeds, but writes (almost right?) garbage data.