ffmpeg mpeg转asf 音视频同步问题
while(av_read_frame(ic,&packet)>=0)//从输入文件中读取一个包
{
if(packet.stream_index==videoindex)//判断是否为当前视频流中的包
{
len=avcodec_decode_video(vCodecCtx,oVFrame,&frameFinished,packet.data,packet.size);//若为视频包,解码该视频包
if(len<0)
{
printf("Error while decoding\n");
exit(0);
}
if(frameFinished)//判断视频祯是否读完
{
fflush(stdout);
oVFrame->pts=av_rescale(frame_index,AV_TIME_BASE*(int64_t)oVcc->time_base.num,oVcc->time_base.den);
oVFrame->pict_type=0;
out_size = avcodec_encode_video(oVcc, video_outbuf, video_outbuf_size, oVFrame);
if (out_size > 0)
{
AVPacket pkt;
av_init_packet(&pkt);
if(oVcc->coded_frame && oVcc->coded_frame->key_frame)
pkt.flags |= PKT_FLAG_KEY;
pkt.flags = packet.flags;
pkt.stream_index= video_st->index;
pkt.data= video_outbuf;
pkt.size= out_size;
ret=av_write_frame(oc, &pkt);
}
frame_index++;
}
else
ret=av_write_frame(oc, &packet);
//img_convert((AVPicture *)vFrame, PIX_FMT_RGB24, (AVPicture*)oVFrame, oVcc->pix_fmt,oVcc->width,oVcc->height);
//SaveFrame(vFrame,oVcc->width,oVcc->height,frame_index);
if(ret!=0)
{
printf("while write video frame error\n");
exit(0);
}
}
else if(packet.stream_index==audioindex)
{
len=packet.size;
ptr=packet.data;
int ret=0;
while(len>0)
{
out_buf=NULL;
out_size=0;
if(&packet)
samples=(short *)av_fast_realloc(samples,&samples_size,
FFMAX(packet.size*sizeof(*samples),AVCODEC_MAX_AUDIO_FRAME_SIZE));
out_size=samples_size;
ret=avcodec_decode_audio(aCodecCtx,samples,&out_size,ptr,len);//若为音频包,解码该音频包
if(ret<0)
{
printf("while decode audio failure\n");
exit(0);
}
fflush(stdout);
ptr+=ret;
len-=ret;
if(out_size<=0)
continue;
out_buf=samples;
AVPacket pkt;
av_init_packet(&pkt);
pkt.size= avcodec_encode_audio(oAcc, audio_outbuf, audio_outbuf_size, out_buf);
pkt.pts= av_rescale_q(oAcc->coded_frame->pts, oAcc->time_base, audio_st->time_base);
pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= audioindex;
pkt.data= audio_outbuf;
if (av_write_frame(oc, &pkt) != 0)
{
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
}
}
av_free_packet(&packet);
}
{
if(packet.stream_index==videoindex)//判断是否为当前视频流中的包
{
len=avcodec_decode_video(vCodecCtx,oVFrame,&frameFinished,packet.data,packet.size);//若为视频包,解码该视频包
if(len<0)
{
printf("Error while decoding\n");
exit(0);
}
if(frameFinished)//判断视频祯是否读完
{
fflush(stdout);
oVFrame->pts=av_rescale(frame_index,AV_TIME_BASE*(int64_t)oVcc->time_base.num,oVcc->time_base.den);
oVFrame->pict_type=0;
out_size = avcodec_encode_video(oVcc, video_outbuf, video_outbuf_size, oVFrame);
if (out_size > 0)
{
AVPacket pkt;
av_init_packet(&pkt);
if(oVcc->coded_frame && oVcc->coded_frame->key_frame)
pkt.flags |= PKT_FLAG_KEY;
pkt.flags = packet.flags;
pkt.stream_index= video_st->index;
pkt.data= video_outbuf;
pkt.size= out_size;
ret=av_write_frame(oc, &pkt);
}
frame_index++;
}
else
ret=av_write_frame(oc, &packet);
//img_convert((AVPicture *)vFrame, PIX_FMT_RGB24, (AVPicture*)oVFrame, oVcc->pix_fmt,oVcc->width,oVcc->height);
//SaveFrame(vFrame,oVcc->width,oVcc->height,frame_index);
if(ret!=0)
{
printf("while write video frame error\n");
exit(0);
}
}
else if(packet.stream_index==audioindex)
{
len=packet.size;
ptr=packet.data;
int ret=0;
while(len>0)
{
out_buf=NULL;
out_size=0;
if(&packet)
samples=(short *)av_fast_realloc(samples,&samples_size,
FFMAX(packet.size*sizeof(*samples),AVCODEC_MAX_AUDIO_FRAME_SIZE));
out_size=samples_size;
ret=avcodec_decode_audio(aCodecCtx,samples,&out_size,ptr,len);//若为音频包,解码该音频包
if(ret<0)
{
printf("while decode audio failure\n");
exit(0);
}
fflush(stdout);
ptr+=ret;
len-=ret;
if(out_size<=0)
continue;
out_buf=samples;
AVPacket pkt;
av_init_packet(&pkt);
pkt.size= avcodec_encode_audio(oAcc, audio_outbuf, audio_outbuf_size, out_buf);
pkt.pts= av_rescale_q(oAcc->coded_frame->pts, oAcc->time_base, audio_st->time_base);
pkt.flags |= PKT_FLAG_KEY;
pkt.stream_index= audioindex;
pkt.data= audio_outbuf;
if (av_write_frame(oc, &pkt) != 0)
{
fprintf(stderr, "Error while writing audio frame\n");
exit(1);
}
}
}
av_free_packet(&packet);
}
...全文
相关推荐
ffmpeg编码成mpeg4或h264文件
ffmpeg编码成mpeg4或h264文件
android ffmpeg mpeg编码-调用摄像头
可以使用,在ffmpegutils.c文件中Java_com_hclydao_webcam_Ffmpeg_open 改为你的摄像头文件名称(例如vedio0)即可
ffmpeg的快速音视频开发方法
本文介绍了ffmpeg的快速音视频开发方法
ffmpeg实现录屏+录音
最简单的基于FFmpeg的AVDevice例子(屏幕录制+声音采集),视频采用mpeg4编码,音频采用aac编码,并生成mp4文件,以前上传的有一个bug,此版本把他修复(因为不能修改资源,所以重新传一个,csdn功能不够啊)
MPEG-PS 流 打包 解包
MPEG-PS 流 打包 解包


工具平台和程序库
