c/c++使用ffmpeg库实现推流本地视频文件,播放过程中可控制跳转到视频指定位置的完整代码

时间: 2023-11-22 09:53:30 浏览: 191
以下是使用FFmpeg库实现推流本地视频文件,并且允许跳转到指定位置的完整代码。需要注意的是,本代码只实现了视频流的推流,如果需要同时推送音频流,还需要在代码中添加相应的音频处理代码。 ```c++ #include <iostream> #include <fstream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> extern "C"{ #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libavutil/avutil.h> #include <libavutil/opt.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> } using namespace std; #define STREAM_FRAME_RATE 25 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P int64_t last_pts = 0; int64_t last_dts = 0; int64_t seek_target = -1; int64_t seek_stream_index = -1; int64_t seek_flags = AVSEEK_FLAG_ANY; AVFormatContext *input_fmt_ctx = NULL; AVCodecContext *input_codec_ctx = NULL; int64_t input_start_time = AV_NOPTS_VALUE; int64_t input_duration = AV_NOPTS_VALUE; AVFormatContext *output_fmt_ctx = NULL; int open_input_file(const char *filename) { int ret = 0; AVCodec *input_codec = NULL; if ((ret = avformat_open_input(&input_fmt_ctx, filename, NULL, NULL)) < 0) { fprintf(stderr, "Could not open input file '%s'", filename); goto end; } if ((ret = avformat_find_stream_info(input_fmt_ctx, NULL)) < 0) { fprintf(stderr, "Failed to retrieve input stream information"); goto end; } av_dump_format(input_fmt_ctx, 0, filename, 0); for (int i = 0; i < input_fmt_ctx->nb_streams; i++) { AVStream *stream = input_fmt_ctx->streams[i]; if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { input_codec_ctx = avcodec_alloc_context3(NULL); if (!input_codec_ctx) { fprintf(stderr, "Failed to allocate codec context"); ret = AVERROR(ENOMEM); goto end; } if ((ret = avcodec_parameters_to_context(input_codec_ctx, stream->codecpar)) < 0) { fprintf(stderr, "Failed to copy codec parameters to codec context"); goto end; } input_codec = avcodec_find_decoder(input_codec_ctx->codec_id); if (!input_codec) { fprintf(stderr, "Failed to find decoder for codec ID %d", input_codec_ctx->codec_id); ret = AVERROR_DECODER_NOT_FOUND; goto end; } if ((ret = avcodec_open2(input_codec_ctx, input_codec, NULL)) < 0) { fprintf(stderr, "Failed to open codec"); goto end; } input_start_time = stream->start_time != AV_NOPTS_VALUE ? stream->start_time : 0; input_duration = stream->duration; break; } } if (!input_codec_ctx) { fprintf(stderr, "Failed to find video stream"); ret = AVERROR_INVALIDDATA; goto end; } end: if (ret < 0) { avcodec_free_context(&input_codec_ctx); avformat_close_input(&input_fmt_ctx); } return ret; } int open_output_file(const char *filename) { int ret = 0; if ((ret = avformat_alloc_output_context2(&output_fmt_ctx, NULL, NULL, filename)) < 0) { fprintf(stderr, "Could not create output context"); goto end; } AVCodec *output_codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!output_codec) { fprintf(stderr, "Failed to find encoder"); ret = AVERROR_ENCODER_NOT_FOUND; goto end; } AVStream *stream = avformat_new_stream(output_fmt_ctx, output_codec); if (!stream) { fprintf(stderr, "Failed to create new stream"); ret = AVERROR(ENOMEM); goto end; } AVCodecContext *output_codec_ctx = avcodec_alloc_context3(output_codec); if (!output_codec_ctx) { fprintf(stderr, "Failed to allocate codec context"); ret = AVERROR(ENOMEM); goto end; } output_codec_ctx->codec_id = AV_CODEC_ID_H264; output_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; output_codec_ctx->pix_fmt = STREAM_PIX_FMT; output_codec_ctx->width = input_codec_ctx->width; output_codec_ctx->height = input_codec_ctx->height; output_codec_ctx->time_base = av_make_q(1, STREAM_FRAME_RATE); stream->time_base = output_codec_ctx->time_base; if ((ret = avcodec_open2(output_codec_ctx, output_codec, NULL)) < 0) { fprintf(stderr, "Failed to open codec"); goto end; } if ((ret = avcodec_parameters_from_context(stream->codecpar, output_codec_ctx)) < 0) { fprintf(stderr, "Failed to copy codec context to codec parameters"); goto end; } av_dump_format(output_fmt_ctx, 0, filename, 1); if (!(output_fmt_ctx->oformat->flags & AVFMT_NOFILE)) { if ((ret = avio_open(&output_fmt_ctx->pb, filename, AVIO_FLAG_WRITE)) < 0) { fprintf(stderr, "Could not open output file '%s'", filename); goto end; } } if ((ret = avformat_write_header(output_fmt_ctx, NULL)) < 0) { fprintf(stderr, "Failed to write header"); goto end; } end: if (ret < 0) { avcodec_free_context(&output_codec_ctx); avformat_free_context(output_fmt_ctx); } return ret; } int process_packet(AVPacket *pkt) { int ret = 0; AVFrame *frame = NULL; AVPacket output_pkt = { 0 }; if ((ret = avcodec_send_packet(input_codec_ctx, pkt)) < 0) { fprintf(stderr, "Error sending a packet for decoding"); goto end; } while (ret >= 0) { frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Failed to allocate frame"); ret = AVERROR(ENOMEM); goto end; } ret = avcodec_receive_frame(input_codec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { av_frame_free(&frame); break; } else if (ret < 0) { fprintf(stderr, "Error during decoding"); goto end; } frame->pts = av_rescale_q(frame->pts - input_start_time, input_codec_ctx->time_base, av_make_q(1, STREAM_FRAME_RATE)); frame->pkt_dts = av_rescale_q(frame->pkt_dts - input_start_time, input_codec_ctx->time_base, av_make_q(1, STREAM_FRAME_RATE)); frame->pkt_duration = av_rescale_q(frame->pkt_duration, input_codec_ctx->time_base, av_make_q(1, STREAM_FRAME_RATE)); if (last_pts && frame->pts < last_pts) { fprintf(stderr, "Error: input file timestamps are not monotonic"); ret = AVERROR_INVALIDDATA; goto end; } if (!last_pts) { last_pts = frame->pts; last_dts = frame->pkt_dts; } av_init_packet(&output_pkt); output_pkt.data = NULL; output_pkt.size = 0; frame->pts -= last_pts; frame->pkt_dts -= last_dts; if ((ret = avcodec_send_frame(output_fmt_ctx->streams[0]->codec, frame)) < 0) { fprintf(stderr, "Error sending a frame for encoding"); goto end; } while (ret >= 0) { ret = avcodec_receive_packet(output_fmt_ctx->streams[0]->codec, &output_pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error during encoding"); goto end; } output_pkt.stream_index = 0; av_packet_rescale_ts(&output_pkt, output_fmt_ctx->streams[0]->codec->time_base, output_fmt_ctx->streams[0]->time_base); output_pkt.pts += last_pts; output_pkt.dts += last_dts; if ((ret = av_interleaved_write_frame(output_fmt_ctx, &output_pkt)) < 0) { fprintf(stderr, "Error writing packet"); goto end; } av_packet_unref(&output_pkt); } av_frame_free(&frame); } end: av_packet_unref(pkt); return ret; } int process_seek() { int ret = 0; if (seek_target < 0 || seek_stream_index < 0) { return 0; } if ((ret = avformat_seek_file(input_fmt_ctx, seek_stream_index, INT64_MIN, seek_target, seek_target, seek_flags)) < 0) { fprintf(stderr, "Error seeking to timestamp %" PRId64 "\n", seek_target); goto end; } avcodec_flush_buffers(input_codec_ctx); last_pts = 0; last_dts = 0; end: seek_target = -1; seek_stream_index = -1; seek_flags = AVSEEK_FLAG_ANY; return ret; } int main(int argc, char *argv[]) { int ret = 0; AVPacket pkt = { 0 }; if (argc != 3) { fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]); return 1; } if ((ret = open_input_file(argv[1])) < 0) { goto end; } if ((ret = open_output_file(argv[2])) < 0) { goto end; } while (true) { if ((ret = av_read_frame(input_fmt_ctx, &pkt)) < 0) { if (ret == AVERROR_EOF) { break; } else { fprintf(stderr, "Error reading packet"); goto end; } } if (pkt.stream_index != 0) { av_packet_unref(&pkt); continue; } if (seek_target >= 0 && pkt.dts >= seek_target) { process_seek(); } if ((ret = process_packet(&pkt)) < 0) { goto end; } } if ((ret = av_write_trailer(output_fmt_ctx)) < 0) { fprintf(stderr, "Failed to write trailer"); goto end; } end: av_packet_unref(&pkt); avcodec_free_context(&input_codec_ctx); avformat_close_input(&input_fmt_ctx); avformat_free_context(output_fmt_ctx); return ret; } ``` 该代码的主要思路是:先打开输入文件并处理视频流,然后打开输出文件并处理视频编码,最后循环读取输入文件的数据包并将其存储到输出文件中。在循环过程中,可以根据需要跳转到指定的时间戳。

相关推荐

最新推荐

recommend-type

java使用FFmpeg合成视频和音频并获取视频中的音频等操作(实例代码详解)

主要介绍了java使用FFmpeg合成视频和音频并获取视频中的音频等操作,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Java使用FFmpeg处理视频文件的方法教程

主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
recommend-type

使用Java和ffmpeg把音频和视频合成视频的操作方法

主要介绍了使用Java和ffmpeg把音频和视频合成视频,本文通过实例代码给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

python调用系统ffmpeg实现视频截图、http发送

主要为大家详细介绍了python调用系统ffmpeg实现视频截图、http发送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++实现视频流转换为图片方式

今天小编就为大家分享一篇C++实现视频流转换为图片方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。