【问题标题】:Incorrect Mpeg file format after being transferred by Elixir/HTTPoisonElixir/HTTPoison 传输后的 Mpeg 文件格式不正确
【发布时间】:2016-09-01 16:54:09
【问题描述】:

跟随我的(愚蠢的)question 并阅读this

我设法连接到我的摄像头,从中获取流量并将其转储到 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 文件。

这是文件的开头。

--myboundary Content-Type: image/jpeg Content-Length: 9609

ˇÿˇ‡JFIFˇ˛ W»XW»Xˇ˛ ß2¨ÃéTY"ˇ€C

它尝试使用几个参数都没有成功,看起来该文件未被识别为 mpeg 文件。

有什么想法吗? 问候, 皮埃尔

【问题讨论】:

  • 当用stream_to_file 写入文件时,什么保证了块(字节)的原始顺序?
  • 感谢您的帮助。我什么都不怕。我唯一可以用 iHex 检查的是里面的所有 jpeg 文件都被 --myboundary 清楚地分隔我确实在 --myboundary 分隔符之间复制了一个随机块,把它放在一个文件中,我有一个正确的图像。
  • MediaInfo 似乎无法从中提取任何信息。我忘了写标题还是什么?

标签: elixir mpeg httpoison


【解决方案1】:

事实上,Elixir/HTTPoison 或 HTTPotion 并不负责。

来自Wikipedia

在多媒体中,Motion JPEG(M-JPEG 或 MJPEG)是一种视频压缩 数字的每个视频帧或隔行扫描场的格式 视频序列被单独压缩为 JPEG 图像。起初 为多媒体 PC 应用程序开发,M-JPEG 现在被 视频捕获设备,例如数码相机、IP 摄像机和 网络摄像头;以及非线性视频编辑系统。它是原生的 受 QuickTime Player、PlayStation 控制台和 Web 支持 Safari、Google Chrome、Mozilla Firefox 和 Microsoft 等浏览器 边缘。

我尝试从原始文件本身手动提取 Jpeg 文件,在成功尝试后,我发现最快的方法是使用 python(如果您不在 2.7 中,可能会略有不同,例如:cv2.IMREAD_COLOR)和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) 

问候,

皮埃尔

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 2014-01-07
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多