[Python] ffmpeg how to set max_num_reorder_frames H264

Discussão em 'Python' iniciado por Stack, Junho 13, 2025.

  1. Stack

    Stack Membro Participativo

    Anyone know how can I set max_num_reorder_frames to 0 when I am encoding H264 video ? You can find in the docs as uint8_t H264RawVUI::bitstream_restriction_flag

    PS. Based on the discussion in the comments. What I actually want to accomplish is to have all the frames written in the order in which they were encoded. My use-case is - I have 1000 images for example. I encode each one of them using the codec, but then when I investigate a little bit and check the actual packets in the H264 container, I see that I have cases when one frame is written twice (for example ... 1,2,3,3,4,5,6,7,7 ...) what I want is once I decode the the H264 container I want to get the same images which I encoded. Is that possible and how ?

    P.P.S: I don't think the g=1 works - giving some more code for reference. This is what I currently have:

    import numpy as np
    import ffmpeg, subprocess, av

    width, height, encoding_profile, pixel_format = 1280, 800, 'main', 'yuv420p'

    # here I create 256 frames where each one has unique pixels all zeros, ones, twos and etc.
    np_images = []
    for i in range(256):
    np_image = i + np.zeros((height, width, 3), dtype=np.uint8)
    np_images.append(np_image)

    print(f'number of numpy images: {len(np_images)}')

    encoder = (ffmpeg
    .input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width, height))
    .output('pipe:', format='H264', pix_fmt=pixel_format, vcodec='libx264', profile='main', g=1)
    .run_async(pipe_stdin=True, pipe_stdout=True)
    )

    for timestamp, frame in enumerate(np_images):
    encoder.stdin.write(
    frame
    .astype(np.uint8)
    .tobytes()
    )

    encoder.stdin.close()
    output = encoder.stdout.read()
    encoder.stdout.close()

    # here I decode the encoded frames using PyAV
    frame_decoder = av.CodecContext.create("h264", "r")
    frame_decoder.thread_count = 0
    frame_decoder.thread_type = 'NONE'
    packets = frame_decoder.parse(output)
    decoded_frames = []

    for packet in packets:
    frame = frame_decoder.decode(packet)
    decoded_frames.extend(frame)

    decoded_frames.extend(frame_decoder.decode())
    print(f'number of decoded frames: {len(decoded_frames)}')
    print('keyframe boolean mask')
    print([e.key_frame for e in decoded_frames])

    decoded_np_images = []

    for frame in decoded_frames:
    decoded_np_images.append(np.array(frame.to_image()))

    print(f'number of decoded numpy images: {len(decoded_np_images)}')

    # here I check what the decoded frames contain (all zeros, ones, twos and etc.)
    print([e[0,0,0].item() for e in decoded_np_images])


    the particular problem which I am facing is that in the output you can observe this:


    number of decoded numpy images: 255

    [0, 1, 2, 3, 3, 4, 5, 6, 8, 9, 10, 10, 11, 12, 13, 15, 16, 17, 17, 18, 19, 20, 22, 23, 24, 24, 25, 26, 27, 29, 30, 31, 31, 32, 33, 34, 36, 37, 38, 39, 39, 40, 41, 43, 44, 45, 46, 46, 47, 48, 50, 51, 52, 53, 53, 54, 55, 57, 58, 59, 60, 60, 61, 62, 64, 65, 66, 67, 67, 68, 69, 71, 72, 73, 74, 74, 75, 76, 78, 79, 80, 81, 81, 82, 83, 85, 86, 87, 88, 88, 89, 90, 91, 93, 94, 95, 95, 96, 97, 98, 100, 101, 102, 102, 103, 104, 105, 107, 108, 109, 109, 110, 111, 112, 114, 115, 116, 116, 117, 118, 119, 121, 122, 123, 123, 124, 125, 126, 128, 129, 130, 131, 131, 132, 133, 135, 136, 137, 138, 138, 139, 140, 142, 143, 144, 145, 145, 146, 147, 149, 150, 151, 152, 152, 153, 154, 156, 157, 158, 159, 159, 160, 161, 163, 164, 165, 166, 166, 167, 168, 170, 171, 172, 173, 173, 174, 175, 176, 178, 179, 180, 180, 181, 182, 183, 185, 186, 187, 187, 188, 189, 190, 192, 193, 194, 194, 195, 196, 197, 199, 200, 201, 201, 202, 203, 204, 206, 207, 208, 208, 209, 210, 211, 213, 214, 215, 216, 216, 217, 218, 220, 221, 222, 223, 223, 224, 225, 227, 228, 229, 230, 230, 231, 232, 234, 235, 236, 237, 237, 238, 239, 241, 242, 243, 244, 244, 245, 246, 248, 249, 250, 251, 251, 252, 253]
    ​

    I still have frames which are appearing twice (and respectively some are missing)

    Continue reading...

Compartilhe esta Página