簡體   English   中英

(raspberry pi csi camera)使用java從raspivid-stdout讀取h264數據

[英](raspberry pi csi camera) Reading h264 data from raspivid-stdout with java

我想寫一個java應用程序,它從raspberry pi csi相機中讀取h264流。 csi相機的界面是命令行c程序“raspivid”,它通常將捕獲的視頻寫入文件。 使用選項“-o - ”,raspivid將視頻寫入stdout,此時我想捕獲h264流並“管道”它而不更改數據。 我的第一步是編寫一個應用程序,它從stdout讀取數據並將其寫入文件而不更改數據(因此您可以獲得可播放的.h264文件)。 我的問題是寫入文件總是損壞,當我用記事本++打開損壞的文件時,我可以看到與可玩的文件相比,存在一般不同的“符號”。 我認為問題是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");
  }

謝謝!

解決了這個問題! InputStreamReader不是必需的,並將字節流轉換為字符流,無法進行反向轉換! 這是工作代碼(將stdout-byte-stream寫入文件):

  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));
  } 

這個線程從Raspi論壇可能有一些有用的信息。

就正確的類而言,OutputStreamWriter看起來正在進行您不需要進行的轉換 你的流以字節為單位。 它需要保持這種狀態。

你需要一個FileOutputStream嗎?

編輯:WHOOPS! 誤解。 你已經有了fos。 但是肯定你的作家正在進行轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM