无法打开实时流的输出容器

时间:2015-05-27 17:52:04

标签: java youtube live-streaming youtube-data-api xuggler

我正在制作YouTube广播Java程序。到目前为止,我可以使用此程序创建live event

https://github.com/youtube/api-samples/tree/master/java

有关我目前所获得的详细信息,请参阅我的另一个问题:

https://stackoverflow.com/questions/30449366/how-to-send-video-stream-for-live-event-using-youtube-broadcast-in-java

现在,接下来我想创建一个视频流,该视频流将传递到直播的YouTube API,以便我的视频将在YouTube上作为直播活动进行广播。

为此,我使用的是Xuggler库5.2。但是,当我尝试为Xuggler运行示例程序时,我收到以下消息,既没有工作,也没有我的网络摄像头启动。

源代码:

import java.awt.Dimension;
import java.awt.image.BufferedImage;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamResolution;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IContainerFormat;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IPixelFormat;
import com.xuggle.xuggler.IRational;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;
import com.xuggle.xuggler.IVideoPicture;
import com.xuggle.xuggler.video.ConverterFactory;
import com.xuggle.xuggler.video.IConverter;



public class HelloWorld {

    public static void main(String[] args) {
        IContainer container = IContainer.make();
        IContainerFormat containerFormat_live = IContainerFormat.make();
        containerFormat_live.setOutputFormat("flv", "rtmp://a.rtmp.youtube.com" + "/"+ "live2", null);
        container.setInputBufferLength(0);
        int retVal = container.open("rtmp://a.rtmp.youtube.com" + "/"+ "live2", IContainer.Type.WRITE, containerFormat_live);
        if (retVal < 0) {
            System.err.println("Could not open output container for live stream");
            System.exit(1);
        }

        Dimension size = WebcamResolution.QVGA.getSize();

        IStream stream = container.addNewStream(0);
        IStreamCoder coder = stream.getStreamCoder();
        ICodec codec = ICodec.findEncodingCodec(ICodec.ID.CODEC_ID_H264);
        coder.setNumPicturesInGroupOfPictures(4);
        coder.setCodec(codec);
        coder.setBitRate(500000);
        coder.setPixelType(IPixelFormat.Type.YUV420P);
        coder.setHeight(size.height);
        coder.setWidth(size.width);

        System.out.println("[ENCODER] video size is " + size.width + "x" + size.height);
        coder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, true);
        coder.setGlobalQuality(0);
        IRational frameRate = IRational.make(24, 1);
        coder.setFrameRate(frameRate);
        coder.setTimeBase(IRational.make(frameRate.getDenominator(), frameRate.getNumerator()));

        coder.open();
        container.writeHeader();
        long firstTimeStamp = System.currentTimeMillis();
        long lastTimeStamp = -1;
        int i = 0;
        try {
         //   Robot robot = new Robot();
            Webcam webcam = Webcam.getDefault();
            webcam.setViewSize(size);
            webcam.open();

            while (i < 100000000) {
                //long iterationStartTime = System.currentTimeMillis();
                long now = System.currentTimeMillis();
                //grab the screenshot
                BufferedImage image = webcam.getImage();
                //convert it for Xuggler
                BufferedImage currentScreenshot = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
                currentScreenshot.getGraphics().drawImage(image, 0, 0, null);
                //start the encoding process
                IPacket packet = IPacket.make();
                IConverter converter = ConverterFactory.createConverter(currentScreenshot, IPixelFormat.Type.YUV420P);
                long timeStamp = (now - firstTimeStamp) * 1000; 
                IVideoPicture outFrame = converter.toPicture(currentScreenshot, timeStamp);
                if (i == 0) {
                    //make first frame keyframe
                    outFrame.setKeyFrame(true);
                }
                outFrame.setQuality(0);
                coder.encodeVideo(packet, outFrame, 0);
                outFrame.delete();
                if (packet.isComplete()) {
                    container.writePacket(packet);
                    System.out.println("[ENCODER] writing packet of size " + packet.getSize() + " for elapsed time " + ((timeStamp - lastTimeStamp) / 1000));
                    lastTimeStamp = timeStamp;
                }
                System.out.println("[ENCODER] encoded image " + i + " in " + (System.currentTimeMillis() - now));
                i++;
                try {
                    Thread.sleep(Math.max((long) (1000 / frameRate.getDouble()) - (System.currentTimeMillis() - now), 0));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

运行时,收到此消息:

23:13:38,807 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
23:13:38,808 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
23:13:38,811 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration.
23:13:38,878 |-WARN in ch.qos.logback.core.ConsoleAppender[console] - This appender no longer admits a layout as a sub-component, set an encoder instead.
23:13:38,878 |-WARN in ch.qos.logback.core.ConsoleAppender[console] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
23:13:38,878 |-WARN in ch.qos.logback.core.ConsoleAppender[console] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details

23:13:46.644 [main] ERROR org.ffmpeg - RTMP_ReadPacket, failed to read RTMP packet header
Could not open output container for live stream
23:13:46.650 [main] ERROR com.xuggle.xuggler - URL: rtmp://a.rtmp.youtube.com/live2; Error: could not open file (../../../../../../../csrc/com/xuggle/xuggler/Container.cpp:516)

我不明白出了什么问题。为什么我的网络摄像头无法启动?

1 个答案:

答案 0 :(得分:0)

读取xuggler库5.2 jar的清单文件(MANIFEST.MF)以获取正确版本的依赖项jar,收集正确的版本jar并将它们添加到类路径中以解决依赖项和版本问题。