Skip to content

How to write the bitstream obtained from PyNvVideoCodec encoding into a video file (e.g., MP4). #1434

Open

Description

@xingguang12

`import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
if not ret:
break

height, width, channels = frame.shape
print(f"Width: {width}, Height: {height}")

# Convert to YUV format
yun_image = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV)

# Separate Y, U, V channels
y = yun_image[:, :, 0]
u = yun_image[:, :, 1]
v = yun_image[:, :, 2]

# Downsample U and V (subsample by 2 in both width and height)
u = u[::2, ::2]
v = v[::2, ::2]

# Flatten Y, U, V channels
y_flat = y.flatten()
uv_flat = np.dstack((u.flatten(), v.flatten())).flatten()

# Combine Y and UV for NV12 format
nv12_data = np.concatenate((y_flat, uv_flat))
import PyNvVideoCodec as nvc
import numpy as np

encoder = nvc.CreateEncoder(
    640,
    480,
    "NV12",
    True)
frame_size = 640 * 480 * 1.5
bitstream = encoder.Encode(nv12_data)  # encode frame one by one
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()`

Activity

  1. Z-NAVY commented on Nov 18, 2025

    @Z-NAVY

    How did you finally solve this problem?

  2. 2000pd3rvr commented on Jul 21, 2026

    @2000pd3rvr

    you could run one known-good image with fixed seed/threshold and compare mask/box IoU against a baseline commit — isolates code vs input. Might be worth checking ---> UIScrollView Scrollable Content Size Ambiguity; How to import a figure as its original height/width ratio?.

    — A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions