簡體   English   中英

捕獲時的ffmpeg管道塊

[英]ffmpeg pipe blocks while capturing

我有以下代碼:

public InputStream getInputStream() throws Exception {
    try {
        process = Runtime.getRuntime().exec("ffmpeg -f dshow -i video=\"" + query + "\":audio=\"" + microPhoneName + "\" -r 25 -vcodec mpeg4 -acodec mp3 -f avi -");
        } 
        catch (Exception e) {
        }
    return process.getInputStream();
}

當我使用inputStream.read(b)命令時,它只能工作一小段時間(180到400次,具體取決於我使用的格式和編解碼器),然后inputStream鎖定read ,應用程序不再運行。

有什么問題? 內存飽和(ffmpeg進程內存至少為14mb)? 有沒有一種方法可以解除這種情況(清理內存,使用文件作為橋接來防止鎖定)?

當然,我需要一點“實時”,而不是“后處理”。 我不受限於使用ffmpeg,如有必要,我可以更改它。

閱讀本文后,我自己找到了解決方案:問題是errorStream已滿,必須讀取它才能讓process繼續其工作,因此我插入了一個使用errorStreamThread

public InputStream getInputStream() throws Exception {
    try {
        process = Runtime.getRuntime().exec("ffmpeg -f dshow -i video=\"" + query + "\":audio=\"" + microPhoneName + "\" -r 25 -vcodec mjpeg -acodec mp3 -f " + getContentExtension() + " -");
        new Thread("Webcam Process ErrorStream Consumer") {
            public void run() {
                InputStream i = process.getErrorStream();
                try {
                    while (!isInterrupted()) {
                        i.read(new byte[bufferLength]);
                    }
                } catch (IOException e) {
                }
            }
        }.start();
    } catch (Exception e) {
    }
    return process.getInputStream();
}

暫無
暫無

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

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