Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

video streaming - avconv tool executed from Java run time stops encoding after 8 minutes

Here I am trying to encode live rtmp stream coming from Flash Media Server and broadcasting low bit rate stream by using avconv tool of Libav. Libav is installed on Ubuntu OS. The encoded stream runs for 8 mins only. As avconv tool is started by using java run time environment. The Java Code is given below -

    public class RunnableStream implements Runnable
    {
        String inStream,outStream,width,height,bitRate,frameRate,fname,line,ar,audioBitRate,audioChannel;
        public RunnableStream(String fname,String inStream,String outStream,String ar,String audioBitRate,String audioChannel,String width,String height,String bitRate,String frameRate)
        {
            this.fname=fname;
            this.inStream=inStream;
            this.outStream=outStream;
            this.width=width;
            this.height=height;
            this.bitRate=bitRate;
            this.frameRate=frameRate;
            this.ar=ar;
            this.audioBitRate=audioBitRate;
            this.audioChannel=audioChannel;

        }

        public void run() {
            Process pr;
            try {
                pr = Runtime.getRuntime().exec("avconv -async 15  -i  "+inStream+" -shortest -s "+width +"*"+height +"  -r " +frameRate+" -b:v "+bitRate+" -ab "+audioBitRate+" -ac "+audioChannel+" -ar "+ar+" -f flv "+outStream);

               InputStream in1 = pr.getInputStream();
               InputStream in = pr.getErrorStream();
    int c1;
    while ((c1 = in1.read()) != -1)
    {
        System.out.print((char)c1);
    }

    int c;
    while ((c = in.read()) != -1)
    {
        System.out.print((char)c);
    }
    pr.waitFor();
    in.close();
    in1.close();

            }catch(Exception e){e.printStackTrace();}
        }
    }

But when same encoding scheme or command is applied directly to command prompt then it can run for atleast 1 hour. Command line statement as given below -

avconv -async 15 -i  rtmp://IP/live/streamname -shortest -s 176*144  -r 10 -b:v 56k  -ab 12k -ac 1 -ar 22050 -f flv rtmp://IP/live/streamname2
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume this code is meant to drain stdout/stderr from the process:

int c1;
while ((c1 = in1.read()) != -1)
{
    System.out.print((char)c1);
}

int c;
while ((c = in.read()) != -1)
{
    System.out.print((char)c);
}

Unfortunately, it will only actually read from in1 (stdout) until the process has finished, then it will read from in (stderr). That means if the process is writing more data to stderr than its buffer can accommodate, it will block - exhibiting exactly the behaviour you're seeing. That isn't definitely the cause, but it seems likely to me.

You should be reading from these streams in different threads, basically - that way you'll read from both streams without having to wait for the process to finish.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...