4

我想编写一个从树莓派 csi 相机读取 h264 流的 java 应用程序。csi 相机的接口是命令行 c 程序“raspivid”,它通常将捕获的视频写入文件。使用选项“-o -” raspivid 将视频写入标准输出,此时我想捕获 h264 流并“管道”它而不更改数据。我的第一步是编写一个应用程序,它从标准输出读取数据并将其写入文件而不更改数据(这样你就得到了一个可播放的 .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");
  }

谢谢!

4

2 回答 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));
  } 
于 2013-08-07T15:17:46.533 回答
0

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

就正确的类而言,OutputStreamWriter 看起来像是在进行您不需要发生的转换。您的流以字节为单位。它需要保持这种状态。

你需要一个FileOutputStream吗?

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

于 2013-08-06T21:36:49.663 回答