5

私は Java でビデオ アプリケーションを作成してffmpegおり、その出力を実行して標準出力にキャプチャしています。Java の の代わりに Apache Commons-Exec を使用することにしRuntimeました。ただし、すべての出力をキャプチャするのは困難です。

プロセス間通信の標準的な方法であるため、パイプを使用するのがよいと思いました。ただし、 and を使用した私のセットアップPipedInputStreamPipedOutputStream間違っています。動作しているように見えますが、ストリームの最初の 1042 バイトのみで、不思議なことにPipedInputStream.PIPE_SIZE.

私はパイプの使用にあまり関心がありませんが、データの速度と量 (解像度 512x384 の 1 分 20 秒のビデオでは 690Mのパイプ データが生成される) のために、(可能であれば) ディスク I/O の使用を避けたいと考えています。

パイプからの大量のデータを処理するための最適なソリューションについて考えていますか? 私の 2 つのクラスのコードは以下のとおりです。(はい、sleep悪いです。それについての考えは? wait()notifyAll()?)

WriteFrames.java

public class WriteFrames {
    public static void main(String[] args) {
        String commandName = "ffmpeg";
        CommandLine commandLine = new CommandLine(commandName);
        File filename = new File(args[0]);
        String[] options = new String[] { 
                "-i",
                filename.getAbsolutePath(),
                "-an",
                "-f",
                "yuv4mpegpipe",
                "-"};

        for (String s : options) {
            commandLine.addArgument(s);
        }



        PipedOutputStream output = new PipedOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
        DefaultExecutor executor = new DefaultExecutor();

        try {
            DataInputStream is = new DataInputStream(new PipedInputStream(output));
            YUV4MPEGPipeParser p = new YUV4MPEGPipeParser(is);
            p.start();

            executor.setStreamHandler(streamHandler);
            executor.execute(commandLine);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

YUV4MPEGPipeParser.java

public class YUV4MPEGPipeParser extends Thread {

    private InputStream is;
    int width, height;

    public YUV4MPEGPipeParser(InputStream is) {
        this.is = is;
    }

    public void run() {
        try {
            while (is.available() == 0) {
                Thread.sleep(100);
            }

            while (is.available() != 0) {
                // do stuff.... like write out YUV frames
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
4

1 に答える 1

4

問題は、YUV4MPEGPipeParser クラスの run メソッドにあります。2 つの連続するループがあります。2 番目のループは、ストリームで現在利用可能なデータがない場合、すぐに終了します (たとえば、これまでのすべての入力がパーサーによって処理され、ffmpeg またはストリーム ポンプが新しいデータを提供するのに十分な速度ではなかった -> available() == 0 -> ループが終了します -> ポンプスレッドが終了します)。

これら 2 つのループとスリープを取り除き、処理できるデータがあるかどうかを確認する代わりに単純なブロッキング read() を実行するだけです。また、パーサー コードが別のスレッドで開始されるため、おそらく wait()/notify() や sleep() も必要ありません。

run() メソッドのコードを次のように書き直すことができます。

public class YUV4MPEGPipeParser extends Thread {

    ...

    // optimal size of buffer for reading from pipe stream :-)
    private static final int BUFSIZE = PipedInputStream.PIPE_SIZE; 

    public void run() {
        try {
            byte buffer[] = new byte[BUFSIZE];
            int len = 0;
            while ((len = is.read(buffer, 0, BUFSIZE) != -1) {
                // we have valid data available 
                // in first 'len' bytes of 'buffer' array.

                // do stuff.... like write out YUV frames
            }
         } catch ...
     }
 }
于 2009-06-05T22:11:09.897 に答える