Muxing MP4 and AAC (jcodec, MP4Parser)

时间:2015-09-01 22:58:19

标签: java android encoding mp4parser jcodec

I have an MP4 file that I generate with jcodec.

I then have an AAC file I generate with Android's MediaCodec.

I want to mux them together into a single file and since I don't want to limit my Android API too high, I'm opting for MP4Parser.


Here's my current code:

H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("input.mp4"));
AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl("input.aac"));

Movie movie = new Movie();
movie.addTrack(h264Track);
movie.addTrack(aacTrack);

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

FileOutputStream fileOutputStream = new FileOutputStream(new File("output.mp4"));
FileChannel fc = fileOutputStream.getChannel();
mp4file.writeContainer(fc);
fileOutputStream.close();

The code crashes at the line:

H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("input.mp4"));

With this as the stack trace:

09-01 18:46:40.611: E/AndroidRuntime(14115): FATAL EXCEPTION: GLThread 1260
09-01 18:46:40.611: E/AndroidRuntime(14115): Process: package.app, PID: 14115
09-01 18:46:40.611: E/AndroidRuntime(14115): java.lang.RuntimeException: Sequence parameter set extension is not yet handled. Needs TLC.
09-01 18:46:40.611: E/AndroidRuntime(14115):    at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.readSamples(H264TrackImpl.java:362)
09-01 18:46:40.611: E/AndroidRuntime(14115):    at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.parse(H264TrackImpl.java:108)
09-01 18:46:40.611: E/AndroidRuntime(14115):    at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.<init>(H264TrackImpl.java:90)
09-01 18:46:40.611: E/AndroidRuntime(14115):    at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.<init>(H264TrackImpl.java:96)
09-01 18:46:40.611: E/AndroidRuntime(14115):    at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.<init>(H264TrackImpl.java:100)
09-01 18:46:40.611: E/AndroidRuntime(14115):    at ...

(The crash occurs in this function at this line.)


So I'm not really sure what that means? Does jcodec output an MP4 file (which works in all video players btw) that isn't compatible with MP4Parser?

What should I be looking into to fix this?

Appreciate any advice.


Edit

Was going about it the wrong way, this worked

Movie movie = MovieCreator.build(_videoFile.getAbsolutePath());

AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(_aacFile));
CroppedTrack aacCroppedTrack = new CroppedTrack(aacTrack, 1, aacTrack.getSamples().size());
movie.addTrack(aacCroppedTrack);

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

FileOutputStream fileOutputStream = new FileOutputStream(_outputFile);
FileChannel fc = fileOutputStream.getChannel();
mp4file.writeContainer(fc);
fileOutputStream.close();

1 个答案:

答案 0 :(得分:0)

这对我有用....

        DataSource videoFile = new FileDataSourceImpl(filePath);

        Movie sor = MovieCreator.build(videoFile);

        List<Track> videoTracks = sor.getTracks();
        Track h264Track = null;

        for (Track t : videoTracks
             ) {
            if(t.getHandler().contains("vid"))
            {
                h264Track = t;