0

I an creating an app witch merge videos and add mp3 audio overlay.

With merging videos i have no problems, but audio not playing in output video.

Here is my code:

 Movie[] inMovies = new Movie[count];
            for (int i = count - 1; i >= 0; i--) {

                File file = new File(lv1List.get(i));
                if (file.exists()) {
                    try {
                        inMovies[counter] = MovieCreator.build(file.getAbsolutePath());
                        counter++;
                    } catch (Exception e) {
                        Log.d("mp4parse", e.getMessage());
                    }
                }

            }
            List<Track> videoTracks = new LinkedList<Track>();
            List<Track> audioTracks = new LinkedList<Track>();
for (Movie m : inMovies) {
                    for (Track t : m.getTracks()) {

                        if (t.getHandler().equals("vide")) {
                            videoTracks.add(t);
                        }
                    }
                }
                try {
                    MP3TrackImpl aacTrack = new MP3TrackImpl(new FileDataSourceImpl(audiopath));
                    CroppedTrack aacTrackShort = new CroppedTrack(aacTrack, 1, aacTrack.getSamples().size());
                    audioTracks.add(aacTrackShort);
                } catch (Exception e) {
                    e.printStackTrace();
                }
 Movie result = new Movie();

            try {
                if (audioTracks.size() > 0) {
                    result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
                }
                if (videoTracks.size() > 0) {
                    result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                Container out = new DefaultMp4Builder().build(result);
                String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MERGEDoutput" + Long.toString(System.currentTimeMillis()) + ".mp4";
                FileOutputStream fos = new FileOutputStream(new File(filename));

                out.writeContainer(fos.getChannel());
                fos.close();
            } catch (Exception e) {

            }

Output video has not sound. Input mp3 is usual music track.

androidAnonDev
  • 459
  • 3
  • 5
  • 13

2 Answers2

1

If you are using mp4parser, don't use mp3 for your audio. Instead encode your audio as AAC and then run your muxer. MP4Parser does not like mp3 files :)

Machine Tribe
  • 871
  • 9
  • 13
0

I had faced the same issue.

The problem is possibly because your audio path isn't valid.

If you are getting the mp3 uri via ACTION_GET_CONTENT, make sure you are resolving the media store uri before using it. code getting path from mediastore

Community
  • 1
  • 1
Akhil Raj
  • 56
  • 2