首页 > 解决方案 > 如何使用 JMuxer.js 播放 Java 的 FFmpegFrameGrabber Frame

问题描述

1.我有来自 JAVA UDP 的原始数据

Log.d(TAG,Arrays.toString(temp2));//Length 951

[0, 0, 0, 1, 103, 77, 0, 40, -87, 80, 10, 0, -73, 66, 0, 0, 7, -48, 0, 1, -122, -96, 8, 0, 0, 0, 1, 104, -18, 60, -128, 0, 0, 0, 0, ...]

2.JMuxer可以播放h264数据

//html
  <video controls autoplay id="_play" style="width: 300px; height: 300px;>
  </video>

javascript

var nn1=[0,0,0,1,103,66,192,30,218,2,128,191,229,192,90,128,128,128,160,0,0,125,32,0,23,112,1,226,197,212,0,0,0,1,104,206,60,128,0,0,0,1,101,136,132,55,255,255,208,92,80,0,16,89,...];
//Length 26139
player = new JMuxer({
  node: '_play',
  mode: 'video',
  fps: 25,
  debug: true
});

setTimeout(function () {
   player.feed({
      video: new Uint8Array(nn1)
    });
}, 500);

3.问:我可以把FFmpegFrameGrabber Frame转成Javascript吗?

我努力了

  1. 帧传输到位图,
  2. 位图传输到 Base64
  3. Base64 返回 Javascript 的 Canvas ,但是这种方式太慢了;
//JAVA
...
    private PipedOutputStream outputStream = null;
    private PipedInputStream inputStream = null;
    private FFmpegFrameGrabber grabber = null;

 private void init() {
        if (this.grabber == null) {
            this.grabber = new FFmpegFrameGrabber(this.inputStream);
            this.grabber.setVideoCodec(27);
            this.grabber.setFrameRate(25.0D);
            this.grabber.setVideoBitrate(1200000);
            this.grabber.setImageWidth(1280);
            this.grabber.setImageHeight(720);
            this.grabber.setVideoCodecName("libx264");
            this.grabber.setOption("bufsize", "10000");
            this.grabber.setOption("filter:v", "fps=fps=25");
...

        }
    }


  public void run() {
        super.run();
        try {
            this.outputStream.write(new byte[1024]);
            this.outputStream.flush();
        } catch (IOException var6) {
            System.out.println(var6);
        }


        try {
            this.grabber.startUnsafe(false);
            try {
                Thread.sleep(50L);
            } catch (InterruptedException var5) {
            }

            Frame frame;

            while((frame = this.grabber.grabFrame(false,true,false,false,false)) != null){

              //Can I transfer frame to buffer array? //[0,0,0,1,103,66,192,30,...]  
              //I need a complete frame to play

            }

        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }

        System.out.println("FFmpeg stop && release");
    }




标签: javascriptjavacordova

解决方案


推荐阅读