2. 博文1中第三种方案安装nodejs过程中sudo apt-get install node-legacy 应该改为sudo apt-get install nodejs-legacy。
最近在做一个流媒体的项目,项目中需要采集摄像头的视频流到网页界面实时播放,一般ip摄像头的流格式都是rtsp的,虽然可以通过vlc实时播放,但是不如浏览器观看给用户的体验简单。
根据查找的资料和实际的实践,目前发现的切实可行的方案有以下几种(因为项目是采用java开发,因此下面的代码也主要使用java实现):
使用xuggle库直接解码rtsp流,解码结果直接发送给rtmp服务器,然后浏览器使用flash直接播放rtmp的视频流;
ffmpeg直接解码rtsp流,将解码结果使用http发送到nodejs服务器,nodejs服务器使用websocket发送给客户端,客户端使用canvas实时绘制图像;
下面详细介绍这几种方案。
FFmpeg是一个自由软件,可以运行音频和视频多种格式的录影、转换、流功能,包含了libavcodec——这是一个用于多个项目中音频和视频的解码器库,以及libavformat——一个音频与视频格式转换库。
FFmpeg的安装请参考:ubuntu上安装ffmpeg
xuggle官网是一个开源的资源库,能够让开发者更好的去对视频和音频文件进行解码、编码、以及录制等功能。xuggle是对ffmepg的封装,是一套基于ffmpeg的开发库。 使用非常方便。 在java中使用时,请在eclipse中导入其jar包。 xuggle-5.4-jar包下载
xuggle读取rtsp摄像头的代码如下:
import包如下:
1
2
3
4
5
|
import
com
.
xuggle
.
mediatool
.
IMediaReader
;
import
com
.
xuggle
.
mediatool
.
MediaListenerAdapter
;
import
com
.
xuggle
.
mediatool
.
ToolFactory
;
import
com
.
xuggle
.
mediatool
.
event
.
IVideoPictureEvent
;
|
其中:streamLocation是需要读取的rtsp地址
1
2
3
4
5
6
|
mediaReader
=
ToolFactory
.
makeReader
(
streamLocation
)
;
mediaReader
.
setBufferedImageTypeToGenerate
(
BufferedImage
.
TYPE_3BYTE
_BGR)
;
mediaReader
.
addListener
(
this
)
;
while
(
mediaReader
.
readPacket
(
)
==
null
&&
running
)
;
mediaReader
.
close
(
)
;
|
上面这段代码实现了rtsp的持续读取,那么读取到的数据怎么获取,很简单,实现以下的帧回调函数onVideoPicture即可。
1
2
3
4
5
6
7
8
9
10
|
/**
* Gets called when FFMPEG transcoded a frame
*/
public
void
onVideoPicture
(
IVideoPictureEvent
event
)
{
BufferedImage
frame
=
event
.
getImage
(
)
;
//stream是用户自定义的rtsp视频流的id,例如;streamId = this.hashcode()即可
images
.
put
(
streamId
,
frame
)
;
frameNr
++
;
}
|
以上的images是使用guava库的Cache建立的代码如下:
1
2
3
4
5
|
import
com
.
google
.
common
.
cache
.
Cache
;
import
com
.
google
.
common
.
cache
.
CacheBuilder
;
private
static
Cache
<
String
,
BufferedImage
>
images
=
null
;
|
使用html5 video标签+javax.ws.rs实现的网页显示代码如下:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
import
java
.
awt
.
image
.
BufferedImage
;
import
java
.
io
.
ByteArrayOutputStream
;
import
java
.
io
.
IOException
;
import
java
.
io
.
OutputStream
;
import
java
.
util
.
HashSet
;
import
java
.
util
.
Set
;
import
javax
.
imageio
.
ImageIO
;
import
javax
.
ws
.
rs
.
DefaultValue
;
import
javax
.
ws
.
rs
.
GET
;
import
javax
.
ws
.
rs
.
Path
;
import
javax
.
ws
.
rs
.
PathParam
;
import
javax
.
ws
.
rs
.
Produces
;
import
javax
.
ws
.
rs
.
QueryParam
;
import
javax
.
ws
.
rs
.
WebApplicationException
;
import
javax
.
ws
.
rs
.
core
.
Application
;
import
javax
.
ws
.
rs
.
core
.
Response
;
import
javax
.
ws
.
rs
.
core
.
StreamingOutput
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
com
.
google
.
common
.
cache
.
Cache
;
import
com
.
sun
.
jersey
.
api
.
container
.
httpserver
.
HttpServerFactory
;
import
com
.
sun
.
jersey
.
api
.
core
.
ApplicationAdapter
;
import
com
.
sun
.
net
.
httpserver
.
HttpServer
;
/**
* A simple webservice used to view results MJPEG streams.
* The webservice supports a number of calls different calls:
*
*
*
*
* pictures
*
* streams available at this service. Clicking an image will open the mjpeg
* stream
*
* never ending mjpeg formatted stream
*
*
* The service runs on port 8558 by default but this can be changed by using the
* port(int) method.
*
* @author Corne Versloot
*
*/
@
Path
(
"/streaming"
)
public
class
MjpegStreamingOp
extends
Application
{
private
static
Cache
<
String
,
BufferedImage
>
images
=
null
;
private
Logger
logger
=
LoggerFactory
.
getLogger
(
getClass
(
)
)
;
private
HttpServer
server
;
private
int
port
=
8558
;
private
int
frameRate
=
20
;
// this parameter decide the sleep time
public
MjpegStreamingOp
port
(
int
nr
)
{
this
.
port
=
nr
;
return
this
;
}
public
MjpegStreamingOp
framerate
(
int
nr
)
{
this
.
frameRate
=
nr
;
return
this
;
}
public
void
prepare
(
)
throws
IllegalArgumentException
,
IOException
{
//此处需要修改为从rtsp读进来的images存放的类
images
=
TCPClient
.
getImages
(
)
;
ApplicationAdapter
connector
=
new
ApplicationAdapter
(
new
MjpegStreamingOp
(
)
)
;
server
=
HttpServerFactory
.
create
(
"http://localhost:"
+
port
+
"/"
,
connector
)
;
server
.
start
(
)
;
}
/**
* Sets the classes to be used as resources for this application
*/
public
Set
<
Class
>>
getClasses
(
)
{
Set
<
Class
>
>
s
=
new
HashSet
<
Class
<
?
>>
(
)
;
s
.
add
(
MjpegStreamingOp
.
class
)
;
return
s
;
}
public
void
deactivate
(
)
{
server
.
stop
(
0
)
;
images
.
invalidateAll
(
)
;
images
.
cleanUp
(
)
;
}
@
GET
@
Path
(
"/streams"
)
@
Produces
(
"text/plain"
)
public
String
getStreamIds
(
)
throws
IOException
{
String
result
=
new
String
(
)
;
for
(
String
id
:
images
.
asMap
(
)
.
keySet
(
)
)
{
result
+=
"/streaming/picture/"
+
id
+
".jpeg\r\n"
;
}
System
.
out
.
println
(
"\r\n"
)
;
for
(
String
id
:
images
.
asMap
(
)
.
keySet
(
)
)
{
result
+=
"/streaming/mjpeg/"
+
id
+
".mjpeg\r\n"
;
}
return
result
;
}
@
GET
@
Path
(
"/picture/{streamid}.jpeg"
)
@
Produces
(
"image/jpg"
)
public
Response
jpeg
(
@
PathParam
(
"streamid"
)
final
String
streamId
)
{
BufferedImage
image
=
null
;
if
(
(
image
=
images
.
getIfPresent
(
streamId
)
)
!=
null
)
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
)
;
try
{
ImageIO
.
write
(
image
,
"jpg"
,
baos
)
;
byte
[
]
imageData
=
baos
.
toByteArray
(
)
;
return
Response
.
ok
(
imageData
)
.
build
(
)
;
// non streaming
// return Response.ok(new
// ByteArrayInputStream(imageDAta)).build(); // streaming
}
catch
(
IOException
ioe
)
{
logger
.
warn
(
"Unable to write image to output"
,
ioe
)
;
return
Response
.
serverError
(
)
.
build
(
)
;
}
}
else
{
return
Response
.
noContent
(
)
.
build
(
)
;
}
}
@
GET
@
Path
(
"/playmultiple"
)
@
Produces
(
"text/html"
)
public
String
showPlayers
(
@
DefaultValue
(
"3"
)
@
QueryParam
(
"cols"
)
int
cols
,
@
DefaultValue
(
"0"
)
@
QueryParam
(
"offset"
)
int
offset
,
@
DefaultValue
(
"6"
)
@
QueryParam
(
"number"
)
int
number
)
throws
IOException
{
// number = Math.min(6, number);
String
result
=
"
"
;
result
+=
"Streams: "
+
images
.
size
(
)
+
" (showing "
+
offset
+
" - "
+
Math
.
min
(
images
.
size
(
)
,
offset
+
number
)
+
")
" ;
result
+=
"
return
result
;
}
@
GET
@
Path
(
"/play"
)
@
Produces
(
"text/html"
)
public
String
showPlayers
(
@
QueryParam
(
"streamid"
)
String
streamId
)
throws
IOException
{
String
result
=
"
+
""
;
result
+=
"
return
result
;
}
@
GET
@
Path
(
"/mjpeg/{streamid}.mjpeg"
)
@
Produces
(
"multipart/x-mixed-replace; boundary=--BoundaryString\r\n"
)
public
Response
mjpeg
(
@
PathParam
(
"streamid"
)
final
String
streamId
)
{
StreamingOutput
output
=
new
StreamingOutput
(
)
{
private
BufferedImage
prevImage
=
null
;
private
int
sleep
=
1000
/
frameRate
;
@
Override
public
void
write
(
OutputStream
outputStream
)
throws
IOException
,
WebApplicationException
{
BufferedImage
image
=
null
;
try
{
while
(
(
image
=
images
.
getIfPresent
(
streamId
)
)
!=
null
)
if
(
prevImage
==
null
||
!
image
.
equals
(
prevImage
)
)
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
(
)
;
ImageIO
.
write
(
image
,
"jpg"
,
baos
)
;
byte
[
]
imageData
=
baos
.
toByteArray
(
)
;
outputStream
.
write
(
(
"--BoundaryString\r\n"
+
"Content-type: image/jpeg\r\n"
+
"Content-Length: "
+
imageData
.
length
+
"\r\n\r\n"
)
.
getBytes
(
)
)
;
outputStream
.
write
(
imageData
)
;
outputStream
.
write
(
"\r\n\r\n"
.
getBytes
(
)
)
;
outputStream
.
flush
(
)
;
}
Thread
.
sleep
(
sleep
)
;
}
outputStream
.
flush
(
)
;
outputStream
.
close
(
)
;
}
catch
(
IOException
ioe
)
{
logger
.
info
(
"Steam for ["
+
streamId
+
"] closed by client!"
)
;
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
;
return
Response
.
ok
(
output
)
.
header
(
"Connection"
,
"close"
)
.
header
(
"Max-Age"
,
"0"
)
.
header
(
"Expires"
,
"0"
)
.
header
(
"Cache-Control"
,
"no-cache, private"
)
.
header
(
"Pragma"
,
"no-cache"
)
.
build
(
)
;
}
}
以上代码参考自github上stormcv项目,项目地址为:https://github.com/sensorstorm/StormCV, 感谢原作者。 至此,打开网页 http://localhost:8558/streaming/tiles 即可查看到实时视频。 方案二的具体实现xuggle库转rtsp为rtmprtsp的reader:
你可能感兴趣的:(视频技术)
|