We must only ever call av_write_trailer once, otherwise we get a segmentation fault. Therefore no matter whether it succeeds or not we must absolutely set self._done.
Also we need to be more careful when closing the streams' CodecContext:
-
It's too late to iterate over the streams in
__dealloc__
as Python object destruction may have already started. -
We must not error if there is not CodecContext or if the CodecContext has already been closed by the user.