[Libav-user] Breaking up RTMP into short audio files

Cesareo Fernandez cesareof at gmail.com
Mon Mar 13 23:16:52 EET 2017


I am attempting to take a live audio stream of AAC and break up the input
into short 2 -> 3 second files.


bool ConvertAudioFrames( AVPacket packetList[], int &nBufferCntr,
AVCodecContext* InCodecCtx  )
{
bool bRet = false;
int nSize = 0;
AVPacket intermPacket;
av_init_packet(&intermPacket);
intermPacket.data = NULL;
intermPacket.size= 0;

AVCodec *pOutCodec=avcodec_find_encoder(AV_CODEC_ID_MP2);
AVCodecContext *pOutCtx = avcodec_alloc_context3(pOutCodec);
if( pOutCtx )
{
pOutCtx->bit_rate = 64000;
pOutCtx->sample_rate = select_sample_rate(pOutCodec);
pOutCtx->sample_fmt = AV_SAMPLE_FMT_S16;
pOutCtx->channels = InCodecCtx->channels;
pOutCtx->channel_layout = InCodecCtx->channel_layout;
pOutCtx->time_base.den = InCodecCtx->time_base.den;
pOutCtx->time_base.den = InCodecCtx->sample_rate;
pOutCtx->time_base.num = 1;

int codecret = avcodec_open2( pOutCtx, pOutCodec, NULL );
if( pOutCodec && (codecret >= 0) )
{
int encoded = 0;
FILE *f;
fopen_s( &f, "\\audio_out.mp2", "ab");

for( int i = 0; i < nBufferCntr; i++ )
{
AVFrame *decodedFrame = av_frame_alloc();
av_frame_unref(decodedFrame);

int frameFinished=0;
int nResult = avcodec_send_packet(InCodecCtx, &packetList[i]);
if( nResult == 0 )
nResult = avcodec_receive_frame(InCodecCtx, decodedFrame );

if( nResult == 0 )
{
if (avcodec_send_frame(pOutCtx, decodedFrame) == 0)
{
while (avcodec_receive_packet(pOutCtx, &intermPacket) == 0)
{
fwrite( intermPacket.data, 1, intermPacket.size, f);
av_packet_unref( &intermPacket );
bRet = true;
}
}
}
}
fclose(f);
}
avcodec_close( pOutCtx );
avcodec_free_context( &pOutCtx );
}
return bRet;
}

The goal would be to take the resulting converted file and send it through
a websocket to a page for playback via webAudio API, but in this code
example I am simply trying to write the resulting encoded packets to a
file. This does not work. What am I doing wrong? The answer to that is
probably plenty, but specifically how can I convert this AVPacket
packetList[] to a playable file?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170313/fdc76345/attachment.html>


More information about the Libav-user mailing list