mbaros
mbaros

Reputation: 845

Media Foundation add audio stream to video file

I was able to successfully encode an MP4 file which contains H.264 encoded video only (using IMFSinkWriter interface). Now I want to add an audio stream to it.

Whenever I try to create a sink writer for an audio using:

MFCreateSinkWriterFromURL(filePath, null, null, &pSinkWriter)

it deletes the previous file and writes only the audio (well, according to this link it is expected).

So my question is: how to add an audio stream to an existing file which contains only video stream?

Or, If I have both raw data from audio and video how do I encode both of them into a single media file (I suppose I have to do something called multiplexing. If so, can someone provide me helpful references)?

Upvotes: 1

Views: 951

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

Sink Writer API creates a media file from scratch when you do IMFSinkWriter::BeginWriting to final completion when you do IMFSinkWriter::Finalize. You don't add new streams to finalized file (well, you can do it, but it works differently - see last paragraph below).

To create a media file with both video and audio you need to add two streams before you begin. Two calls IMFSinkWriter::AddStream, then two IMFSinkWriter::SetInputMediaType, then you start writing IMFSinkWriter::BeginWriting and you feed both video and audio data IMFSinkWriter::WriteSample providing respective stream index.

To adding a new stream to already existing file you need to create a completely new file. One of the options you have is to read already compressed data from existing file you have and write it to the new file using IMFSinkWriter::WriteSample method without re-compression. At the same time second stream can be written doing the compression. This way you can create a video and audio MP4 file by taking video from existing file and adding/encoding an additional audio track.

Upvotes: 2

Related Questions