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

Combine a music.m4a on a video.mp4 overwrite my video audio #92

Closed
mico81 opened this issue Jul 1, 2015 · 2 comments
Closed

Combine a music.m4a on a video.mp4 overwrite my video audio #92

mico81 opened this issue Jul 1, 2015 · 2 comments

Comments

@mico81
Copy link

mico81 commented Jul 1, 2015

Hi,
I need to combine a song (.m4a) with a video (.mp4). I'm using mp4Parser, it's worked but my song overwrite the audio track of my video, and I need to have these two audio tracks on my video.
Can you help me please ?
There is my function for muxing my video with a song 👍

videoFile : mp4
audioFile : m4a
outputFile : mp4

public boolean mux(String videoFile, String audioFile, String outputFile) {

    Movie video;
    try {
        video = new MovieCreator().build(videoFile);
        Log.d("ATTENTION","On a fini le try du montage de video");
    } catch (RuntimeException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    Movie audio;
    try {
        audio = new MovieCreator().build(audioFile);
        Log.d("ATTENTION","On a fini le try de montage de audio");
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (NullPointerException e) {
        e.printStackTrace();
        return false;
    }

    Log.d("TAILLE",String.valueOf(video.getTracks().size()));
    Track audioTrack = audio.getTracks().get(0);
    Track videoTrack = video.getTracks().get(0);
    Track videoAudioTrack = video.getTracks().get(1);
    Movie result = new Movie();

    //Append all audio and video
    result.addTrack(videoTrack);
    result.addTrack(audioTrack);
    result.addTrack(videoAudioTrack);


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

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }
    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
    try {
        out.writeContainer(byteBufferByteChannel);
        byteBufferByteChannel.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    Log.d("ATTENTION","On a finis la conversion");
    return true;
}

And my BufferedWritableFileByteChannel metod 👍
private static class BufferedWritableFileByteChannel implements WritableByteChannel {
private static final int BUFFER_CAPACITY = 1000000;

    private boolean isOpen = true;
    private final OutputStream outputStream;
    private final ByteBuffer byteBuffer;
    private final byte[] rawBuffer = new byte[BUFFER_CAPACITY];

    private BufferedWritableFileByteChannel(OutputStream outputStream) {
        this.outputStream = outputStream;
        this.byteBuffer = ByteBuffer.wrap(rawBuffer);
    }

    @Override
    public int write(ByteBuffer inputBuffer) throws IOException {
        int inputBytes = inputBuffer.remaining();

        if (inputBytes > byteBuffer.remaining()) {
            dumpToFile();
            byteBuffer.clear();

            if (inputBytes > byteBuffer.remaining()) {
                throw new BufferOverflowException();
            }
        }

        byteBuffer.put(inputBuffer);

        return inputBytes;
    }

    @Override
    public boolean isOpen() {
        return isOpen;
    }

    @Override
    public void close() throws IOException {
        dumpToFile();
        isOpen = false;
    }
    private void dumpToFile() {
        try {
            outputStream.write(rawBuffer, 0, byteBuffer.position());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Sry for my english.

@sannies
Copy link
Owner

sannies commented Jul 1, 2015

Basically you want to merge two audio tracks to one. Players won't play two
audio tracks at the same time. This is a use case that requires decoding,
then merging and encoding into one audio track again. The MP4parser cannot
do that. I guess you'll need to use ffmpeg or the MediaCodec API.

Best Regards,
Sebastian

mico81 notifications@github.com schrieb am Mi., 1. Juli 2015 um 15:58 Uhr:

Hi,
I need to combine a song (.m4a) with a video (.mp4). I'm using mp4Parser,
it's worked but my song overwrite the audio track of my video, and I need
to have these two audio tracks on my video.
Can you help me please ?
There is my function for muxing my video with a song [image: 👍]

videoFile : mp4
audioFile : m4a
outputFile : mp4

public boolean mux(String videoFile, String audioFile, String outputFile) {

Movie video;
try {
    video = new MovieCreator().build(videoFile);
    Log.d("ATTENTION","On a fini le try du montage de video");
} catch (RuntimeException e) {
    e.printStackTrace();
    return false;
} catch (IOException e) {
    e.printStackTrace();
    return false;
}

Movie audio;
try {
    audio = new MovieCreator().build(audioFile);
    Log.d("ATTENTION","On a fini le try de montage de audio");
} catch (IOException e) {
    e.printStackTrace();
    return false;
} catch (NullPointerException e) {
    e.printStackTrace();
    return false;
}

Log.d("TAILLE",String.valueOf(video.getTracks().size()));
Track audioTrack = audio.getTracks().get(0);
Track videoTrack = video.getTracks().get(0);
Track videoAudioTrack = video.getTracks().get(1);
Movie result = new Movie();

//Append all audio and video
result.addTrack(videoTrack);
result.addTrack(audioTrack);
result.addTrack(videoAudioTrack);


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

FileOutputStream fos;
try {
    fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return false;
}
BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos);
try {
    out.writeContainer(byteBufferByteChannel);
    byteBufferByteChannel.close();
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
    return false;
}
Log.d("ATTENTION","On a finis la conversion");
return true;

}


Reply to this email directly or view it on GitHub
#92.

@mico81
Copy link
Author

mico81 commented Jul 1, 2015

Ok, i'll try with ffmpeg.
Thanks for your quick answer !

Best Regards,
Nicolas

@mico81 mico81 closed this as completed Jul 1, 2015
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

2 participants