-
I have pretty basic PyAV-based code producing slightly wrong colors similarly to using ffmpeg with the default options. The code is: class MP4:
def __init__(self, fname, width, height, fps):
self.output = av.open(fname, 'w', format='mp4')
self.stream = self.output.add_stream('h264', str(fps))
self.stream.width = width
self.stream.height = height
def write_frame(self, pixels):
frame = av.VideoFrame.from_ndarray(pixels, format='rgb24')
packet = self.stream.encode(frame)
self.output.mux(packet)
def close(self):
packet = self.stream.encode(None)
self.output.mux(packet)
self.output.close() The ffmpeg invocation producing similarly wrong colors is:
Using the -colorspace flag fixes the colors in this ffmpeg invocation:
What is the equivalent in PyAV's APIs? (My apologies for what I'm guessing is the very basic question, I've spent more time trying to get this right than I'd like to admit... I did try setting setting |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Adding |
Beta Was this translation helpful? Give feedback.
Adding
frame = frame.reformat(format='yuv420p', dst_colorspace=av.video.reformatter.Colorspace.ITU709)
before the call toencode
fixes the problem. I don't know why both the format and the dst_colorspace arguments are needed for this to work, I tried them separately to no effect and gave up on them but now they turn out to work when combined. (I would guess that only the dst_colorspace option would be needed based on the reasoning in the end of the question.)