当前位置: 首页 > 工具软件 > Xuggler > 使用案例 >

【OpenCV】【JavaCV】【Xuggler】【Java】获取视频的编解码器

傅宏恺
2023-12-01
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputStream);

        grabber.start();

        System.out.println(JSON.toJSONString(grabber));
        System.out.println(grabber.getVideoMetadata().get("encoder"));

AVC Coding  即为 H.264

{
    "aspectRatio":1,
    "audioBitrate":317375,
    "audioChannels":2,
    "audioCodec":86018,
    "audioFrameRate":46.875,
    "audioMetadata":{
        "creation_time":"2021-07-27T03:36:46.000000Z",
        "handler_name":"#Mainconcept MP4 Sound Media Handler",
        "language":"eng"
    },
    "audioOptions":{

    },
    "audioStream":1,
    "bitsPerPixel":0,
    "closeInputStream":true,
    "deinterlace":false,
    "delayedTime":0,
    "format":"mov,mp4,m4a,3gp,3g2,mj2",
    "formatContext":{
        "null":false
    },
    "frameNumber":0,
    "frameRate":25,
    "gamma":2.2,
    "imageHeight":1920,
    "imageMode":"COLOR",
    "imageScalingFlags":0,
    "imageWidth":1080,
    "lengthInAudioFrames":708,
    "lengthInFrames":378,
    "lengthInTime":15125333,
    "lengthInVideoFrames":378,
    "maxDelay":-1,
    "metadata":{
        "creation_time":"2021-07-27T03:36:46.000000Z",
        "major_brand":"mp42",
        "minor_version":"0",
        "compatible_brands":"mp42mp41"
    },
    "numBuffers":4,
    "options":{

    },
    "pixelFormat":3,
    "sampleFormat":1,
    "sampleMode":"SHORT",
    "sampleRate":48000,
    "sensorPattern":-1,
    "timeout":10000,
    "timestamp":0,
    "triggerMode":false,
    "videoBitrate":10681406,
    "videoCodec":27,
    "videoFrameRate":25,
    "videoMetadata":{
        "creation_time":"2021-07-27T03:36:46.000000Z",
        "handler_name":"\u001FMainconcept Video Media Handler",
        "language":"eng",
        "encoder":"AVC Coding"
    },
    "videoOptions":{

    },
    "videoStream":0
}

Xuggler

手动安装到本地Maven仓库

mvn install:install-file -Dfile=F:\xuggle-xuggler-5.4.jar -DgroupId=xuggle  -DartifactId=xuggle-xuggler  -Dversion=5.4 -Dpackaging=jar

        <!-- https://mvnrepository.com/artifact/xuggle/xuggle-xuggler -->
        <dependency>
            <groupId>xuggle</groupId>
            <artifactId>xuggle-xuggler</artifactId>
            <version>5.4</version>
        </dependency>
public static void main(String[] args) {
        long startTime =System.currentTimeMillis();
        String filename = "F:\\share\\XXX.mp4";
        // 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(),
                    stream.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 ***");

        }
    }
Number of streams: 2
Duration (ms): 15125333
File Size (bytes): 20776688
Bit Rate: 10989080
*** Start of Stream Info ***
stream 0: type: CODEC_TYPE_VIDEO; codec: CODEC_ID_H264; duration: 377000; start time: 0; timebase: 1/25000; coder tb: 1/50; 
width: 1080; height: 1920; format: YUV420P; frame-rate: 25.00; 
*** End of Stream Info ***
*** Start of Stream Info ***
stream 1: type: CODEC_TYPE_AUDIO; codec: CODEC_ID_AAC; duration: 726016; start time: 0; timebase: 1/48000; coder tb: 1/48000; 
sample rate: 48000; channels: 2; format: FMT_S16
*** End of Stream Info ***

Xuggler-JAR下载地址

https://www.dcm4che.org/maven2/xuggle/xuggle-xuggler/5.4/xuggle-xuggler-5.4.jar

 类似资料: