"AVStream" 没有成员 "codec"C/C++(135)
时间: 2023-08-08 12:13:55 浏览: 114
这个错误是由于你的代码中使用了"AVStream"的成员"codec",但是该成员在"AVStream"结构体中并不存在。可能是你使用了过时的代码或者API版本不兼容导致的。你可以检查一下你所使用的库或者头文件的版本,并参考相关文档进行修改。
另外,也可能是你没有正确地包含相应的头文件。请确保你包含了正确的头文件,并且链接了相关的库文件。如果问题仍然存在,你可以提供更多的代码细节,以便我能够更好地帮助你解决这个问题。
相关问题
c/c++使用FFmpeg库推流本地文件,实现跳转和倍速播放的完整代码
以下是使用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 秒开始播放,以两倍速度播放。
c/c++使用ffmpeg库推流本地文件,并使用av_seek_frame实现跳转功能
要使用FFmpeg库进行推流和跳转功能,需要按照以下步骤进行操作:
1. 初始化FFmpeg库
在使用FFmpeg库之前,需要先初始化FFmpeg库。可以使用av_register_all()函数进行初始化。
```c
av_register_all();
```
2. 打开输入文件
使用avformat_open_input()函数打开输入文件,然后使用avformat_find_stream_info()函数查找文件中的流信息。
```c
AVFormatContext *formatCtx = NULL;
avformat_open_input(&formatCtx, inputFile, NULL, NULL);
avformat_find_stream_info(formatCtx, NULL);
```
3. 打开输出文件
使用avformat_alloc_output_context2()函数创建输出文件的AVFormatContext,并使用avio_open()函数打开输出文件。
```c
AVFormatContext *outFormatCtx = NULL;
avformat_alloc_output_context2(&outFormatCtx, NULL, NULL, outputFile);
AVIOContext *outAVIOContext = NULL;
avio_open(&outAVIOContext, outputFile, AVIO_FLAG_WRITE);
outFormatCtx->pb = outAVIOContext;
```
4. 为输出文件添加流
使用avformat_new_stream()函数为输出文件添加音频或视频流,并设置流的编码格式和参数。
```c
AVStream *outStream = avformat_new_stream(outFormatCtx, NULL);
outStream->codecpar->codec_id = codecId;
outStream->codecpar->codec_type = codecType;
outStream->codecpar->width = width;
outStream->codecpar->height = height;
outStream->codecpar->sample_rate = sampleRate;
outStream->codecpar->channels = channels;
outStream->codecpar->format = AV_SAMPLE_FMT_FLTP;
```
5. 打开编码器
使用avcodec_find_encoder()函数查找流的编码器,并使用avcodec_open2()函数打开编码器。
```c
AVCodec *encoder = avcodec_find_encoder(outStream->codecpar->codec_id);
AVCodecContext *encoderCtx = avcodec_alloc_context3(encoder);
avcodec_parameters_to_context(encoderCtx, outStream->codecpar);
avcodec_open2(encoderCtx, encoder, NULL);
```
6. 写入文件头
使用avformat_write_header()函数写入输出文件的文件头。
```c
avformat_write_header(outFormatCtx, NULL);
```
7. 读取和写入数据
使用av_read_frame()函数读取输入文件中的数据,并使用av_write_frame()函数将数据写入输出文件。如果需要跳转到指定时间点,可以使用av_seek_frame()函数进行跳转。
```c
while (av_read_frame(formatCtx, &packet) == 0) {
if (packet.stream_index == streamIndex) {
if (av_seek_frame(formatCtx, streamIndex, timestamp, AVSEEK_FLAG_BACKWARD) >= 0) {
avcodec_flush_buffers(decoderCtx);
continue;
}
avcodec_send_packet(decoderCtx, &packet);
while (avcodec_receive_frame(decoderCtx, frame) == 0) {
// 对音视频数据进行处理
avcodec_send_frame(encoderCtx, frame);
while (avcodec_receive_packet(encoderCtx, &outPacket) == 0) {
outPacket.stream_index = outStream->index;
av_interleaved_write_frame(outFormatCtx, &outPacket);
av_packet_unref(&outPacket);
}
}
}
av_packet_unref(&packet);
}
```
8. 关闭和释放资源
使用av_write_trailer()函数写入输出文件的文件尾,并使用avformat_close_input()、avformat_close_output()等函数关闭输入输出文件,并释放相应的资源。
```c
av_write_trailer(outFormatCtx);
avformat_close_input(&formatCtx);
avformat_close_input(&outFormatCtx);
avcodec_close(decoderCtx);
avcodec_close(encoderCtx);
avformat_free_context(formatCtx);
avformat_free_context(outFormatCtx);
av_frame_free(&frame);
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)