Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In
Continue with Google
Continue with Facebook
or use


Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here
Continue with Google
Continue with Facebook
or use


Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Continue with Google
Continue with Facebook
or use


Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Stack Ask

Stack Ask Logo Stack Ask Logo

Stack Ask Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5721
Next
In Process
Hihi
  • 1
HihiTeacher
Asked: May 5, 20222022-05-05T14:27:26+00:00 2022-05-05T14:27:26+00:00In: Spring

Expose API trả về kiểu Video (giống như Tiktok,..)

  • 1

Hi mọi người,

Cho em hỏi là ở app Back End (em dùng Java Spring) thông thường theo em hiểu là khi expose 1 API để trả về video cho client sử dụng thì quá trình sẽ là :

  1. Chuyển file video thành mảng byte (byte [])
  2. Set mảng array vào body của Response
  3. Set header của Response Content-type = video/mp4.

Em nghĩ đây là cách cơ bản nhất.Nhưng em có search thì em thấy để trả về 1 API video thì cần phải có Response có header liên quan đến Range của video nữa và hình như Spring Webflux có hỗ trợ việc này. Link em xem ạ https://www.youtube.com/watch?v=_PEPaWFs064

  1. Mọi người cho em hỏi là Range này là gì và nó có tác dụng của nó đối  với viêc API trả về video ạ ? (Em nghĩ chắc chắn nó sẽ có ưu điểm gì thì người ta mới dùng nó )?Em thấy nó call API liên tục khi video đang chạy thì có gây sập server ko ạ ?
  2. Và cho em xin lời khuyên để làm BackEnd trả về API Video như này thì dùng Spring Boot bình thường hay dùng Spring WebFlux sẽ tốt hơn ạ ?

Cảm ơn mọi người!

spring
  • 7 7 Answers
  • 113 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

7 Answers

  • Voted
  • Oldest
  • Recent
  1. tvd12 Enlightened
    2022-05-05T15:14:25+00:00Added an answer on May 5, 2022 at 3:14 pm

    1. Nếu anh không nhầm thì cái Range này để lấy từng phần của video, bởi vì khi video mà lớn thì nó sẽ tạo ra 1 request rất dài, mà 1 request http sẽ thường có 1 khoảng thời gian timeout, như jetty đang để timeout mặc định đâu đó 60 giây như vậy nếu tốc độ tải chậm nó sẽ gây ra tình trạng video bị interrupt, vậy cần chia nhỏ việc tải file để đảm bảo khả năng download liên tục.

    2. Spring WebFlux có vẻ hỗ trợ cho lập trình reactive nhỉ? Vậy thì có vẻ nó không có tác dụng lắm đối với việc tải file này, vì reactive thích hợp cho việc tổng hợp data từ nhiều luồng, đây em chỉ có 1 luồng đọc file và ghi vào output stream mà thôi. Nên em có thể dùng cái gì cũng được, anh nghĩ là như nhau nên em chọn cái gì đơn giản thì làm.

    3. Trên thực tế thì ngày nay việc stream video thường dùng các giao thức kiểu hls. Các file media sẽ được cắt thành nhiều phần nhỏ từ đó mà việc đọc file cũng dễ dàng và nhanh chóng hơn.

    • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
    • Hihi Teacher
      2022-05-06T03:58:06+00:00Replied to answer on May 6, 2022 at 3:58 am

      Anh ơi ! Em có 1 thắc mắc ,em xin phép hỏi anh là khi trả về 1 API Video thì body của response ngoài byte array (1) thì còn là các đối tượng ByteArrayResource,InputStreamResource (2).Sự khác nhau của (1) và (2) cụ thể là như thế nào ạ ? Em có search google rồi mà vẫn ko hiểu ạ.

      • 0
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report
  2. monkey Enlightened
    2022-05-06T04:06:41+00:00Added an answer on May 6, 2022 at 4:06 am

    1. ByteArrayResource là 1 lớp wrap lại 1 mảng byte. Nó thích hợp trong trường hợp em có 1 mảng byte và em cần truyền mảng byte này cho client. Đối với trường hợp download file thì em cũng sẽ không hay dùng cái này, vì nếu đọc cả 1 file rồi biến nó thành byte array rồi mới truyền xuống client nó sẽ gây ra tốn RAM hoặc hết RAM.

    2. InputStreamResource là 1 lớp wrap lại 1 inputstream. Nó rất thích hợp trong việc em download file. Nó sẽ không load toàn bộ file lên bộ nhớ, nó sẽ đọc dần dần ví dụ 1024 bytes 1 lần, như vậy nó sẽ giúp em tiết kiệm được RAM và bảo vệ an toàn cho hệ thống.

    • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
    • Hihi Teacher
      2022-05-06T05:01:04+00:00Replied to answer on May 6, 2022 at 5:01 am

      Vậy từ câu trả lời của anh theo em hiểu là cách tốt nhất trong trường hợp dowload file cũng như trả về video cho cilent hiển thị (như tik tok) thì nên dùng InputStreamResource thay vì trả về thẳng mảng byte hay ByteArrayResource có đúng ko a ?

      • 0
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report
      • monkey Enlightened
        2022-05-06T06:23:30+00:00Replied to answer on May 6, 2022 at 6:23 am

        Đúng rồi em ạ.

        • 0
        • Share
          Share
          • Share on Facebook
          • Share on Twitter
          • Share on LinkedIn
          • Share on WhatsApp
          • Report
  3. Thành Lê Văn Beginner
    2022-05-07T16:24:17+00:00Added an answer on May 7, 2022 at 4:24 pm

    Mình nhớ Range  là client gửi lên cho server chứ ?

    Để giải thích điều này thì nên quay lại lịch sử xem video thời xưa với các định dạng video 1 file duy nhất. (Mp4…)

    Thời xưa ví dụ chúng ta đang xem video có tổng dung lượng là 10 phút.
    Ví dụ chúng ta đang xem đến phút thứ 1:02 và chúng ta muốn tua nhanh đến phút thứ 9.

    Trong trường hợp xấu nhất, client chưa tải hết byte của video hoặc chưa tải được buffer byte đến phút thứ 9. Client sẽ cần chờ 1 khoản thời gian để chờ tải các byte đến phút thứ 9 sau đó mới tiếp túc xem video từ phút thứ 9 được. (Thật tốn thời gian)

    Về sau thì người ta đã nghĩ ra ý tưởng hay hơn.

    Ví dụ tổng video 10 phút của chúng ta có 10_000 byte.

    Khi chúng ta muốn tua nhanh và xem đến phút thứ 9 để xem. Client sẽ tính toán phút thứ 9 thì sẽ ở khoảng byte thứ bao nhiêu sau đó gửi một request mới đến server với key ở Header là Range và value là  vị trí của byte.

    Với ví dụ trên chúng ta sẽ gửi 1 request mới với Range có value là 9_000. Server sau đó sẽ trả về các byte tiếp theo bắt đầu từ 9_000 đến 10_000.

    Hiện tại thì các website video sử dụng HLS khá nhiều, và đa số các website video lớn trên thế giới đang chuyển dần đến hết sử dụng HLS ( HTTP Live Streaming ) bởi các ưu điểm của nó rất tốt cho các ứng dụng video đa quốc gia, Line Streaming …etc…

    • 0
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
    • Hihi Teacher
      2022-05-18T10:00:11+00:00Replied to answer on May 18, 2022 at 10:00 am

      Cảm ơn anh đã chia sẻ !

      • 0
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

You must login to add an answer.

Continue with Google
Continue with Facebook
or use


Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 554
  • Answers 1k
  • Best Answers 63
  • Users 291
  • Popular
  • Answers
  • monkey

    [Deep Learning] Làm thế nào để xác định được cái ...

    • 16 Answers
  • Tú Trần Anh

    [Ezyfox Server] Unity game client không gửi được command khi ...

    • 12 Answers
  • Thưởng Đặng Văn

    Multiple tenant multiple database .net core

    • 10 Answers
  • monkey
    monkey added an answer 1. Em có thể lưu dữ liệu tạm ở Local… May 25, 2022 at 4:52 am
  • Hoàng Kiếm
    Hoàng Kiếm added an answer Huy ơi bạn đã thử login bằng command, mysql -u… May 21, 2022 at 5:42 am
  • tvd12
    tvd12 added an answer public class EzyEventLoopGroup { private final EzyRoundRobin eventLoops; private final… May 19, 2022 at 2:20 pm

Related Questions

  • monkey

    Hướng dấn sử dụng Spring Boot Test controller

    • 0 Answers
  • ptitdev

    Nhiều instance service websocket ?

    • 2 Answers
  • Nguyễn Hữu Cường

    Có phải xóa config stateless khi sử dụng web socket ...

    • 1 Answer

Top Members

tvd12

tvd12

  • 63 Questions
  • 1k Points
Enlightened
monkey

monkey

  • 88 Questions
  • 588 Points
Enlightened
Nguyễn Thái Sơn

Nguyễn Thái Sơn

  • 140 Questions
  • 220 Points
Professional

Trending Tags

.net core abstract class analytics android ansible anti-flooding apache poi api async asyncawait atomicboolean backend backend nestjs bash script batch bean big project binding bitcoin blockchain blog british buffered build bundle c# cache caching callback career career path cast centos chat cloud cloud reliability commit company content-disposition contract cors cosmos css database datasource datastructure decentralized exchange deep learning design-pattern devops dex distraction programing docker download draw.io du học dữ liệu lớn eclip editor employee english erc20 erc721 eth ethereum ethereum login extensions exyfox ezyfox ezyfox-boot ezyfox-server ezyfoxserver ezyhttp facebook fe flutter freetank french front-end frontend fullstack fulltextsearch future game game-box game-room game-server get git go golang google grapql grpc guide hazelcast hibernate hibernateconfig html http https indexing intellij interface interview io ipfs isolate issue it java javacore java core javascript java spring javaw java web job jpa js json jsp & servlet jvm jwt kafka keep promise kerberos keycloak kotlin language languague library load-balancing log log4j log4j-core login lưu trữ machine learning macos math maven merge messaging metamask microservice model msgpack multiple tenant multithread multithreading mysql n naming naming convention netcore netty nft nft game nio nodejs non-blocking io opensource optimize orm pagination pancakeswap panic pgpool phỏng vấn pointer postgresql pre programming promise push message android python python unicode question queue rabbitmq reactive reactjs reactjs download recyclerview redis request resource rest roadmap ropssten ropsten rust rxjava schedule search security server shift jis singleton sjis slack smart contract socket soft delete sosanh spring spring-boot-test spring-jpa spring aop spring boot springboot spring data jpa spring redis spring security springsecurity sql sql server sse stackask storage stream structure trong spring boot system environment variables thread threadjava thymeleaf totp tracking location unit test unity upload file vector view volatile vue vue cli web3 web3 client webpack websocket windows 11 winforms work zookeeper

Footer

Stack Ask

Stack Ask is where Developers Ask & Answers. It will help you resolve any questions and connect you with other people.

About Us

  • Meet The Team
  • About Us
  • Contact Us

Legal Stuff

  • Terms of Service
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2021 Stack Ask. All Rights Reserved
Powered by youngmonkeys.org