1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| bool CopyAudioStream(PAVISTREAM & audioStream)
{
// the new audio stream
PAVISTREAM newstream;
// to get infos on the audio stream to copy
AVISTREAMINFO avi_stream_info;
// get infos about the audio stream to copy
if(AVIStreamInfo(audioStream, &avi_stream_info, sizeof(AVISTREAMINFO)))
{
ShowMessage("Error with AVIStreamInfo");
}
// get the first sample number
int audioSrcStreamStart = AVIStreamStart(audioStream);
// get the last sample number
int audioSrcStreamStop = AVIStreamEnd(audioStream);
// to memorize the wave infos of the audio stream to copy
WAVEFORMATEX wave;
LONG lStreamSize;
// get the size of the WAVEFORMATEX
if(AVIStreamReadFormat(audioStream, audioSrcStreamStart, NULL, &lStreamSize))
{
ShowMessage("Error with AVIStreamReadFormat (0)");
}
// get the wave infos of the audio stream to copy
if(AVIStreamReadFormat(audioStream, audioSrcStreamStart, &wave, &lStreamSize))
{
ShowMessage("Error with AVIStreamReadFormat (1)");
}
// create the new audio stream int the PAVIFILE avi
if(AVIFileCreateStream(this->avi, &newstream, &avi_stream_info))
{
ShowMessage("Error with AVIFileCreateStream");
}
// set the same wave format than the audio stream to copy
if(AVIStreamSetFormat(newstream, 0, &wave, sizeof(wave)))
{
ShowMessage("Error with AVIStreamSetFormat");
}
//Copie byte par byte
for(int i = audioSrcStreamStart; i < audioSrcStreamStop; i++)
{
long numberOfBytesReaden = 0;
// get the size (in bytes) of the next sample to copy
if(AVIStreamRead(audioStream, i, 1, NULL, 0, &numberOfBytesReaden, 0))
{
ShowMessage("Error with AVIStreamRead (0)");
}
// create the frame to receive the frame bytes from the audio stream to copy
unsigned char * frame = new unsigned char[numberOfBytesReaden];
// save the current audio stream sample to the buffer frame
if(AVIStreamRead(audioStream, i, 1, frame, numberOfBytesReaden, 0, 0))
{
ShowMessage("Error with AVIStreamRead (1)");
}
// verification if the current sample is a key frame
if(AVIStreamIsKeyFrame(audioStream, i))
{
// write it in the audio stream destination as a key frame
if(AVIStreamWrite(newstream, i - audioSrcStreamStart, 1, frame, numberOfBytesReaden, AVIIF_KEYFRAME, 0, 0))
{
ShowMessage("Error with AVIStreamWrite (0)");
}
}
// else write it in the audio stream destination as a basic frame
else if(AVIStreamWrite(newstream, i - audioSrcStreamStart, 1, frame, numberOfBytesReaden, AVIIF_KEYFRAME, 0, 0))
{
ShowMessage("Error with AVIStreamWrite (1)");
}
// delete the buffer
delete [] frame;
}
// release the created audio stream
AVIStreamRelease(newstream);
} |
Partager