javascriptnode.jsffmpeghtml5-videonode.js-fs

How to stream only a part of a video without downloading the whole thing?


I am building a YouTube video trimmer, and currently, it takes the YouTube video ID, downloads the whole video from the server (Even it is like 10 hours long), and then it trims it according to user's inputted timeframe using ffmpeg.

Now the problem with this is that it takes so much time to download the whole video even if we want a small piece of it hence it is highly impractical.

I was thinking to implement something like an HTML5 video player does when you seek the video forward. It just jumps to the part where you seek-ed to without downloading the part you skipped over. How can I only download a part of a video file from a server in form of a buffer and then generate an already trimmed video file from that? I don't know if that is even possible on the server-side, but video players do it on the client-side.


Solution

  • I know it has been 3 years but for anyone still wondering about this, it can be done using HTTP range requests. Here's a very helpful article: https://surma.dev/things/range-requests/ and https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests

    Basically when making the request to the server to get the video file, you define the exact range of bytes you want to get instead of the whole file. This is done using the Range header.