基于vs2017 vc++ ffmpeg4.0.2下测试
ffmpeg 环境配置请百度(vs2017 ffmpeg )
部分方法在https://blog.csdn.net/Java_lilin/article/details/85118365中查找
头文件参考上篇
int main33333 ()
{
AVOutputFormat *ofmt = NULL;
//Input AVFormatContext and Output AVFormatContext
AVFormatContext *ifmt_ctx = avformat_alloc_context(), *ofmt_ctx = NULL;
AVPacket pkt;
const char *in_filename, *out_filename;
int ret, i;
int videoindex = -1;
int _index = 0;
in_filename = \"rtmp://192.168.0.108/live/a\";//\"rtmp://192.168.0.108/live/f\";
//in_filename = \"rtp://233.233.233.233:6666\";
//out_filename = \"receive.ts\";
//out_filename = \"receive.mkv\";
out_filename = \"C:\\\\Users\\\\lilin\\\\Desktop\\\\out.flv\";
//Network
avformat_network_init();
//av_dict_set(&options, \"video_size\",\"1920*1080\",0);//大小 默认全部
cchaoshitime = av_gettime();//每次循环帧都要这个呢
ifmt_ctx->interrupt_callback.callback = interrupt_test; //超时判断设置
if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
printf(\"Could not open input file.\");
return -1;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
printf(\"Failed to retrieve input stream information\");
return -1;
}
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoindex = i;
break;
}
}
av_dump_format(ifmt_ctx, 0, in_filename, 0);
//Output
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename); //
if (!ofmt_ctx) {
printf(\"Could not create output context\\n\");
return -1;
}
ofmt = ofmt_ctx->oformat;
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
//Create output AVStream according to input AVStream
AVStream *in_stream = ifmt_ctx->streams[i];
AVCodec *incodec = avcodec_find_encoder(in_stream->codecpar->codec_id);
if (!incodec) {
printf(\"not found encoder 1 \");
return -1;
}
AVStream *out_stream = avformat_new_stream(ofmt_ctx, incodec);
if (!out_stream) {
printf(\"Failed allocating output stream\\n\");
ret = AVERROR_UNKNOWN;
return -1;
}
//Copy the settings of AVCodecContext
AVCodecContext *codec_ctx = avcodec_alloc_context3(incodec);
ret = avcodec_parameters_to_context(codec_ctx, in_stream->codecpar);
if (ret < 0) {
printf(\"Failed to copy in_stream codecpar to codec context\\n\");
return -1;
}
codec_ctx->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
ret = avcodec_parameters_from_context(out_stream->codecpar, codec_ctx);
if (ret < 0) {
printf(\"Failed to copy codec context to out_stream codecpar context\\n\");
}
}
//Dump Format------------------
av_dump_format(ofmt_ctx, 0, out_filename, 1);
//Open output URL
if (!(ofmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0) {
printf(\"Could not open output URL \'%s\'\", out_filename);
return -1;
}
}
//Write file header
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
printf(\"Error occurred when opening output URL\\n\");
return -1;
}
//AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init(\"h264_mp4toannexb\");
while (1) {
cchaoshitime = av_gettime();
AVStream *in_stream, *out_stream;
//Get an AVPacket
ret = av_read_ (ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
//简化的时间帧计算 非简化参考上pian
av_packet_rescale_ts(&pkt, in_stream->time_ , out_stream->time_ );
ret = av_interleaved_write_ (ofmt_ctx, &pkt);
if (ret < 0) {
printf(\"Error muxing packet\\n\");
break;
}
av_packet_unref(&pkt);
}
//Write file trailer
av_write_trailer(ofmt_ctx);
avformat_close_input(&ifmt_ctx);
/* close output */
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) {
avio_close(ofmt_ctx->pb);
}
avformat_free_context(ofmt_ctx);
if (ret < 0 && ret != AVERROR_EOF) {
printf(\"Error occurred.\\n\");
return -1;
}
printf(\"ok1\");
getchar();
return 0;
}
讨论群261074724
继续阅读与本文标签相同的文章
Flink数据查询和过滤
1003 我要通过! (20 分)
-
零售电商月销售额高达20+万?小程序分销模式助你一臂之力
2026-05-18栏目: 教程
-
如何让日志说话,让它主动说话
2026-05-18栏目: 教程
-
《Android组件化架构》| 每日读本书
2026-05-18栏目: 教程
-
从零开始入门 K8s | 详解 K8s 容器基本概念
2026-05-18栏目: 教程
-
8年前诞生于淘宝,细数阿里云RPA 的前世今生!
2026-05-18栏目: 教程
