I am trying to stream video from my webcam to YouTube using Xuggler and YouTube's live streaming API. I was successful in connecting to YouTube and in creating a broadcast by following the example provided here.
I used Xuggler to encode the videos and send it to YouTube using the code below. The steps for encoding the videos is taken from a post here.
I am not getting any error in my console when I run my program, but the stream is not going live in Youtube. In Youtube Live control room, it keeps showing a message that "We are not receiving data from your encoder. Please make sure it is configured correctly in the Ingestion Settings page."
What am I doing wrong? Any help or pointers will be highly appreciated...
String url = returnedStream.getCdn().getIngestionInfo().getIngestionAddress();
String fileName = returnedStream.getCdn().getIngestionInfo().getStreamName() ;
IContainer container = IContainer.make();
IContainerFormat containerFormat_live = IContainerFormat.make();
containerFormat_live.setOutputFormat("flv", url + "/"+ fileName, null);
container.setInputBufferLength(0);
int retVal = container.open(url + "/"+ fileName, 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(true);
while (i < framesToEncode) {
//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();
}
来源:https://stackoverflow.com/questions/29915767/youtube-live-streaming-api-stream-is-not-appearing-in-youtube