微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有 FFmpeg 和 JavaFx 的 JavaCV 视频播放器

如何解决带有 FFmpeg 和 JavaFx 的 JavaCV 视频播放器

我想使用 Java CV 创建一个媒体播放器,但我无法调整帧速率 正因为如此,有些视频播放速度快,有些播放慢

是否可以通过一个FFmpegFrameGrraber对象读取一个视频的信息并将其设置在另一个对象中来解决问题? 当然,我这样做并没有得到答案,但如果有其他方法,请帮助

playThread = new Thread(new Runnable() {
            public void run() {
                try {
                    final String videoFilename = "E:\\Java\\s1\\3.mp4";
                    final String videoFilename2 = "E:AudioVideo.mp4";
                    final String videoFilename3 = "E:\\1.mp4";
                    final String videoFilename4 = "E:\\Java\\s1\\3.mp4";


                     FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFilename);



                    grabber.start();

                    grabber.setFrameRate(30.00);


                    primaryStage.setWidth(grabber.getimageWidth());
                    primaryStage.setHeight(grabber.getimageHeight());

                    final AudioFormat audioFormat = new AudioFormat(grabber.getSampleRate(),16,grabber.getAudioChannels(),true,true);

                    final DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat);
                    final SourceDataLine soundLine = (SourceDataLine) AudioSystem.getLine(info);
                    soundLine.open(audioFormat);
                    soundLine.start();

                    JavaFXFrameConverter converter = new JavaFXFrameConverter();

                    ExecutorService executor = Executors.newSingleThreadExecutor();

                    while (!Thread.interrupted()) {
                        Frame frame = grabber.grab();
                        if (frame == null) {
                            break;
                        }
                        if (frame.image != null) {
                            final Image image = converter.convert(frame);//
                            Platform.runLater(() -> {
                                
                                imageView.setimage(image);

                            });
                        }
                         else if (frame.samples != null) {
                            final ShortBuffer channelSamplesShortBuffer = (ShortBuffer) frame.samples[0];
                            channelSamplesShortBuffer.rewind();

                            final ByteBuffer outBuffer = ByteBuffer.allocate(channelSamplesShortBuffer.capacity() * 2);

                            for (int i = 0; i < channelSamplesShortBuffer.capacity(); i++) {
                                short val = channelSamplesShortBuffer.get(i);
                                outBuffer.putShort(val);
                            }

                            try {



                                executor.execute(() -> {
                                    soundLine.write(outBuffer.array(),outBuffer.capacity());
                                    outBuffer.clear();
                                });
                            } catch (Exception interruptedException) {
                                Thread.currentThread().interrupt();
                            }
                        }

                    }
                    executor.shutdownNow();
                    executor.awaitTermination(10,TimeUnit.SECONDS);

                    soundLine.stop();
                    grabber.stop();
                    grabber.release();
                    Platform.exit();
                } catch (Exception exception) {
                    LOG.log(Level.SEVERE,null,exception);
                    System.exit(1);
                }
            }
        });
        playThread.start();
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。