sannies / mp4parser

A Java API to read, write and create MP4 files
Apache License 2.0
2.76k stars 571 forks source link

Trimming video is swapping video for audio track in mime type #475

Open slyvelikov opened 3 months ago

slyvelikov commented 3 months ago

Hi all !

Im using mp4parser for trimming and "stitching"/appending the videos. The issue I have is that when Im in debug mode I see that the trimmed video has a swapped/switched audio and video track. Thus when I check the MIME type of the video it becomes "audio/mp4" instead of "video/mp4". Im using the Movie class for appending these tracks. The other odd thing Ive come across is that this issue only appears when Im trimming from the beginning of the video. All videos are longer than 5-6 seconds. If I trim from the end towards the start it successfully appends my 2 videos into one. Ive checked the actual video files in the folder and the files are there and they are playable its just when I append/stitch them together and the video is trimmed from the beginning the issue occurs. Here is the code for stitching/appending the videos:

File baseFile = (File) intent.getExtras().get(INTENT_EXTRA_BASE_FILE); File fileToAppend = (File) intent.getExtras().get(INTENT_EXTRA_FILE_TO_APPEND); File outputFile = (File) intent.getExtras().get(INTENT_EXTRA_OUTPUT_FILE); if (!isIntentValid(baseFile, fileToAppend, outputFile)) { return; }

// Log.d(TAG, "Appending file " + baseFile.getAbsolutePath() + " to " + fileToAppend.getName()); try { Movie[] inputMovies = new Movie[]{ MovieCreator.build(baseFile.getAbsolutePath()), MovieCreator.build(fileToAppend.getAbsolutePath())};

        List<Track> videoTracks = new LinkedList<Track>();
        List<Track> audioTracks = new LinkedList<Track>(); 
        for (Movie m : inputMovies) {
            System.out.println();
            for (Track t : m.getTracks()) {
                if (t.getHandler().equals("soun")) {
                    audioTracks.add(t);
                }
                if (t.getHandler().equals("vide")) {
                    videoTracks.add(t);
                }
            }
        }

        Movie result = new Movie();
        int numAudioTracks = audioTracks.size();
        if (numAudioTracks > 0) {
            result.addTrack(new AppendTrack(audioTracks.toArray(new Track[numAudioTracks])));
        }
        int numVideoTracks = videoTracks.size();
        if (numVideoTracks > 0) {
            result.addTrack(new AppendTrack(videoTracks.toArray(new Track[numVideoTracks])));
        }

        Container out = new DefaultMp4Builder().build(result);

// Log.d(TAG, "Writing combined video files to: " + outputFile.getAbsolutePath()); FileChannel fc = new RandomAccessFile(outputFile.getAbsolutePath(), "rw").getChannel(); out.writeContainer(fc); fc.close();

        // Delete input files after stitching has completed
        baseFile.delete();
        fileToAppend.delete();
    } catch (IOException e) {

// Log.e(TAG, e.getMessage(), e); CommonMethods.outputErrorInformation(e, "Video stitching failed."); }

Any ideas ?