2024年1月3日发(作者:)
FILE *fp;
fp=fopen("123.h264","wb+");
char *InputFileName="4";
if ((ret = avformat_open_input(&ic, InputFileName, NULL, NULL)) < 0) { return ret; }
if ((ret = avformat_find_stream_info(ic, NULL)) < 0)
{
avformat_close_input(&ic);
return ret;
}
spsLength=ic->streams[0]->codec->extradata[6]*0xFF+ic->streams[0]->codec->extradata[7];
ppsLength=ic->streams[0]->codec->extradata[8+spsLength+1]*0xFF+ic->streams[0]->codec->extradata[8+spsLength+2];
for (int i=0;i
for (int i=0;i
for(int i=0;i
if(ic->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) { videoindex=i; } else if(ic->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) { audioindex=i; } }
AVOutputFormat *ofmt = NULL; AVPacket pkt;
avformat_alloc_output_context2(&oc, NULL, NULL, OutPutPath);
if (!oc)
{ printf( "Could not create output contextn"); ret = AVERROR_UNKNOWN; } ofmt = oc->oformat; int i;
for (i = 0; i < ic->nb_streams; i++)
{ AVStream *in_stream = ic->streams[i]; AVStream *out_stream = avformat_new_stream(oc, in_stream->codec->codec);
if (!out_stream)
{ printf( "Failed allocating output streamn"); ret = AVERROR_UNKNOWN; } ret = avcodec_copy_context(out_stream->codec, in_stream->codec); if (ret < 0)
{ printf( "Failed to copy context from input to output stream codec contextn"); } out_stream->codec->codec_tag = 0;
if (oc->oformat->flags & AVFMT_GLOBALHEADER) out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; }
if (!(ofmt->flags & AVFMT_NOFILE))
{ ret = avio_open(&oc->pb, OutPutPath, AVIO_FLAG_WRITE); if (ret < 0)
{ printf( "Could not open output file '%s'", OutPutPath);
} } ret = avformat_write_header(oc, NULL);
int frame_index=0; int flag=1;
av_init_packet(&pkt); = NULL; = 0;
while (1)
{ AVStream *in_stream, *out_stream;
ret = av_read_frame(ic, &pkt);
if (ret < 0) break; in_stream = ic->streams[_index]; out_stream = oc->streams[_index];
AVPacket tmppkt; if (in_stream->codec->codec_type==AVMEDIA_TYPE_VIDEO ) {
if (flag) { fwrite(startcode,4,1,fp); fwrite(sps,spsLength,1,fp); fwrite(startcode,4,1,fp); fwrite(pps,ppsLength,1,fp);
[0]=0x00; [1]=0x00; [2]=0x00; [3]=0x01; fwrite(,,1,fp);
flag=0; } else { [0]=0x00; [1]=0x00; [2]=0x00; [3]=0x01; fwrite(,,1,fp); }
= av_rescale_q_rnd(, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX)); = av_rescale_q_rnd(, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX)); on = av_rescale_q(on, in_stream->time_base, out_stream->time_base); = -1;
_index=0; ret = av_interleaved_write_frame(oc, &pkt); }
av_free_packet(&pkt); }
fclose(fp); fp=NULL;
av_write_trailer(oc); return 0;}


发布评论