java - 使用 Xuggler 将音频(aac)和视频(mp4 容器中的 h.264)合并到 mp4 容器中

标签 java javafx ffmpeg desktop-application xuggler

这是我正在使用的代码

    String filenamevideo = videoFilePath;(video.mp4)

    String filenameaudio = audioAACFilePath; (audio.aac)



    IMediaWriter mWriter = ToolFactory.makeWriter(videoWithAudioFilePath); // output
    // file

    IContainer containerVideo = IContainer.make();
    IContainer containerAudio = IContainer.make();

    if (containerVideo.open(filenamevideo, IContainer.Type.READ, null) < 0)
        throw new IllegalArgumentException("Cant find " + filenamevideo);

    if (containerAudio.open(filenameaudio, IContainer.Type.READ, null) < 0)
        throw new IllegalArgumentException("Cant find " + filenameaudio);

    int numStreamVideo = containerVideo.getNumStreams();
    int numStreamAudio = containerAudio.getNumStreams();    

    int videostreamt = -1; // this is the video stream id
    int audiostreamt = -1;

    IStreamCoder videocoder = null;

    for (int i = 0; i < numStreamVideo; i++) {
        IStream stream = containerVideo.getStream(i);
        IStreamCoder code = stream.getStreamCoder();

        if (code.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
            videostreamt = i;
            videocoder = code;
            break;
        }

    }

    for (int i = 0; i < numStreamAudio; i++) {
        IStream stream = containerAudio.getStream(i);
        IStreamCoder code = stream.getStreamCoder();

        if (code.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO) {
            audiostreamt = i;
            break;
        }

    }

    if (videostreamt == -1)
        throw new RuntimeException("No video steam found");
    if (audiostreamt == -1)
        throw new RuntimeException("No audio steam found");

    if (videocoder.open() < 0)
        throw new RuntimeException("Cant open video coder");
    IPacket packetvideo = IPacket.make();

    IStreamCoder audioCoder = containerAudio.getStream(audiostreamt).getStreamCoder();

    if (audioCoder.open() < 0)
        throw new RuntimeException("Cant open audio coder");

    mWriter.addAudioStream(0, 0, ICodec.ID.CODEC_ID_AAC, audioCoder.getChannels(),audioCoder.getSampleRate());

    mWriter.addVideoStream(1, 0, ICodec.ID.CODEC_ID_H264, videocoder.getWidth(), videocoder.getHeight());


    IPacket packetaudio = IPacket.make();

    while (containerVideo.readNextPacket(packetvideo) >= 0 || containerAudio.readNextPacket(packetaudio) >= 0) {

        if (packetvideo.getStreamIndex() == videostreamt) {

            // video packet
            IVideoPicture picture = IVideoPicture.make(videocoder.getPixelType(), videocoder.getWidth(),
                    videocoder.getHeight());
            int offset = 0;
            while (offset < packetvideo.getSize()) {
                int bytesDecoded = videocoder.decodeVideo(picture, packetvideo, offset);
                if (bytesDecoded < 0)
                    throw new RuntimeException("bytesDecoded not working");
                offset += bytesDecoded;
                if (picture.isComplete()) {
                    // System.out.println(picture.getPixelType());
                    mWriter.encodeVideo(1, picture);
                }
            }
        }

        if (packetaudio.getStreamIndex() == audiostreamt) {
            // audio packet
            IAudioSamples samples = IAudioSamples.make(512, audioCoder.getChannels(), IAudioSamples.Format.FMT_S32);
            int offset = 0;
            while (offset < packetaudio.getSize()) {
                int bytesDecodedaudio = audioCoder.decodeAudio(samples, packetaudio, offset);
                if (bytesDecodedaudio < 0)
                    throw new RuntimeException("could not detect audio");
                offset += bytesDecodedaudio;
                if (samples.isComplete()) {
                    mWriter.encodeAudio(0, samples);
                }
            }

        }           
    }

输出文件 (mp4) 正在生成,但无法使用 (vlc) 和 JavaFX 场景媒体播放。

请帮助我输入上述代码的输入,我以正确的方式使用它(或)帮助我解决将音频(aac)和视频(h264)合并到 mp4 容器的可能解决方案。

预先感谢。

最佳答案

所有的流都必须结束。你所有的仍然是开放的。

关于java - 使用 Xuggler 将音频(aac)和视频(mp4 容器中的 h.264)合并到 mp4 容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33923058/

相关文章:

java - 使用 Eclipse 和 JBoss 调试 JSP

listview - 如何使ListView中的文本居中?

JavaFX DatePicker 禁用 future 日期

ffmpeg - 启用 VideoJS 播放 .mov

FFMPEG:编码解码器 'rgb24' 的像素格式不兼容 'gif' ,自动选择格式 'rgb8'

java - 指定Java中的默认文件编码

java - 如何让此扫描仪在空行之前停止?

java - 使用 Quicksort 的内置 Java 数组/链表排序算法

java - 不使用 CSS 样式化 JavaFX 组件

filter - ffmpeg drawtext - 仅在剪切矩形内绘制