Giter Club home page Giter Club logo

Comments (6)

ltpquang avatar ltpquang commented on August 21, 2024 2

@damienoliver sorry for digging this up, but I cant understand what does correctTimeToSyncSample method do? Could you give me some sort explanations, and how did you implement it?

I took some examinations and relized that with audio (AAC in my cases), the audioTrack.getSyncSamples() always returns null, therefore the time correction process was never be executed. Or am I doing it wrong?

from mp4parser.

pajama avatar pajama commented on August 21, 2024 1

@ltpquang @chad187
Found the correctTimeToSyncSample method with description here -
https://stackoverflow.com/questions/26841622/trimming-video-with-the-help-of-mp4parser

from mp4parser.

pajama avatar pajama commented on August 21, 2024 1

@ltpquang good luck! The method that worked for me was actually in his sample project, at the bottom of this class.
https://gitlab.com/lassana/mp4parser-test/blob/master/src/com/github/lassana/mp4parser_test/Mp4Cutter2.java

from mp4parser.

damienoliver avatar damienoliver commented on August 21, 2024

Whoops, ignore that. I just realised I was measuring samples on the video track not audio track.

Here is the working code if anyone else is trying to do this.

Thanks!

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath();
String videoPath = path + "/" + "sas-video.mp4";

Movie orig_movie = MovieCreator.build(videoPath);

String filename = "finalaudiolong.aac";
File audioFile = new File(path, filename);

AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(audioFile));

//get duration of video
IsoFile isoFile = new IsoFile(videoPath);
double lengthInSeconds = (double)
        isoFile.getMovieBox().getMovieHeaderBox().getDuration() /
        isoFile.getMovieBox().getMovieHeaderBox().getTimescale();


Track track = (Track) orig_movie.getTracks().get(0);

Track audioTrack = (Track) aacTrack;


double startTime1 = 0;
double endTime1 = lengthInSeconds;

boolean timeCorrected = false;

if (audioTrack.getSyncSamples() != null && audioTrack.getSyncSamples().length > 0) {
    if (timeCorrected) {

        throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");
    }
    startTime1 = correctTimeToSyncSample(audioTrack, startTime1, false);
    endTime1 = correctTimeToSyncSample(audioTrack, endTime1, true);
    timeCorrected = true;
}

long currentSample = 0;
double currentTime = 0;
double lastTime = -1;
long startSample1 = -1;
long endSample1 = -1;


for (int i = 0; i < audioTrack.getSampleDurations().length; i++) {
    long delta = audioTrack.getSampleDurations()[i];


    if (currentTime > lastTime && currentTime <= startTime1) {
        // current sample is still before the new starttime
        startSample1 = currentSample;
    }
    if (currentTime > lastTime && currentTime <= endTime1) {
        // current sample is after the new start time and still before the new endtime
        endSample1 = currentSample;
    }

    lastTime = currentTime;
    currentTime += (double) delta / (double) audioTrack.getTrackMetaData().getTimescale();
    currentSample++;
}

CroppedTrack cropperAacTrack = new CroppedTrack(aacTrack, startSample1, endSample1);

Movie movie = new Movie();

movie.addTrack(track);
movie.addTrack(cropperAacTrack);

Container mp4file = new DefaultMp4Builder().build(movie);

FileChannel fc = new FileOutputStream(new File(path,"sas-video-with-audio.mp4")).getChannel();
mp4file.writeContainer(fc);
fc.close();

from mp4parser.

sannies avatar sannies commented on August 21, 2024

clean up

from mp4parser.

ltpquang avatar ltpquang commented on August 21, 2024

@pajama nice find, thank you so much, gonna give it a try

from mp4parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.