This project has moved and is read-only. For the latest updates, please go here.

Add Audio to Video created by FFMPEG

Apr 2, 2014 at 11:04 AM
Edited Apr 2, 2014 at 11:08 AM
My program records screen & mic & speaker in webm.
  1. How i can add audio samples recorded by NAudio to Video?
  2. What I need to have synchronzed video & audio ( i screenshot 3 times/sec & add it to video )?
Here are my functions..

C++ part :
char* ByteArray_to_charptr(array<System::Byte>^ byteArray)
{
pin_ptr<System::Byte> p = &byteArray[0];
unsigned char* pby = p;
char* pch = reinterpret_cast<char*>(pby);
return pch;
// use it...
}

void VideoEncoder::AddAudioSampleBuffer(array<System::Byte>^ byteArray,int^ buffersize)
{
char * buffer=ByteArray_to_charptr(byteArray);

if (buffer && (int)buffersize > 0)
{
AddAudioSample(pFormatContext,pVideoStream,buffer,(int)buffersize);
}
}

bool VideoEncoder::AddAudioSample(AVFormatContext *pFormatContext, AVStream *pStream,
                                   const char* soundBuffer, int soundBufferSize)
{
AVCodecContext *pCodecCxt;
bool res = true;

pCodecCxt = pStream->codec;
memcpy(audioBuffer + nAudioBufferSizeCurrent, soundBuffer, soundBufferSize);
nAudioBufferSizeCurrent += soundBufferSize;

BYTE * pSoundBuffer = (BYTE *)audioBuffer;
int nCurrentSize = nAudioBufferSizeCurrent;

// Size of packet on bytes.
// FORMAT s16
DWORD packSizeInSize = 2 * audioInputSampleSize;

while(nCurrentSize >= packSizeInSize)
{
AVPacket pkt;
av_init_packet(&pkt);

pkt.size = avcodec_encode_audio(pCodecCxt, pAudioEncodeBuffer,
 nSizeAudioEncodeBuffer, (const short *)pSoundBuffer);
if (pCodecCxt->coded_frame && pCodecCxt->coded_frame->pts != AV_NOPTS_VALUE)
{
 pkt.pts = av_rescale_q(pCodecCxt->coded_frame->pts, pCodecCxt->time_base, >pStream->time_base);
}

pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = pStream->index;
pkt.data = pAudioEncodeBuffer;

// Write the compressed frame in the media file.
if (av_interleaved_write_frame(pFormatContext, &pkt) != 0)
{
 res = false;
 break;
}

nCurrentSize -= packSizeInSize;
pSoundBuffer += packSizeInSize;
}

// save excess
memcpy(audioBuffer, audioBuffer + nAudioBufferSizeCurrent - nCurrentSize, nCurrentSize);
nAudioBufferSizeCurrent = nCurrentSize;

return res;
}
& C# part
private static void wmic_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
   {
       if (e.BytesRecorded > 0)
           vencoder.AddAudioSampleBuffer(e.Buffer, e.BytesRecorded);
   }
This adds audio to video but I hear no audio or sometimes video also doesn't play at all.