2010-07-09 16 views
13

मैं जावा एप्लिकेशन में जेनरेट किए गए लाइव वीडियो को स्ट्रीम करने के तरीकों को खोजने का प्रयास कर रहा हूं। एप्लिकेशन को स्वयं के स्क्रीनशॉट लेने और इन्हें वीडियो स्ट्रीम में एन्कोड करने और स्ट्रीम प्रकाशित करने की आवश्यकता है।जावा एप्लिकेशन के भीतर से लाइव वीडियो कैसे प्रेषित करें?

अब तक मैं एक वीडियो फ़ाइल में स्क्रीनशॉट एन्कोड करने के लिए Xuggler (FFMPEG के शीर्ष पर जावा लाइब्रेरी) का उपयोग कर रहा हूं। यह बहुत अच्छा काम करता है। Xuggler आरटीएमपी के माध्यम से लाइव वीडियो संचारित करने में सक्षम होने का दावा करता है लेकिन मुझे इस प्रोग्रामेटिक तरीके से कैसे करें इस पर कोई दस्तावेज नहीं मिला है।

  1. क्या कोई जानता है कि Xuggler से प्रोग्रामेटिक रूप से आरटीएमपी वीडियो स्ट्रीम कैसे करें?
  2. क्या किसी के पास अन्य पुस्तकालयों पर कोई सुझाव है जिसका उपयोग मैं एक ही परिणाम प्राप्त करने के लिए कर सकता हूं? मैं एमपीईजी 2 में आरटीपी पर वीडियो स्ट्रीम करना पसंद करूंगा।

मुझे किसी अन्य प्रतिक्रिया के साथ Xuggler मंच here पर एक बहुत ही समान प्रश्न पूछने वाला कोई और मिला।

मैं JMF में देखा है और यह अन्य कारणों के लिए एक विकल्प नहीं है।

उत्तर

20

ईमानदारी से जेएमएफ के साथ अपना समय बर्बाद न करें, आप उस पेशकश को मृत मान सकते हैं। यहां बताया गया है कि आप h.264 का उपयोग कर आरटीएमपी स्ट्रीम में स्क्रीन शॉटिंग कैसे करेंगे (उदाहरण के लिए [email protected] के लिए धन्यवाद)। कोड यहाँ दिखाई नहीं देता है, तो इसके लिए pastebin है: http://pastebin.com/sJHwj0nW के रूप में मैं हो सकता है

import com.xuggle.xuggler.Configuration; 
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; 
import java.awt.AWTException; 
import java.awt.Rectangle; 
import java.awt.Robot; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 

public class XugglerRtmpReferenceImpl { 

    private static String url = "rtmp://your.test.server/screen/"; 
    private static String fileName = "test/teststream"; 
    private static int framesToEncode = 60; 
    private static int x = 0; 
    private static int y = 0; 
    private static int height = 480; 
    private static int width = 640; 

    public static void main(String[] args) { 
     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); 
     } 
     IStream stream = container.addNewStream(0); 
     IStreamCoder coder = stream.getStreamCoder(); 
     ICodec codec = ICodec.findEncodingCodec(ICodec.ID.CODEC_ID_H264); 
     coder.setNumPicturesInGroupOfPictures(5); 
     coder.setCodec(codec); 
     coder.setBitRate(200000); 
     coder.setPixelType(IPixelFormat.Type.YUV420P); 
     coder.setHeight(height); 
     coder.setWidth(width); 
     System.out.println("[ENCODER] video size is " + width + "x" + height); 
     coder.setFlag(IStreamCoder.Flags.FLAG_QSCALE, true); 
     coder.setGlobalQuality(0); 
     IRational frameRate = IRational.make(5, 1); 
     coder.setFrameRate(frameRate); 
     coder.setTimeBase(IRational.make(frameRate.getDenominator(), frameRate.getNumerator())); 
     Properties props = new Properties(); 
     InputStream is = XugglerRtmpReferenceImpl.class.getResourceAsStream("/libx264-normal.ffpreset"); 
     try { 
      props.load(is); 
     } catch (IOException e) { 
      System.err.println("You need the libx264-normal.ffpreset file from the Xuggle distribution in your classpath."); 
      System.exit(1); 
     } 
     Configuration.configure(props, coder); 
     coder.open(); 
     container.writeHeader(); 
     long firstTimeStamp = System.currentTimeMillis(); 
     long lastTimeStamp = -1; 
     int i = 0; 
     try { 
      Robot robot = new Robot(); 
      while (i < framesToEncode) { 
       //long iterationStartTime = System.currentTimeMillis(); 
       long now = System.currentTimeMillis(); 
       //grab the screenshot 
       BufferedImage image = robot.createScreenCapture(new Rectangle(x, y, width, height)); 
       //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 (AWTException e) { 
      e.printStackTrace(); 
     } 
     container.writeTrailer(); 
    } 
} 
+0

प्रयास करें, मैं एक कोड ब्लॉक के लिए अपने बहुत लंबा लगता है। –

+2

धन्यवाद। इससे मुझे सही दिशा में इंगित करने में मदद मिली। – Tansir1

+0

लाइव से कैमरे और माइक से xuggler स्ट्रीम वीडियो –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^