Search code examples
javaconvertersbufferedimagempegjcodec

Java BufferedImages from .ts File


How can I get a BufferedImage (Frame at defined Position) from a .ts File in Java? I don't want to use any JNI / CLI Wrapper if possible.

     System.out.println( JCodecUtil.detectFormat( file ) );
        Demuxer demuxer = JCodecUtil.createDemuxer( JCodecUtil.detectFormat( file ), file );
        for ( DemuxerTrack demuxerTrack : demuxer.getVideoTracks() ) {
            Packet packet;
            while ( ( packet = demuxerTrack.nextFrame() ) != null ) {
                System.out.println( "frame " + packet.getDuration() );
            }
        }

The output of the first Snippet is just MPEG_TS [ERROR] . (:0): Format MPEG_TS is not supported MPEG_TS [ERROR] . (:0): Format MPEG_TS is not supported

    for ( DemuxerTrack demuxerTrack : JCodecUtil.createM2TSDemuxer( file, TrackType.VIDEO ).v1.getTracks() ) {
        Packet packet;
        while ( ( packet = demuxerTrack.nextFrame() ) != null ) {
            System.out.println( "frame : " + ImageIO.read( new ByteArrayInputStream( packet.getData().array() ) ) );
        }
    }

In this Snippet, it just outputs null for each Frame. How to fix this?


Solution

  • Picture tmp = Picture.create(1920, 1088, ColorSpace.YUV420);
    VideoDecoder vd = JCodecUtil.createVideoDecoder(JCodecUtil.detectDecoder(data.duplicate()), data.duplicate());
    Picture pic = vd.decodeFrame(data, tmp.getData());
    BufferedImage buf = AWTUtil.toBufferedImage(pic);