将音频(aac)和视频(mp4容器中的h.264)合并到使用Xuggler的mp4容器中 [英] Merging audio(aac) and video (h.264 in mp4 container) into a mp4 container using Xuggler

查看:1191
本文介绍了将音频(aac)和视频(mp4容器中的h.264)合并到使用Xuggler的mp4容器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用的代码

    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场景媒体播放。

The output file (mp4) is generating , but unable to play it using (vlc) and in JavaFX scene media.

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

Please help me with the inputs on the above code I'm using it in a correct way (Or) help me with the possible solution for merging audio(aac) and video(h264) to mp4 container.

感谢提前。

推荐答案

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

All streams must come to a close. All of yours are still open.

这篇关于将音频(aac)和视频(mp4容器中的h.264)合并到使用Xuggler的mp4容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆