我设法连接到我的摄像头,从它获得流,并将它转储到一个mpeg文件中。这是更清晰的代码。
test "request headers from cam" do
options = [basic_auth: {"LOGIN","PASSWORD"}, ibrowse: [{:save_response_to_file, true}]]
{:ok, %HTTPoison.AsyncResponse{id: id}} = HTTPoison.get "http://x.x.x.x/axis-cgi/mjpg/video.cgi?duration=2&resolution=320x240",[], [stream_to: self, recv_timeout: :infinity, hackney: options
]
{:ok, file} = File.open("/tmp/test.mpeg", [:write])
retour = stream_to_file(id,file)
send self,{:retour, retour}
assert_receive {:retour ,:ok}, 10_000
end
defp stream_to_file(id, output) do
receive do
%HTTPoison.AsyncStatus{ id: ^id, code: code} ->
IO.puts " AsyncStatus"
stream_to_file(id,output)
%HTTPoison.AsyncHeaders{ id: ^id} ->
stream_to_file(id,output)
%HTTPoison.AsyncEnd{ id: ^id} ->
IO.puts " AsyncEnd received"
:ok
%HTTPoison.AsyncChunk{id: ^id, chunk: chunk} ->
IO.binwrite(output, chunk)
stream_to_file(id, output)
end
end测试运行良好,一个文件按预期生成,但是当我尝试读取它(使用几个播放器)时,我可以偷偷地从摄像机中看到一个图像,然后它就停止了。
大小(取决于参数)可能很重要,在编辑文件时,我可以清楚地猜到这些文件是连续的Jpeg文件。
这里是文件的开头。
-我的边界内容-类型:图像/jpeg内容-长度: 9609 ˇ€ˇ‡JFIFˇ˛W XW Xˇ˛2éTY“ˇ欧元C”
它试图使用几个参数,但没有成功,看起来文件不能被识别为mpeg文件。
有什么想法吗?你好,皮埃尔
发布于 2016-09-02 08:33:21
事实上,药剂/HTTPoison或HTTPotion并不对此负责。
来自维基百科
在多媒体中,Motion (MJPEG或MJPEG)是一种视频压缩格式,其中数字视频序列的每个视频帧或交错字段被单独压缩为JPEG图像。M最初是为多媒体个人电脑应用开发的,现在被数码相机、IP摄像机和网络摄像机等视频采集设备所使用;也被非线性视频编辑系统所使用。QuickTime播放器、PlayStation控制台以及诸如Safari、Google、Mozilla和Microsoft等web浏览器都对此进行了本机支持。
我尝试从原始文件本身手动提取Jpeg文件,在成功尝试之后,我发现最快的方法是使用python (如果您没有使用2.7 (例如: cv2.IMREAD_COLOR)和OpenCv )和OpenCv。
import cv2
import urllib
import numpy as np
import os
stream=open('./example.mjpg','rb')
sizemax = os.path.getsize("./example.mjpg")
bytes=''
allbytes = 0
nimage = 0
while nimage * 1024 < sizemax:
nimage= nimage + 1
bytes+=stream.read(1024)
allbytes += allbytes + 1024
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a!=-1 and b!=-1:
jpg = bytes[a:b+2]
bytes= bytes[b+2:]
i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
cv2.imshow('i',i)
if cv2.waitKey(1) ==27:
exit(0) 致以敬意,
皮埃尔
https://stackoverflow.com/questions/39277015
复制相似问题