Play a video without a file on disk [Java]

2019-07-17 23:13发布

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.

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

1条回答
叼着烟拽天下
2楼-- · 2019-07-17 23:54

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();

...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();
        }
    }
}

...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")

...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

查看更多
登录 后发表回答