Chunking/splitting video file

Hello guys.

I am trying to split/chunk a video.mp4 file using the methods in Fs::File. i have tried almost all the methods not working. Currently am chunking and saving each chunked bytes but my main intension is to upload each to server.

The issue am having is that the first batch of chunk works fine but when i tried to chunk by offset position that is greater than zero the chunked file wont open.

One of the method i have tried is shown below

pub fn chunk_file(offset: u64) {
    let mut file = File::open("/Users/Downloads/production ID_3818213.mp4").unwrap();
    const SIZE: usize = 4 * 1024 * 1024;

    let mut buffer = [0u8; SIZE];

    file.read_exact_at(&mut buffer, offset);

    // I will upload each chunk later but for now just saving it
    let mut file_ = File::create("/Users/Downloads/production_1.mp4").unwrap();

When offset == 0 the saved chunked file can be open and played but when the offset > 0 the saved chunked file cant open.
Any suggestion on how to fix this or general suggestion on how to chunk video.mp4

.mp4 files aren't a raw video stream. They're a container file that presumably has some kind of header specifying what kind of streams are present, etc. — When you start your chunk later, you lose all of this header information so the player doesn't know what to make of the file.

You'll need to find some crate that understands the internal format of .mp4 files to make this work.

1 Like

Thanks for the information.
do you know any crate you recommend ?

Maybe you should try

1 Like

Thanks, i will try exploring them.

Mozilla's mp4 parse won't be sufficient. It only extracts some high-level data, and is not useful for working with the video data.

If you're chunking for something like HLS, you need to create properly structured video files. It's non-trivial. You'll need something like this, but extend it to split and write fragments accordingly:

1 Like


Hello @kornel what method could be used in the splitting of the video. I have been looking at it but not sure yet.

Thanks for your help