2012-01-17 78 views
0

我想要的視頻文件(flv)沒有音頻使用Xuggler音頻文件(mp3)結合起來。目前,我已經拍攝了兩個視頻流,並將這些視頻流的視頻和音頻部分分別合成爲畫中畫。現在我想將音頻和視頻文件相互結合..任何建議或提示將不勝感激。我正在使用red5服務器。謝謝。如何加入一個音頻和視頻文件 - Xuggler

回答

0

使用MediaConcatenator。見示例代碼"Concatenate Audio And Video"

+2

它串聯隊友。我們需要音頻和視頻同時運行......這首先添加視頻,然後添加音頻文件。我們不需要那個 – 2012-01-18 06:36:57

+0

感謝您的回覆。但是我想爲第一個文件的視頻運行第二個文件的音頻文件。 – Hadi 2012-01-18 07:08:42

+0

因此,您可以嘗試創建一個具有2個視頻流1的音頻1的IContainer,而不是連接。然後在一個循環中解碼mp3和flv,創建一個'IPacket',然後使用'encodeVideo'和'encodeAudio'與相同的數據包。一旦數據包完成,使用'writePacket'(在輸出'IContainer'上)來錄製視頻和音頻。我想我在xuggle的wiki上看到一個教程,解釋如何做這樣的事情,但我找不到它。 – zeropouet 2012-01-18 08:29:01

3

我公司開發的一類,將採取兩個輸入,一個音頻文件和第二視頻文件,並將它們合併到一個單一的音頻視頻文件。

public static void main(String[] args) 
    { 

    String filenamevideo = "f:/testvidfol/video.mp4"; //video file on your disk 
    String filenameaudio = "f:/testvidfol/audio.wav"; //audio file on your disk 


    IMediaWriter mWriter = ToolFactory.makeWriter("f:/testvidfol/videowriter.flv"); //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(); 

    System.out.println("Number of video streams: "+numStreamVideo + "\n" + "Number of audio streams: "+numStreamAudio); 

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, audioCoder.getChannels(), audioCoder.getSampleRate()); 

    mWriter.addVideoStream(1, 1, 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); 

     } 
      } 

    } 

    } 
} 

希望它能幫助你。

+0

我正在尋找一種手段結合兩個音頻文件(就像同時播放一樣),我可以將視頻流修改爲音頻流2並使其工作? – Matical 2013-06-24 11:06:33

+0

@Matical他們這個代碼是用來結合兩個文件一個是音頻,第二個是視頻。但您可以對其進行自定義以實現所需的功能。只要理解代碼,你就可以得到你想要的。 – 2013-06-24 12:49:57

+0

感謝您的回覆,我一定會告訴你,我的進步(週末),共享代碼,我開始使用你的基地:) – Matical 2013-06-24 13:31:09