[Libav-user] How to set the video and audio's timestamp

Tony fantasyvideo at 126.com
Tue Aug 9 17:23:55 EEST 2016


In my application , I used  ffmpeg to publish my camera and microphone 's av stream as rtmp stream to nginx server.
I use following code to publish stream:
void RtmpLiveEncoder::Run()
{
AVBitStreamFilterContext* aacbsfc =  av_bitstream_filter_init("aac_adtstoasc");
start_time = av_gettime();
while(1)
{
    do
    {
         int ret = 0;
         AVPacket pkt;
        av_init_packet(&pkt);
        ret = av_read_frame(ifmt_ctx,&pkt);
        if(ret<0 )
        {
            printf("read video frame failed\n");
            break;
        }


        if(pkt.pts==AV_NOPTS_VALUE)
        {
            if(_frameduration==0)
            {
                pkt.dts = pkt.pts=(av_gettime()-start_time)/1000;
            }
            else
            {
                pkt.dts = pkt.pts = _lastvideopts;
                pkt.duration = _frameduration;
                pkt.pos = -1;
                _lastvideopts += _frameduration;
            }

        }

        if(av_write_frame(ofmt_ctx,&pkt)<0)
        {
           printf("write video frame failed\n");
        }

        av_packet_unref(&pkt);
    }while(0);

    do
    {
        if(!_hasaudio)
        {
            break;
        }
        if((_lastaudiopts-_lastvideopts)>0)
        {
            printf("the audio is faster than video, the audio pts is %d, the video pts is %d\n",_lastaudiopts,_lastvideopts);
            break;
        }

        int ret = 0;
        AVPacket audiopacket;
        av_init_packet(&audiopacket);
        ret = av_read_frame(aifmt_ctx,&audiopacket);
        if(ret<0)
        {
          break;
        }

        AVStream* out_stream = ofmt_ctx->streams[1];
        if(av_bitstream_filter_filter(aacbsfc, out_stream->codec, NULL, &audiopacket.data, &audiopacket.size, audiopacket.data, audiopacket.size, 0)<0)
        {
            printf("remove adts header failed\n");
        }

        if(av_bitstream_filter_filter(aacbsfc, out_stream->codec, NULL, &audiopacket.buf->data, &audiopacket.buf->size, audiopacket.buf->data, audiopacket.buf->size, 0)<0)
        {
           printf("remove adts header failed\n");

        }

        audiopacket.stream_index=1;
        audiopacket.dts = audiopacket.pts=_lastaudiopts;
        audiopacket.duration = (double)1024/out_stream->codecpar->sample_rate*1000;
        _lastaudiopts += audiopacket.duration;
        audiopacket.pos = -1;


        if(av_write_frame(ofmt_ctx,&audiopacket)<0)
        {
            printf("write audio failed.\n");
        }

        av_packet_unref(&audiopacket);

    }while(0);

}
I used vlc to access the hls stream, but after some minutes, the audio&video isn't sync any more.
How could I do?
THANKS IN ADVANCE!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160809/f5ca5bd3/attachment.html>


More information about the Libav-user mailing list