【问题标题】:(raspberry pi csi camera) Reading h264 data from raspivid-stdout with java(树莓派 csi 相机)使用 java 从 raspivid-stdout 读取 h264 数据
【发布时间】:2013-08-06 21:12:41
【问题描述】:

我想编写一个从树莓派 csi 相机读取 h264 流的 java 应用程序。 csi 相机的接口是命令行 c 程序“raspivid”,它通常将捕获的视频写入文件。 使用选项“-o -” raspivid 将视频写入标准输出,此时我想捕获 h264 流并“管道”它而不更改数据。 我的第一步是编写一个应用程序,它从 stdout 读取数据并将其写入文件而不更改数据(这样你就得到了一个可播放的 .h264 文件)。 我的问题是写入的文件总是损坏,当我用记事本++打开损坏的文件时,我可以看到与可播放的相比,存在一般不同的“符号”。 我认为问题在于 InputStreamReader() 类,它将标准输出字节流转换为字符流。 我无法为此找到合适的课程。 这是我的实际代码:

public static void main(String[] args) throws IOException, InterruptedException
  {
    System.out.println("START PROGRAM");
    try
    {
    Process p = Runtime.getRuntime().exec("raspivid -w 100 -h 100 -n -t 5000 -o -");

    FileOutputStream fos = new FileOutputStream("testvid.h264");
    Writer out = new OutputStreamWriter(fos);
    BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while (bri.read() != -1)
    {
      out.write(bri.read());
    }

    bri.close();
    out.close();
    }
    catch (Exception err)
    {
      err.printStackTrace();
    }
    System.out.println("END PROGRAM");
  }

谢谢!

【问题讨论】:

    标签: java inputstream stdout h.264 raspberry-pi


    【解决方案1】:

    解决了问题! InputStreamReader 不是必需的,将字节流转换为字符流,不可能进行反向转换! 这是工作代码(将标准输出字节流写入文件):

      public static void main(String[] args) throws IOException
      {
        System.out.println("START PROGRAM");
        long start = System.currentTimeMillis();
        try
        {
    
          Process p = Runtime.getRuntime().exec("raspivid -w 100 -h 100 -n -t 10000 -o -");
          BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
          //Direct methode p.getInputStream().read() also possible, but BufferedInputStream gives 0,5-1s better performance
          FileOutputStream fos = new FileOutputStream("testvid.h264");
    
          System.out.println("start writing");
          int read = bis.read();
          fos.write(read);
    
          while (read != -1)
          {
            read = bis.read();
            fos.write(read);
          }
          System.out.println("end writing");
          bis.close();
          fos.close();
    
        }
        catch (IOException ieo)
        {
          ieo.printStackTrace();
        }
        System.out.println("END PROGRAM");
        System.out.println("Duration in ms: " + (System.currentTimeMillis() - start));
      } 
    

    【讨论】:

      【解决方案2】:

      来自 Raspi 论坛的This thread 可能有一些有用的信息。

      就正确的类而言,OutputStreamWriter 看起来像是doing a conversion you don't need to happen。您的流以字节为单位。它需要保持这种状态。

      你需要FileOutputStream吗?

      编辑:哎呀!误解。你已经有一个fos。但可以肯定的是,您的作者正在转换。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多