2018년 2월 4일 일요일

xuggler 로 스트리밍 정보 및 read

package com.oopscraft.test.generator;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;

public class VideoInfo {

    private static final String filename = "rtmp://101.79.242.18/ch14/ch14_800.stream?id=3101&sa=67e56ce383fa39821e1dbe9d6445fcf5aa0f6e83fc5f719b64717325ac30693143dc407c681599bb365d38c1140db122b96fc58ed1e83a553c3a3ecbda295b2d";

    public static void main(String[] args) {
        // first we create a Xuggler container object
        IContainer container = IContainer.make();

        // we attempt to open up the container
        int result = container.open(filename, IContainer.Type.READ, null);

        // check if the operation was successful
        if (result<0)
            throw new RuntimeException("Failed to open media file");

        // query how many streams the call to open found
        int numStreams = container.getNumStreams();

        // query for the total duration
        long duration = container.getDuration();

        // query for the file size
        long fileSize = container.getFileSize();

        // query for the bit rate
        long bitRate = container.getBitRate();

        System.out.println("Number of streams: " + numStreams);
        System.out.println("Duration (ms): " + duration);
        System.out.println("File Size (bytes): " + fileSize);
        System.out.println("Bit Rate: " + bitRate);

        // iterate through the streams to print their meta data
        for (int i=0; i<numStreams; i++) {

            // find the stream object
            IStream stream = container.getStream(i);

            // get the pre-configured decoder that can decode this stream;
            IStreamCoder coder = stream.getStreamCoder();

            System.out.println("*** Start of Stream Info ***");
            System.out.printf("stream %d: ", i);
            System.out.printf("type: %s; ", coder.getCodecType());
            System.out.printf("codec: %s; ", coder.getCodecID());
            System.out.printf("duration: %s; ", stream.getDuration());
            System.out.printf("start time: %s; ", container.getStartTime());
            System.out.printf("timebase: %d/%d; ",
stream.getTimeBase().getNumerator(),
tream.getTimeBase().getDenominator());
System.out.printf("coder tb: %d/%d; ",
                 coder.getTimeBase().getNumerator(),
                 coder.getTimeBase().getDenominator());
            System.out.println();

            if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO) {
                System.out.printf("sample rate: %d; ", coder.getSampleRate());
                System.out.printf("channels: %d; ", coder.getChannels());
                System.out.printf("format: %s", coder.getSampleFormat());
            }
            else if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
                System.out.printf("width: %d; ", coder.getWidth());
                System.out.printf("height: %d; ", coder.getHeight());
                System.out.printf("format: %s; ", coder.getPixelType());
                System.out.printf("frame-rate: %5.2f; ", coder.getFrameRate().getDouble());
            }

            System.out.println();
            System.out.println("*** End of Stream Info ***");
        }
    }
}

package com.oopscraft.test.generator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import com.xuggle.xuggler.ICodec;
import com.xuggle.xuggler.IContainer;
import com.xuggle.xuggler.IPacket;
import com.xuggle.xuggler.IStream;
import com.xuggle.xuggler.IStreamCoder;

public class Record {

        public static void main(String[] args) throws Exception {
                IContainer readContainer = IContainer.make();
                IContainer writeContainer = IContainer.make();
                int videoStreamId=0;
                int i = readContainer.open("rtmp://101.79.242.18/ch14/ch14_800.stream?id=3101&sa=6465dbea334f3242be1ebe766e15b3fd9ab267539e5381f261f1962afcf86e31f5d630016195d2bad6b83d710d03f128162d6c79da7bd4af05b2f622255fb92e",IContainer.Type.READ, null, true, false);
                writeContainer.open("header1.flv", IContainer.Type.WRITE, null);

                if (i >= 0) {
                        IStream inVideoStream = readContainer.getStream(0);
                        IStreamCoder inVideocoder = inVideoStream.getStreamCoder();
                        inVideocoder.setCodec(ICodec.ID.CODEC_ID_FLV1);
                        inVideocoder.setTimeBase(inVideoStream.getTimeBase());
                        inVideocoder.setHeight(320);
                        inVideocoder.setWidth(240);
                        IStream outVideoStream = writeContainer.addNewStream(0);
                        outVideoStream.setStreamCoder(inVideocoder);
                        int video = inVideocoder.open();
                        if (video >= 0) {
                                System.out.println("Good videoStream");
                        } else {
                                System.out.println("Wrong videoStream");

                        }

                        IStream inAudioStream = readContainer.getStream(1);
                        IStreamCoder inAudioCoder = inAudioStream.getStreamCoder();
                        inAudioCoder.setCodec(ICodec.ID.CODEC_ID_MP3);
                        inAudioCoder.setSampleRate(44100);
                        inAudioCoder.setTimeBase(inAudioStream.getTimeBase());
IStream outAudioStream = writeContainer.addNewStream(1);
                        outAudioStream.setStreamCoder(inAudioCoder);
                        int audio = inAudioCoder.open();
                        if (audio >= 0) {
                                System.out.println("Good audioStream");
                        } else {
                                System.out.println("Wrong audioStream");
                        }
                        int header = writeContainer.writeHeader();
                        if (header == 0) {
                                System.out.println("good header");
                        } else {
                                System.out.println("wrong header" + header);
                        }
                        IPacket packet = IPacket.make();
File file = new File("data0-1000.manu");
FileChannel wChannel = new FileOutputStream(file, false).getChannel();
ByteBuffer bbuf = null;
long initTS=1000;

while (readContainer.readNextPacket(packet) >= 0 && packet.isComplete()) {
bbuf = packet.getByteBuffer();
if(packet.getTimeStamp()-initTS >= 1000 && packet.isKeyPacket())
{
file = new File("data"+initTS+"-"+packet.getTimeStamp()+".manu");
initTS=packet.getTimeStamp();
wChannel = new FileOutputStream(file, false).getChannel();
}
if(bbuf!=null)
wChannel.write(bbuf);
writeContainer.writePacket(packet);
System.out.println(packet.getTimeStamp());
                        }

                        //writeContainer.writeTrailer();
                        writeContainer.close();
                        wChannel.close();
                } else {
                        System.out.print("Wrong!!!");
                }
        }
}

댓글 없음:

댓글 쓰기

Creating CRC32 Hex string

public String getCRC32HexaString(String paramString) throws Exception  {   byte bytes[] = paramString.getBytes(DEFAULT_CHARSET);   Che...