使用xuggler从图片和mp3 java创建mp4

时间:2014-06-14 20:18:15

标签: java mp3 mp4 xuggler

我试图通过添加mp3文件将图片列表组合到mp4电影中。 电影的长度,用户可以选择mp3文件的长度或选择手动。 如果用户选择手动(长度!= mp3文件长度),则应该剪切或循环播放mp3文件。

没有它适用于图片但没有声音:(

private void convertImageToVideo() {

    IMediaWriter writer = ToolFactory.makeWriter(outputFilename);

    long delay = videotime / PicPathList.size();

    long milliseconds = 0;

    //adds Pictures to the mp4 stream
    for (int i = 0; i < PicPathList.size(); i++) {



        BufferedImage bi;
        try {
            bi = ImageIO.read(new File(PicPathList.get(i)));
            bi = Tools.prepareForEncoding(bi);

            int width=bi.getWidth();
            int height=bi.getHeight();

            if(width%2==1){
                width++;
            }

            if(height%2==1){
                height++;
            }

            if (i == 0) {
                writer.addVideoStream(0, 0, ID.CODEC_ID_H264, width, height);
            }
        //debug
            //  System.out.println(PicPathList.get(i) + ", bi:" + bi.getWidth() + "x"
            //        + bi.getHeight() + ", ms:" + milliseconds);
            writer.encodeVideo(0, bi, milliseconds, TimeUnit.MILLISECONDS);
            milliseconds += delay;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error");
        }





    }

    writer.close();

//at this part Im trying to combine the further generated mp4 file with the mp3 file

     String inputVideoFilePath = outputFilename;
        String inputAudioFilePath = this.musicFile.getAbsolutePath();
        String outputVideoFilePath = "outputFilename";

        IMediaWriter mWriter = ToolFactory.makeWriter(outputVideoFilePath);

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

        // check files are readable
        containerVideo.open(inputVideoFilePath, IContainer.Type.READ, null);

       containerAudio.open(inputAudioFilePath, IContainer.Type.READ, null);

        // read video file and create stream
        IStreamCoder coderVideo = containerVideo.getStream(0).getStreamCoder();

        IPacket packetvideo = IPacket.make();
        int width = coderVideo.getWidth();
        int height = coderVideo.getHeight();

        // read audio file and create stream
        IStreamCoder coderAudio = containerAudio.getStream(0).getStreamCoder();

        IPacket packetaudio = IPacket.make();


        mWriter.addAudioStream(1, 0,coderAudio.getCodecID(), coderAudio.getChannels(), coderAudio.getSampleRate());
        mWriter.addVideoStream(0, 0, width, height);

        while (containerVideo.readNextPacket(packetvideo) >= 0) {

            containerAudio.readNextPacket(packetaudio);

            // video packet
            IVideoPicture picture = IVideoPicture.make(coderVideo.getPixelType(), width, height);
            coderVideo.decodeVideo(picture, packetvideo, 0);
            if (picture.isComplete()) 
                mWriter.encodeVideo(0, picture);

            // audio packet 
            IAudioSamples samples = IAudioSamples.make(512, coderAudio.getChannels(),      IAudioSamples.Format.FMT_S32);
            coderAudio.decodeAudio(samples, packetaudio, 0);
            if (samples.isComplete()) 
                mWriter.encodeAudio(1, samples);

        }

        coderAudio.close();
        coderVideo.close();
        containerAudio.close();
        containerVideo.close();
        mWriter.close();
}

2 个答案:

答案 0 :(得分:0)

您可以使用其他jar文件合并视频和音频。请注意这不是正确的方法,但我没有任何选择和时间去挖掘Xuggler代码。

我希望它也适合你。

package MP4;

/**
 *
 * @author Pasban
 */
import com.coremedia.iso.boxes.Container;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MuxMp4 {

    public static void merge(String audio, String video, String output) throws IOException {

        Movie countVideo = MovieCreator.build(video);
        Movie countAudioEnglish = MovieCreator.build(audio);

        Track audioTrackEnglish = countAudioEnglish.getTracks().get(0);
        audioTrackEnglish.getTrackMetaData().setLanguage("eng");

        countVideo.addTrack(audioTrackEnglish);

        Container out = new DefaultMp4Builder().build(countVideo);
        FileOutputStream fos = new FileOutputStream(new File(output));
        out.writeContainer(fos.getChannel());
        fos.close();


    }
}

检查MP4Parser示例代码以获取更多信息。

很高兴提到你的两个文件都应该是mp4。所以你需要将你的mp3转换为mp4。并且您的视频不应包含任何声音,在您的情况下不会包含任何声音。

正如我之前提到的,这不是完成工作的正确方法。

答案 1 :(得分:0)

我在这里回答了这个问题,这是一个完整的答案。

JAVA - Xuggler - Play video while combining an MP3 audio file and a MP4 movie