播放磁盘上没有文件的视频[Java] [英] Play a video without a file on disk [Java]

查看:107
本文介绍了播放磁盘上没有文件的视频[Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,其中我拥有AES 256位加密的视频块(块的原始格式是MP4)

I'm doing a project where I have AES 256bit encrypted chunks of video (Original format of chunks is MP4)

当用户选择开始日期和结束日期时他要观看的视频中,我必须解密相应的块并在视频播放器中播放它们。
问题是我无法将解密的文件存储在磁盘上,而只能将它们加载到内存中,例如,我只能将字节数组发送到视频播放器。

When a user select start date and end date of the video which he wants to see,I have to decrypt the corresponding chunks and play them in a video player. The problem is that I can't store decrypted files on disk, but only load them into memory,say, I can only send byte arrays to the videoplayer.

我想用Java实现这个项目,但是我不知道如何在没有物理文件的情况下将块流式传输到视频播放器。有任何想法吗?杂技演员?确实,是否可以使用Web应用程序,还是应该选择独立的应用程序?谢谢

I would like to implement this project in java, but I don't know how to stream chunks to the video player without having a physical file. Any ideas? Xuggler? Indeed, Is it possible to have a a web application or should I opt for a standalone application? Thanks

推荐答案

当前实现此目的的最佳方法是在Java FX中使用嵌入式HTTP服务器并使用HTTP Live Streaming(视频点播)。请参阅此链接以了解 HLS 。因此,只要您准备播放视频,就可以在创建Media对象之前...

The best way to achieve this currently, in Java FX is to use an embedded HTTP server and use HTTP Live Streaming (video on demand). See this link to learn about HLS. So, whenever you are ready to play the video, before you create the Media object...

// Creates a server on localhost, port 7777, runs on background thread
// Note that Media does not recognize localhost, you'll have to use 127.0.0.1
HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 7777), 0);
httpServer.createContext("/", new CustomHttpHandler("/dir/to/files/to/play"));
httpServer.start();

...在本地计算机上,在传递给CustomHttpHandler的目录中,您需要有一个.m3u8文件和要播放的文件。在最简单的情况下,要播放的文件应该是.ts文件,但是只要您在处理它们的请求时将它们转换为MPEG-2 TS格式,它们就可以是任何文件。让我们看一下CustomHttpHandler ...

...on your local machine, in the directory that you passed to the CustomHttpHandler, you need to have a .m3u8 file and the files for playback. For the simplest case, the files for playback should be .ts files, but they can be ANYTHING as long as you convert them to the MPEG-2 TS format when you handle their request. Let's look at the CustomHttpHandler...

public class CustomHttpHandler implements HttpHandler {
    private String rootDirectory;

    public CustomHttpHandler(String rootDirectory) {
        this.rootDirectory = rootDirectory;
    }

    @Override
    public void handle(HttpExchange httpExchange) throws IOException {
        URI uri = httpExchange.getRequestURI();
        File file = new File(rootDirectory + uri.getPath()).getCanonicalFile();

        Headers responseHeaders = httpExchange.getResponseHeaders();

        if (uri.toString().contains(".ts")) {
            responseHeaders.set("Content-Type", "video/MP2T");
        } else {
            responseHeaders.set("Content-Type", "application/vnd.apple.mpegurl");
        }

        if (file.exists()) {
            byte[] bytes = Files.readAllBytes(Paths.get(file.toURI()));
            httpExchange.sendResponseHeaders(200, 0);

            OutputStream outputStream = httpExchange.getResponseBody();
            outputStream.write(bytes);
            outputStream.close();
        }
    }
}

...请注意HttpHandler假设您要提供的文件已经是.ts格式,但是如果您还有其他内容(加密数据,压缩数据,MP4,RAW H264,二进制文件等),则只需将其放入.ts格式并将该数据写入上面的输出流。然后,在启动并运行该服务器后,您要做的就是创建媒体!

...notice that this HttpHandler is assuming the files you are going to serve are already in .ts format, but if you have something else (encrypted data, zipped data, MP4, RAW H264, binary, etc.), all you need to do is get it into the .ts format and write that data to the output stream above. Then all you do after you have this server up and running is create your Media!

// Note the 127.0.0.1 here, localhost will NOT work!
Media myMedia = new Media("http://127.0.0.1:7777/something.m3u8")

...就是这样!现在,您有了一个Java FX媒体播放器,该播放器可以从任何地方加载并支持完整的播放功能(快进,慢动作,搜索等)。 d(-_-)b

...and that's it! Now you have a Java FX media player, that can load from anywhere and supports full playback capabilities (fast forward, slow motion, seek, etc.). d(-_-)b

这篇关于播放磁盘上没有文件的视频[Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆