树莓派CSI相机:使用Java从raspivid-stdout读取h264数据

4
我想编写一个Java应用程序,从树莓派CSI相机读取h264流。 CSI相机的接口是命令行C程序“raspivid”,它通常将捕获的视频写入文件。使用选项“-o -”,raspivid会将视频写入stdout,此时我希望捕获h264流并“管道化”它而不更改数据。我的第一步是编写一个应用程序,该程序从stdout读取数据并将其写入文件,而不更改数据(因此您可以得到可播放的.h264文件)。我的问题是,所写文件总是损坏的,当我用notepad ++打开损坏的文件时,我可以看到与可播放文件相比有普遍不同的“符号”。我认为问题在于InputStreamReader()类,它将stdout-byte-stream转换为字符流。我无法找到正确的类来解决这个问题。这是我的实际代码:
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");
  }

谢谢!

2个回答

5

问题已解决! 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));
  } 

0

这个Raspi论坛的帖子可能会有一些有用的信息。

就正确的类而言,OutputStreamWriter看起来正在执行您不需要的转换。您的流是以字节形式存在的,请保持这种方式。

您需要一个FileOutputStream吗?

编辑:哎呀!我读错了。您已经有一个fos。但是,无论如何,您的编写器都在进行转换。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接