c/c++使用FFmpeg库推流本地文件,实现跳转和倍速播放的完整代码

时间: 2023-12-06 11:44:21 浏览: 29
以下是使用FFmpeg库推流本地文件,并实现跳转和倍速播放的完整代码。 ```c++ #include <iostream> #include <string> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/opt.h> #include <libavutil/time.h> #include <libswresample/swresample.h> #include <libswscale/swscale.h> } using namespace std; int main(int argc, char *argv[]) { if (argc < 5) { cerr << "Usage: " << argv[0] << " input_file output_url seek_time speed\n"; return -1; } const char *input_file = argv[1]; const char *output_url = argv[2]; const double seek_time = atof(argv[3]); const double speed = atof(argv[4]); av_register_all(); AVFormatContext *fmt_ctx = nullptr; if (avformat_open_input(&fmt_ctx, input_file, nullptr, nullptr) != 0) { cerr << "Failed to open input file" << endl; return -1; } if (avformat_find_stream_info(fmt_ctx, nullptr) < 0) { cerr << "Failed to retrieve input stream information" << endl; return -1; } AVStream *stream = nullptr; int stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &stream, 0); if (stream_index < 0) { cerr << "Failed to find video stream" << endl; return -1; } AVCodecParameters *codecpar = stream->codecpar; AVCodec *decoder = avcodec_find_decoder(codecpar->codec_id); if (decoder == nullptr) { cerr << "Failed to find decoder" << endl; return -1; } AVCodecContext *codec_ctx = avcodec_alloc_context3(decoder); if (avcodec_parameters_to_context(codec_ctx, codecpar) < 0) { cerr << "Failed to copy codec parameters to decoder context" << endl; return -1; } if (avcodec_open2(codec_ctx, decoder, nullptr) < 0) { cerr << "Failed to open codec" << endl; return -1; } AVFrame *frame = av_frame_alloc(); if (frame == nullptr) { cerr << "Failed to allocate frame" << endl; return -1; } AVPacket *pkt = av_packet_alloc(); if (pkt == nullptr) { cerr << "Failed to allocate packet" << endl; return -1; } AVDictionary *opts = nullptr; av_dict_set(&opts, "bufsize", "1024000", 0); av_dict_set(&opts, "maxrate", "1024000", 0); AVFormatContext *out_fmt_ctx = nullptr; if (avformat_alloc_output_context2(&out_fmt_ctx, nullptr, "flv", output_url) < 0) { cerr << "Failed to allocate output context" << endl; return -1; } AVStream *out_stream = avformat_new_stream(out_fmt_ctx, nullptr); if (out_stream == nullptr) { cerr << "Failed to create output stream" << endl; return -1; } if (avcodec_parameters_copy(out_stream->codecpar, codecpar) < 0) { cerr << "Failed to copy codec parameters to output stream" << endl; return -1; } if (avio_open(&out_fmt_ctx->pb, output_url, AVIO_FLAG_WRITE) < 0) { cerr << "Failed to open output url" << endl; return -1; } if (avformat_write_header(out_fmt_ctx, &opts) < 0) { cerr << "Failed to write output header" << endl; return -1; } av_dump_format(fmt_ctx, 0, input_file, 0); int64_t seek_target = seek_time * AV_TIME_BASE; if (av_seek_frame(fmt_ctx, stream_index, seek_target, AVSEEK_FLAG_BACKWARD) < 0) { cerr << "Failed to seek to " << seek_time << endl; return -1; } SwsContext *sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P, 0, nullptr, nullptr, nullptr); if (sws_ctx == nullptr) { cerr << "Failed to allocate sws context" << endl; return -1; } AVRational frame_rate = stream->r_frame_rate; AVRational time_base = stream->time_base; AVRational out_time_base = out_stream->time_base; int64_t next_pts = 0; double speed_factor = 1.0 / speed; while (av_read_frame(fmt_ctx, pkt) >= 0) { if (pkt->stream_index != stream_index) { av_packet_unref(pkt); continue; } if (avcodec_send_packet(codec_ctx, pkt) < 0) { cerr << "Failed to send packet to decoder" << endl; return -1; } while (avcodec_receive_frame(codec_ctx, frame) == 0) { double pts = static_cast<double>(frame->pts) * time_base.num / time_base.den; double dts = static_cast<double>(frame->pkt_dts) * time_base.num / time_base.den; double duration = static_cast<double>(frame->pkt_duration) * time_base.num / time_base.den; if (pts < seek_time) { av_frame_unref(frame); continue; } if (duration > 0) { duration *= speed_factor; frame->pkt_duration = static_cast<int64_t>(duration * time_base.den / time_base.num); } if (pts >= next_pts) { int64_t pts_ms = static_cast<int64_t>(pts * 1000); int64_t out_pts = av_rescale_q(pts_ms, {1, 1000}, out_time_base); int64_t delay_ms = static_cast<int64_t>((pts - next_pts) * 1000 * speed_factor); int64_t delay = av_rescale_q(delay_ms, {1, 1000}, out_time_base); av_usleep(delay); AVFrame *out_frame = av_frame_alloc(); if (out_frame == nullptr) { cerr << "Failed to allocate output frame" << endl; return -1; } out_frame->format = AV_PIX_FMT_YUV420P; out_frame->width = codec_ctx->width; out_frame->height = codec_ctx->height; av_frame_get_buffer(out_frame, 0); sws_scale(sws_ctx, frame->data, frame->linesize, 0, codec_ctx->height, out_frame->data, out_frame->linesize); out_frame->pts = out_pts; if (avcodec_send_frame(out_stream->codec, out_frame) < 0) { cerr << "Failed to send frame to encoder" << endl; return -1; } while (avcodec_receive_packet(out_stream->codec, pkt) == 0) { pkt->stream_index = out_stream->index; av_interleaved_write_frame(out_fmt_ctx, pkt); av_packet_unref(pkt); } av_frame_unref(out_frame); next_pts += static_cast<int64_t>(duration * AV_TIME_BASE); } av_frame_unref(frame); } av_packet_unref(pkt); } av_write_trailer(out_fmt_ctx); av_packet_free(&pkt); av_frame_free(&frame); avcodec_free_context(&codec_ctx); avformat_close_input(&fmt_ctx); avio_closep(&out_fmt_ctx->pb); avformat_free_context(out_fmt_ctx); sws_freeContext(sws_ctx); return 0; } ``` 使用方法: ```bash ./push_stream input_file output_url seek_time speed ``` 其中,`input_file` 是输入的本地文件名,`output_url` 是输出的推流地址,`seek_time` 是跳转的时间(单位秒),`speed` 是倍速播放的速度。例如: ```bash ./push_stream input.mp4 rtmp://127.0.0.1/live/test 30 2.0 ``` 表示将 `input.mp4` 推流到 `rtmp://127.0.0.1/live/test`,从第 30 秒开始播放,以两倍速度播放。

相关推荐

最新推荐

recommend-type

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

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

使用 FFmpeg 命令拼接mp3音频文件异常问题及解决方法

主要介绍了使用 FFmpeg 命令拼接mp3音频文件异常问题及解决方法,本文给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

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

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

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

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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

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

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依