概述
av_register_all();
std::string filename = "d6.mp4";
AVFormatContext *outContext = NULL;
avformat_alloc_output_context2(&outContext, NULL, "mp4", filename.c_str());
AVStream *stream = avformat_new_stream(outContext, NULL);
stream->codec->codec_id = AV_CODEC_ID_H264;
stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
//stream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
stream->codec->width = 1280;
stream->codec->height = 720;
//stream->codec->bit_rate = context->bit_rate;
stream->codec->time_base.den = 25;
stream->codec->time_base.num = 1;
stream->nb_frames = 1050;
stream->codec->qmin = 10;
stream->codec->qmax = 51;
stream->codec->qcompress = 0.6;
//stream->time_base.den = 25;
//stream->time_base.num = 1;
AVStream *audioStream = avformat_new_stream(outContext, NULL);
if (outContext->oformat->flags & AVFMT_GLOBALHEADER)
{
stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
audioStream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
audioStream->codec->codec_id = AV_CODEC_ID_AAC;
audioStream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
audioStream->codec->sample_rate = 16000;
audioStream->codec->channels = 1;
audioStream->codec->bits_per_raw_sample = 16;
audioStream->codec->sample_fmt = AV_SAMPLE_FMT_S16;
audioStream->codec->time_base.den = 30;
audioStream->codec->time_base.num = 1;
audioStream->codec->frame_size = 1024;//av_samples_get_buffer_size(NULL,
// audioStream->codec->channels, 1024, audioStream->codec->sample_fmt, 1);;
//audioStream->time_base.den = 30;
//audioStream->time_base.num = 1;
AVBitStreamFilterContext* aacbsfc = av_bitstream_filter_init("aac_adtstoasc");
AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");
avio_open(&outContext->pb, filename.c_str(), AVIO_FLAG_WRITE);
av_dump_format(outContext, 0, NULL, 1);
AVDictionary* opt = NULL;
//av_dict_set_int(&opt, "video_track_timescale", 25, 0);
avformat_write_header(outContext, &opt);
MediaFormat informat;
informat.m_audioCodec = IMS_FRAME_G711A;
informat.m_audioChannel = 1;
informat.m_audioSampleRate = 16000;
informat.m_audioPropSize = 16;
MediaFormat outformat;
outformat.m_audioCodec = IMS_FRAME_AAC_ADTS;
outformat.m_audioChannel = 1;
outformat.m_audioSampleRate = 16000;
outformat.m_audioPropSize = 16;
AudioConvert *convert = new AudioConvert();
convert->init(informat, outformat);
while ((!feof(fr)) && (len == sizeof(info)))
{
.........
if (视频帧)
{
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = (unsigned char *)(pBuf);
pkt.size = info.len;
pkt.stream_index = stream->index;
pkt.pts = stream->pts.val * stream->time_base.num / stream->time_base.den;// info.timestamp / 25;
//int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(stream->time_base);
//pkt.dts = pkt.pts = (double)(count*calc_duration) / (double)(av_q2d(stream->time_base)*AV_TIME_BASE);
int type = pBuf[4];
//printf("pts:%.2X, %dn", type, type & 0x1F);
if (info.flag == 0x22)
{
pkt.flags |= AV_PKT_FLAG_KEY;
}
if (stream->codec->extradata == NULL && info.flag == 0x22)
{
int i = 0;
char *pbuffer = pBuf;
for (;i < info.len - 4; i++)
{
UINT32 data = *((uint32_t*)pbuffer);
if (data == 0x01000000 && *(pbuffer + 5) == 0x65)
{
break;
}
}
stream->codec->extradata_size = i;
stream->codec->extradata = new uint8_t[i + FF_INPUT_BUFFER_PADDING_SIZE];
memcpy(stream->codec->extradata, pBuf, i);
memset(stream->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
}
//av_bitstream_filter_filter(h264bsfc, stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
if (av_interleaved_write_frame(outContext, &pkt) < 0)
printf("write frame error");
count++;
Sleep(20);
//fwrite(pBuf, info.len, 1, fv);
}
else
{
//fwrite(pBuf, info.len, 1, fa);
if (convert)
{
outlen = 0;
convert->convert(pBuf, info.len, buffer, outlen);
}
if (outlen > 0)
{
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = (unsigned char *)(buffer);
pkt.size = outlen;
pkt.stream_index = audioStream->index;
pkt.pts = audioStream->pts.val * audioStream->time_base.num / audioStream->time_base.den;
av_bitstream_filter_filter(aacbsfc, audioStream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
if (av_interleaved_write_frame(outContext, &pkt) < 0)
printf("write frame error");
}
}
delete[]pBuf;
......
}
......
av_write_trailer(outContext);
avcodec_close(outContext->streams[0]->codec);
av_freep(&outContext->streams[0]->codec);
av_freep(&outContext->streams[0]);
avcodec_close(outContext->streams[1]->codec);
av_freep(&outContext->streams[1]->codec);
av_freep(&outContext->streams[1]);
avio_close(outContext->pb);
av_free(outContext);
最后
以上就是可靠背包为你收集整理的ffmpeg 保存数据流到文件的全部内容,希望文章能够帮你解决ffmpeg 保存数据流到文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复