Comments (6)
@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.
@ltpquang @chad187
Found the correctTimeToSyncSample method with description here -
https://stackoverflow.com/questions/26841622/trimming-video-with-the-help-of-mp4parser
from mp4parser.
@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.
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.
clean up
from mp4parser.
@pajama nice find, thank you so much, gonna give it a try
from mp4parser.
Related Issues (20)
- On the fly HOT 1
- Class Not Found Exception : org,mp4parser.boxes.iso.14496.part12.FileTypeBox.
- Java mp4parser to create a single .m4s fragment (without malformation)
- Write MP4 file from RTSP stream
- MovieCreator.build(file) NullPointerException
- mux bytebuffer audio and video
- VP9 support for mp4parser
- Will your library support OpenHarmony platform by JavaScript language ? HOT 1
- Cant Trim Videos
- How to release resources HOT 1
- java.lang.OutOfMemoryError: Direct buffer memory
- NullPointerException when trying on Android corrupt video file
- Latest Git Tag not pushed
- Can this be used to add timed metadata?
- Can you provide a guide on how to install?
- Why are so many tags updated on Apr 26, 2022?
- Android SDK mp4parser issue while implementation to other project because of dependency
- convert video in 3 seconds segments
- Adding HEIF Support
- Buffer size should be checked when parsing font name field of QuicktimeText block
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mp4parser.