Загрузите видео с помощью тега html <video>с помощью Spring - PullRequest
1 голос
/ 25 февраля 2020

Я пытаюсь разработать приложение. Я получаю фрагменты видео в виде байтового массива из очереди и отправляю их в браузер без сохранения в виде файла. Однако мой код не работает. Может кто-нибудь дать несколько советов о том, как этого добиться, пожалуйста. Заранее спасибо.

@GetMapping("/stream")
public void lasting(HttpServletRequest request,HttpServletResponse response) throws IOException {
        OutputStream output=response.getOutputStream();
        response.setContentType("video/mp4");
        String range = request.getHeader("Range");
        File file=new File("temp.mp4");

        byte[] arr= Files.readAllBytes(Paths.get(file.getAbsolutePath()));
        int size=arr.length;
        if(range!=null) {
            String[] parts=range.replace("bytes=", "").split("-");
            long start=Long.parseLong(parts[0].trim());
            long end;
            if(parts.length>1) {
                end=Long.parseLong(parts[1].trim());
            }
            else {
                end=size-1;

            }
            long chunksize=end-start+1;
            output.write(( "Content-type: video/mp4\r\n" +
                    "Accept-Ranges: bytes \r\n"+
                    "Content-Range: bytes "+start +"-"+end+"/"+arr.length+"\r\n"+
                    "Content-Length: " +
                    chunksize+
                    "\r\n\r\n").getBytes());
            output.flush();
            long a=end;
            long b=start;

            byte[] d=Arrays.copyOfRange(arr, (int)start,(int) end+1);


            output.write(d);


            output.write("\r\n\r\n".getBytes());
            output.flush();



        }
        else {
            output.write(( "Content-type: video/mp4\r\n" +
                    "Content-Length: " +arr.length+
                    "\r\n\r\n").getBytes());


            output.write(arr);
            output.write("\r\n\r\n".getBytes());
            output.flush();

        }
    }

Также мой html код здесь

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Consumer's stream</title>
</head>
<body>
    <video id="/video" src="http://locallhost:9999/stream" type="video/mp4"></video>
</body>
</html>
...