Skip to content

Commit

Permalink
use pyav
Browse files Browse the repository at this point in the history
  • Loading branch information
yzld2002 committed Sep 6, 2024
1 parent 78e62d4 commit 88f0c89
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions vision_agent/utils/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List, Optional, Tuple

import cv2
import av
import numpy as np
from decord import VideoReader # type: ignore

Expand Down Expand Up @@ -48,13 +49,20 @@ def video_writer(
) -> str:
if filename is None:
filename = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name

fourcc = cv2.VideoWriter_fourcc(*"mp4v") # type: ignore
height, width = frames[0].shape[:2]
writer = cv2.VideoWriter(filename, fourcc, fps, (width, height))
container = av.open(filename, mode="w")
stream = container.add_stream("h264", rate=fps)
stream.width = frames[0].shape[1]
stream.height = frames[0].shape[0]
stream.pix_fmt = "yuv420p"
for frame in frames:
writer.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
writer.release()
frame_rgb = frame[:, :, :3]
av_frame = av.VideoFrame.from_ndarray(frame_rgb, format="rgb24")
for packet in stream.encode(av_frame):
container.mux(packet)

for packet in stream.encode():
container.mux(packet)
container.close()
return filename


Expand Down

0 comments on commit 88f0c89

Please sign in to comment.