Skip to content

Commit

Permalink
add ReadLiveLast
Browse files Browse the repository at this point in the history
  • Loading branch information
chenxinfeng committed Nov 14, 2023
1 parent 690df21 commit 385ca81
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
30 changes: 30 additions & 0 deletions ffmpegcv/ffmpeg_noblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from .ffmpeg_writer_noblock import FFmpegWriterNoblock
from typing import Callable
import ffmpegcv
import threading
import numpy as np


def noblock(fun:Callable, *v_args, **v_kargs):
Expand All @@ -16,3 +18,31 @@ def noblock(fun:Callable, *v_args, **v_kargs):
raise ValueError('The function is not supported as a Reader or Writer')

return proxyfun


class ReadLast(threading.Thread, ffmpegcv.FFmpegReader):
def __init__(self, fun, *args, **kvargs):
threading.Thread.__init__(self)
ffmpegcv.FFmpegReader.__init__(self)
self.vid = vid = fun(*args, **kvargs)
self.out_numpy_shape = vid.out_numpy_shape
self.width, self.height = vid.width, vid.height
self.img = np.zeros(self.out_numpy_shape, dtype=np.uint8)
self.ret = True
self._isopen = True
self.start()

def read(self):
if self.ret:
self.iframe += 1
return self.ret, self.img

def release(self):
self._isopen = False
self.vid.release()

def run(self):
while self._isopen:
self.ret, self.img = self.vid.read()
if self.ret:
break
8 changes: 1 addition & 7 deletions ffmpegcv/ffmpeg_reader_stream_realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def VideoReader(
'-rtsp_transport tcp '
'-fflags nobuffer -flags low_delay -strict experimental '
f' -i {stream_url}'
#'-vf extractplanes=y '
f" -pix_fmt {pix_fmt} -f rawvideo pipe:"
)

Expand All @@ -40,11 +39,6 @@ def VideoReader(
}[pix_fmt]
vid.process = run_async(vid.ffmpeg_cmd)
vid.isopened = True
# producer
# vid.step = 1
# vid.q = Queue(maxsize=30)
# producer = ProducerThread(vid, vid.q)
# producer.start()
return vid

def read(self):
Expand All @@ -55,4 +49,4 @@ def read(self):
self.iframe += 1
img = None
img = np.frombuffer(in_bytes, np.uint8).reshape(self.out_numpy_shape)
return True, img
return True, img

0 comments on commit 385ca81

Please sign in to comment.