ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

How to create a video by frame with timestamp

Open 1170300710 opened this issue 3 years ago • 0 comments

I have some frames, type is ndarray. And I get the timestamp of every frame

class Frame:
    def __init__(self, mat, timestamp):
        self.mat = mat # type is ndarray
        self.timestamp = timestamp

frames = [...] # many "Frame" objects
height = frames[0].mat.shape[0]
width = frames[0].mat.shape[1]

process = ffmpeg.input(
    'pipe:',
    format='rawvideo',
    pix_fmt='bgr24',
    s=f'{width}x{height}'
).output(
    "VideoCapture.mp4",
    pix_fmt='yuv420p',
    vcodec="h264_nvenc",
    r=60,
).overwrite_output().run_async(
    pipe_stdin=True
)

for f in frames:
    process.stdin.write(
        f.mat.astype(np.uint8).tobytes()
    )

I use this code to encode a video, but I get the wrong video lenth. so how can I write a video by each frame's timestamp?

1170300710 avatar May 12 '22 11:05 1170300710