20

有谁知道如何在 Java 环境中解码 H.264 视频帧?

我的网络摄像机产品支持 RTP/RTSP 流媒体。

我的网络摄像机提供服务标准 RTP/RTSP,它还支持“RTP/RTSP over HTTP”。

RTSP:TCP 554 RTP 起始端口:UDP 5000

4

5 回答 5

6

或使用Xuggler。适用于 RTP、RTMP、HTTP 或其他协议,可以对 H264 和大多数其他编解码器进行解码和编码。并且得到积极维护、免费和开源 (LGPL)。

于 2010-02-03T04:21:04.467 回答
6

我发现了一个基于JavaCV 的 FFmpegFrameGrabber 类的非常简单直接的解决方案。该库允许您通过将 ffmpeg 包装在 Java 中来播放流媒体。

如何使用它?

首先,您可以使用 Maven 或 Gradle 下载并安装该库。

在这里,您有一个StreamingClient类调用SimplePlayer具有 Thread 播放视频的类。

public class StreamingClient extends Application implements GrabberListener
{
    public static void main(String[] args)
    {
        launch(args);
    }

    private Stage primaryStage;
    private ImageView imageView;

    private SimplePlayer simplePlayer;

    @Override
    public void start(Stage stage) throws Exception
    {
        String source = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"; // the video is weird for 1 minute then becomes stable

        primaryStage = stage;
        imageView = new ImageView();

        StackPane root = new StackPane();

        root.getChildren().add(imageView);
        imageView.fitWidthProperty().bind(primaryStage.widthProperty());
        imageView.fitHeightProperty().bind(primaryStage.heightProperty());

        Scene scene = new Scene(root, 640, 480);

        primaryStage.setTitle("Streaming Player");
        primaryStage.setScene(scene);
        primaryStage.show();

        simplePlayer = new SimplePlayer(source, this);
    }

    @Override
    public void onMediaGrabbed(int width, int height)
    {
        primaryStage.setWidth(width);
        primaryStage.setHeight(height);
    }

    @Override
    public void onImageProcessed(Image image)
    {
        LogHelper.e(TAG, "image: " + image);

        Platform.runLater(() -> {
            imageView.setImage(image);
        });
    }

    @Override
    public void onPlaying() {}

    @Override
    public void onGainControl(FloatControl gainControl) {}

    @Override
    public void stop() throws Exception
    {
        simplePlayer.stop();
    }
}

SimplePlayer类用于FFmpegFrameGrabber解码frame转换为图像并显示在舞台中的

public class SimplePlayer
{
    private static volatile Thread playThread;
    private AnimationTimer timer;

    private SourceDataLine soundLine;

    private int counter;

    public SimplePlayer(String source, GrabberListener grabberListener)
    {
        if (grabberListener == null) return;
        if (source.isEmpty()) return;

        counter = 0;

        playThread = new Thread(() -> {
            try {
                FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(source);
                grabber.start();

                grabberListener.onMediaGrabbed(grabber.getImageWidth(), grabber.getImageHeight());

                if (grabber.getSampleRate() > 0 && grabber.getAudioChannels() > 0) {
                    AudioFormat audioFormat = new AudioFormat(grabber.getSampleRate(), 16, grabber.getAudioChannels(), true, true);

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

                Java2DFrameConverter converter = new Java2DFrameConverter();

                while (!Thread.interrupted()) {
                    Frame frame = grabber.grab();
                    if (frame == null) {
                        break;
                    }
                    if (frame.image != null) {

                        Image image = SwingFXUtils.toFXImage(converter.convert(frame), null);
                        Platform.runLater(() -> {
                            grabberListener.onImageProcessed(image);
                        });
                    } else if (frame.samples != null) {
                        ShortBuffer channelSamplesFloatBuffer = (ShortBuffer) frame.samples[0];
                        channelSamplesFloatBuffer.rewind();

                        ByteBuffer outBuffer = ByteBuffer.allocate(channelSamplesFloatBuffer.capacity() * 2);

                        for (int i = 0; i < channelSamplesFloatBuffer.capacity(); i++) {
                            short val = channelSamplesFloatBuffer.get(i);
                            outBuffer.putShort(val);
                        }
                    }
                }
                grabber.stop();
                grabber.release();
                Platform.exit();
            } catch (Exception exception) {
                System.exit(1);
            }
        });
        playThread.start();
    }

    public void stop()
    {
        playThread.interrupt();
    }
}
于 2018-03-15T08:22:11.847 回答
4

我认为最好的解决方案是使用“JNI + ffmpeg”。在我目前的项目中,我需要在一个基于 libgdx 的 java openGL 游戏中同时播放多个全屏视频。我已经尝试了几乎所有的免费库,但没有一个具有可接受的性能。所以最后我决定编写自己的 jni C 代码来使用 ffmpeg。这是我笔记本电脑上的最终表现:

  • 环境:CPU:Core i7 Q740 @1.73G,视频:nVidia GeForce GT 435M,操作系统:Windows 7 64bit,Java:Java7u60 64bit
  • 视频:h264rgb / h264 编码,无声音,分辨率:1366 * 768
  • 解决方案:解码:JNI + ffmpeg v2.2.2,上传到GPU:使用lwjgl更新openGL纹理
  • 性能:解码速度: 700-800FPS,纹理上传:每帧约1ms

我只花了几天时间就完成了第一个版本。但是第一版的解码速度只有120FPS左右,上传时间大约是每帧5ms。经过几个月的优化,我得到了这个最终性能和一些附加功能。现在我可以同时播放多个高清视频而不会出现任何缓慢。

我游戏中的大多数视频都有透明背景。这种透明视频是一个mp4文件,有2个视频流,一个流存储h264rgb编码的rgb数据,另一个流存储h264编码的alpha数据。所以要播放 alpha 视频,我需要解码 2 个视频流并将它们合并在一起,然后上传到 GPU。因此,我可以在我的游戏中同时在不透明高清视频上方播放多个透明高清视频。

于 2014-06-13T20:00:16.950 回答
4

您可以使用名为 JCodec ( http://jcodec.org ) 的纯 Java 库。
解码一个 H.264 帧很简单:

ByteBuffer bb = ... // Your frame data is stored in this buffer
H264Decoder decoder = new H264Decoder();
Picture out = Picture.create(1920, 1088, ColorSpace.YUV_420); // Allocate output frame of max size
Picture real = decoder.decodeFrame(bb, out.getData());
BufferedImage bi = JCodecUtil.toBufferedImage(real); // If you prefere AWT image

如果你想从一个容器(比如 MP4)中读取一个,你可以使用一个方便的辅助类 FrameGrab:

int frameNumber = 150;
BufferedImage frame = FrameGrab.getFrame(new File("filename.mp4"), frameNumber);
ImageIO.write(frame, "png", new File("frame_150.png"));

最后,这是一个完整的复杂示例:

private static void avc2png(String in, String out) throws IOException {
    SeekableByteChannel sink = null;
    SeekableByteChannel source = null;
    try {
        source = readableFileChannel(in);
        sink = writableFileChannel(out);

        MP4Demuxer demux = new MP4Demuxer(source);

        H264Decoder decoder = new H264Decoder();

        Transform transform = new Yuv420pToRgb(0, 0);

        MP4DemuxerTrack inTrack = demux.getVideoTrack();

        VideoSampleEntry ine = (VideoSampleEntry) inTrack.getSampleEntries()[0];
        Picture target1 = Picture.create((ine.getWidth() + 15) & ~0xf, (ine.getHeight() + 15) & ~0xf,
                ColorSpace.YUV420);
        Picture rgb = Picture.create(ine.getWidth(), ine.getHeight(), ColorSpace.RGB);
        ByteBuffer _out = ByteBuffer.allocate(ine.getWidth() * ine.getHeight() * 6);
        BufferedImage bi = new BufferedImage(ine.getWidth(), ine.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        AvcCBox avcC = Box.as(AvcCBox.class, Box.findFirst(ine, LeafBox.class, "avcC"));

        decoder.addSps(avcC.getSpsList());
        decoder.addPps(avcC.getPpsList());

        Packet inFrame;
        int totalFrames = (int) inTrack.getFrameCount();
        for (int i = 0; (inFrame = inTrack.getFrames(1)) != null; i++) {
            ByteBuffer data = inFrame.getData();

            Picture dec = decoder.decodeFrame(splitMOVPacket(data, avcC), target1.getData());
            transform.transform(dec, rgb);
            _out.clear();

            AWTUtil.toBufferedImage(rgb, bi);
            ImageIO.write(bi, "png", new File(format(out, i)));
            if (i % 100 == 0)
                System.out.println((i * 100 / totalFrames) + "%");
        }
    } finally {
        if (sink != null)
            sink.close();
        if (source != null)
            source.close();
    }
}
于 2013-05-13T03:35:27.433 回答
0

Take a look at the Java Media Framework (JMF) - http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/formats.html

I used it a while back and it was a bit immature, but they may have beefed it up since then.

于 2010-01-29T22:29:32.373 回答