복붙노트

[SPRING] html5 <video> 태그를 사용하여 탐색 할 수 있도록 Spring MVC로 비디오를 반환하려면 어떻게해야합니까?

SPRING

html5

웹 서버 (Tomcat)에 파일이 있고 태그를 만들면 동영상을 시청하고 일시 중지하고 탐색하고 완료된 후에 다시 시작할 수 있습니다.

그러나 요청시 비디오 파일을 전송하는 REST 인터페이스를 만들고 해당 URL을 태그에 추가하면 재생 및 일시 중지 만 할 수 있습니다. 되감기, 빨리 감기, 탐색하지 않음, 아무 것도 없습니다.

그렇다면이 문제를 해결할 수있는 방법이 있습니까? 어딘가에서 뭔가 놓친거야?

비디오 파일은 REST 인터페이스와 동일한 서버에 있으며 REST 인터페이스는 세션을 확인하고 전송할 비디오를 찾은 후에 비디오를 전송합니다.

이것들은 제가 지금까지 시도한 방법들입니다. 모두 작동하지만 어느 누구도 탐색 할 수 없습니다.

/*
 * This will actually load the whole video file in a byte array in memory,
 * so it's not recommended.
 */
@RequestMapping(value = "/{id}/preview", method = RequestMethod.GET)
@ResponseBody public ResponseEntity<byte[]> getPreview1(@PathVariable("id") String id, HttpServletResponse response) {
    ResponseEntity<byte[]> result = null;
    try {
        String path = repositoryService.findVideoLocationById(id);
        Path path = Paths.get(pathString);
        byte[] image = Files.readAllBytes(path);

        response.setStatus(HttpStatus.OK.value());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentLength(image.length);
        result = new ResponseEntity<byte[]>(image, headers, HttpStatus.OK);
    } catch (java.nio.file.NoSuchFileException e) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
    } catch (Exception e) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
    return result;
}
/*
 * IOUtils is available in Apache commons io
 */
@RequestMapping(value = "/{id}/preview2", method = RequestMethod.GET)
@ResponseBody public void getPreview2(@PathVariable("id") String id, HttpServletResponse response) {
    try {
        String path = repositoryService.findVideoLocationById(id);
        File file = new File(path)
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", "attachment; filename="+file.getName().replace(" ", "_"));
        InputStream iStream = new FileInputStream(file);
        IOUtils.copy(iStream, response.getOutputStream());
        response.flushBuffer();
    } catch (java.nio.file.NoSuchFileException e) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
    } catch (Exception e) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
}
@RequestMapping(value = "/{id}/preview3", method = RequestMethod.GET)
@ResponseBody public FileSystemResource getPreview3(@PathVariable("id") String id, HttpServletResponse response) {
    String path = repositoryService.findVideoLocationById(id);
    return new FileSystemResource(path);
}

해결법

  1. ==============================

    1.HTTP 재시작 다운로드 기능은 친구가 될 수 있습니다. 전에도 같은 문제가있었습니다. http 범위를 구현 한 후 비디오 탐색이 가능했습니다.

    HTTP 재시작 다운로드 기능은 친구가 될 수 있습니다. 전에도 같은 문제가있었습니다. http 범위를 구현 한 후 비디오 탐색이 가능했습니다.

    http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html

  2. ==============================

    2.비 정적 리소스 처리를위한 간단한 솔루션 :

    비 정적 리소스 처리를위한 간단한 솔루션 :

    @SpringBootApplication
    public class DemoApplication {
    
        private final static File MP4_FILE = new File("/home/ego/bbb_sunflower_1080p_60fps_normal.mp4");
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Controller
        final static class MyController {
    
            @Autowired
            private MyResourceHttpRequestHandler handler;
    
            // supports byte-range requests
            @GetMapping("/")
            public void home(
                    HttpServletRequest request,
                    HttpServletResponse response
            ) throws ServletException, IOException {
    
                request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE);
                handler.handleRequest(request, response);
            }
    
            // does not support byte-range requests
            @GetMapping(path = "/plain", produces = "video/mp4")
            public FileSystemResource plain() {
    
                return new FileSystemResource(MP4_FILE);
            }
        }
    
        @Component
        final static class MyResourceHttpRequestHandler extends ResourceHttpRequestHandler {
    
            private final static String ATTR_FILE = MyResourceHttpRequestHandler.class.getName() + ".file";
    
            @Override
            protected Resource getResource(HttpServletRequest request) throws IOException {
    
                final File file = (File) request.getAttribute(ATTR_FILE);
                return new FileSystemResource(file);
            }
        }
    }
    

    (스프링 부트 로그 파일 MvcEndpoint에서 영감을 얻었으며 Paul-Warrens (@ paul-warren) StoreByteRange HttpRequestHandler와 같음).

    Spring이 가까운 장래에 지원할 수 있기를 바랍니다. https://jira.spring.io/browse/SPR-13834 (투표하십시오).

  3. ==============================

    3.나는 이것이 오래된 게시물 인 것을 압니다.하지만 다른 사람들에게도 같은 질문이나 비슷한 질문을하는 것이 유용합니다.

    나는 이것이 오래된 게시물 인 것을 압니다.하지만 다른 사람들에게도 같은 질문이나 비슷한 질문을하는 것이 유용합니다.

    이제는 비디오 스트리밍을 기본적으로 지원하는 Spring Content와 같은 프로젝트가 있습니다. 가장 간단한 구현에 필요한 모든 코드는 다음과 같습니다.

    @StoreRestResource(path="videos")
    public interface VideoStore extends Store<String> {}
    

    그리고 이것으로 Java API 및 REST 엔드 포인트 세트를 작성하면 비디오 스트림을 PUT / POST, GET 및 DELETE 할 수 있습니다. GET은 바이트 범위를 지원하며 HTML5 동영상 플레이어 등에서 제대로 재생됩니다.

  4. ==============================

    4.사실,

    사실,

    특수 비디오 형식의 각 브라우저에는 기본 제어판이 있습니다.

    html과 css를 사용하여 미디어 API로 자신 만의 컨트롤을 만들 수 있습니다. 미디어 API

    Div에서 HTML5로 [기본적으로

  5. from https://stackoverflow.com/questions/20634603/how-do-i-return-a-video-with-spring-mvc-so-that-it-can-be-navigated-using-the-ht by cc-by-sa and MIT license