ffmpeg png 到 mov 保持精确的颜色

ffmpeg png 到 mov 保持精确的颜色

当我将 PNG 序列转换为 h264(yuv420p)电影并通过 FFmpeg 的 rawvideo 管道输出读取颜色值时,每个像素上的值都会略有修改。

这就是我写图像的方式:

import numpy as NP
from PIL import Image

# Set up colors.
pixel_array = NP.array(
    [
        [
            [0, 0, 0],
            [255, 0, 0],
            [0, 255, 0],
            [0, 255, 0],
        ],
        [
            [125, 125, 125],
            [0, 255, 0],
            [0, 255, 0],
            [0, 255, 0],
        ],
    ],
    dtype=NP.uint8,
)

Image.fromarray(pixel_array).save("test.png")

当我使用 ffmpeg 的管道读回这些值时,这些值都是正确的:

# Read image.
process = subprocess.Popen(
    ["ffmpeg", "-i", "test.png", "-f", "rawvideo", "-pix_fmt", "rgb24", "pipe:"],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)
process.wait()
data_from_png = NP.frombuffer(process.stdout.read(), NP.uint8).reshape([-1, 2, 4, 3])

但是,当我首先转换为 mov 并读取 mov 的像素值时,数字都不同:


# Convert png to a movie
process = subprocess.Popen(
    [
        "ffmpeg",
        "-i",
        "test.png",
        "-c:v",
        "libx264",
        "-pix_fmt",
        "yuv444p",
        "-preset",
        "slow",
        "-crf",
        "18",
        "-y",
        "test.mov",
    ],
)
process.wait()


# Read mov.
process = subprocess.Popen(
    ["ffmpeg", "-i", "test.mov", "-f", "rawvideo", "-pix_fmt", "rgb24", "pipe:"],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
)
process.wait()
data_from_mov = NP.frombuffer(process.stdout.read(), NP.uint8).reshape([-1, 2, 4, 3])


读取 png 与 mov 的输出:


data from png:
array([[[[  0,   0,   0],
         [255,   0,   0],
         [  0, 255,   0],
         [  0, 255,   0]],

        [[125, 125, 125],
         [  0, 255,   0],
         [  0, 255,   0],
         [  0, 255,   0]]]], dtype=uint8)

data from mov:
array([[[[  0,   0,   0],
         [254,   5,   0],
         [  3, 239,   5],
         [  0, 255,   3]],

        [[111, 131, 117],
         [  0, 255,  36],
         [  0, 255,   6],
         [  3, 254,   0]]]], dtype=uint8)


我在将 png 转换为 mov 时做错了吗?还是我读取 mov 的方式有问题?

相关内容