-
Notifications
You must be signed in to change notification settings - Fork 2
/
ffmpeg.py
62 lines (54 loc) · 1.54 KB
/
ffmpeg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
An FFMPEG wrapper for using CUDA-based streaming
Author: Jose Stovall | [email protected] | oitsjustjose@git
Center for Urban Informatics and Progress | CUIP | utccuip.com
"""
import subprocess
import cv2
import numpy as np
class CUDAStreamer:
"""
Uses the natively compiled FFMPEG command to get a CV2 image from RTSP
Arguments
"""
def __init__(self, camera_url: str, width: int, height: int):
self.cmd = subprocess.Popen(
[
"ffmpeg",
"-hide_banner",
"-loglevel",
"panic",
"-hwaccel",
"nvdec",
"-reorder_queue_size",
"10000",
"-rtsp_transport",
"tcp",
"-i",
camera_url,
"-vsync",
"0",
"-vcodec",
"h264_nvenc",
"-f",
"image2pipe",
"-pix_fmt",
"bgr24",
"-vcodec",
"rawvideo",
"-",
],
stdout=subprocess.PIPE,
bufsize=10,
)
self.width = width
self.height = height
def get_image(self) -> np.array:
raw = self.cmd.stdout.read(self.width * self.height * 3)
image = np.fromstring(raw, dtype="uint8")
try:
image = image.reshape((self.height, self.width, 3))
except ValueError:
pass
self.cmd.stdout.flush()
return image