javamp4parser

mp4parse timelapse frame rate


I'm trying to ingest a mp4 file and make it a timelapse. It works with the code attached below. However, the output file has frame rate of 16*originalFrameRate. Since I don't intend to play it as a slow motion video I'd prefer to drop those redundant frames to make the output file smaller.

Movie inputMovie = MovieCreator.build(fileUri);

List<Track> videoTracks = new LinkedList<>();

for (Track track : inputMovie.getTracks()) {
    if (track.getHandler().equals("vide")) {
        videoTracks.add(track);
    }
}

final int speedByFactorOf = 16;

Movie outputMovie = new Movie();

AppendTrack appendedTracks = new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()]));
outputMovie.addTrack(new WrappingTrack(appendedTracks) {
    @Override
    public long[] getSampleDurations() {
        long[] l = super.getSampleDurations();
        for (int i = 0; i < l.length; i++) {
            l[i] /= speedByFactorOf;
        }
        return l;
    }
});

BasicContainer out = (BasicContainer) new DefaultMp4Builder().build(outputMovie);

FileChannel fc = new RandomAccessFile("timelapse.mp4", "rw").getChannel();
out.writeContainer(fc);
fc.close();
out.close();

I was unable to find any examples of how to change the output frame rate.


Solution

  • As stated by @tarun-lalwani if the project you're referring to is https://github.com/sannies/mp4parser then it is only able to edit the MP4 container and NOT the video / audio / media etc. held within the container. Even if you could use metadata to accelerate the FPS by sixteen times, the file size would not become any smaller because all the frames would still be within the file (just shown for a shorter duration). You would need to use something like FFmpeg (e.g. via https://github.com/bramp/ffmpeg-cli-wrapper ) or some other programmatic video editor to do what you're describing thus only keeping every sixteenth frame of video so the video file actually becomes smaller.

    TLDR; mp4parser is not the correct project for editing video (as opposed to metadata) and what you want to achieve sounds like it is beyond the scope of just fiddling with the container.