How to crop a Track according to a specific time

319 views Asked by At

I'm using mp4parser library and I already replace audio tracks from the video to a specific audio file. However the final Video keeps the duration of the biggest track(between Video and Music).

According to the mp4parser library it's possible to crop a Track using CroppedTrack whick I use like this

    for (Movie m : inMoviesSound) {

        double startTime = 0.000;
        double endTime = nSecondsVideo;

        boolean timeCorrected = false;


        for (Track track : m.getTracks()) {
            if (track.getSyncSamples() != null
                    && track.getSyncSamples().length > 0) {
                if (timeCorrected) {


                    throw new RuntimeException(
                            "The startTime has already been corrected by another track with SyncSample. Not Supported.");
                }
                startTime = correctTimeToNextSyncSample(track, startTime);
                endTime = correctTimeToNextSyncSample(track, endTime);
                timeCorrected = true;
            }
        }

        for (Track track : m.getTracks()) {
            long currentSample = 0;
            double currentTime = 0;
            long startSample = -1;
            long endSample = -1;

            for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) {
                TimeToSampleBox.Entry entry = track
                        .getDecodingTimeEntries().get(i);
                for (int j = 0; j < entry.getCount(); j++) {


                    if (currentTime <= startTime) {
                        // current sample is still before the new starttime
                        startSample = currentSample;
                    }
                    if (currentTime <= endTime) {
                        // current sample is after the new start time and
                        // still before the new endtime
                        endSample = currentSample;
                    } else {
                        // current sample is after the end of the cropped
                        // video
                        break;
                    }
                    currentTime += (double) entry.getDelta()
                            / (double) track.getTrackMetaData()
                                    .getTimescale();
                    currentSample++;
                }
            }
            result.addTrack(new CroppedTrack(track, startSample, endSample));
        }

    }

However the music stops a little bit earlier than the video.

Is there any way of being more specific to the end time?

0

There are 0 answers