Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
slyvelikov opened this issue Aug 30, 2024 · 0 comments
Open

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

slyvelikov opened this issue Aug 30, 2024 · 0 comments

Comments

@slyvelikov
Copy link

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant