前言
本文介绍在 Linux 环境下使用 C++ 进行 FFmpeg 音视频解码、视频推流的完整开发流程。内容涵盖从 FFmpeg 开发环境搭建(源码编译)、核心结构体拆解、解码完整流程到推流完整流程,以及解码 + 推流一体化实战。
本文介绍在 Linux 环境下使用 C++ 进行 FFmpeg 音视频解码与 RTMP 推流的完整开发流程。内容包括环境搭建(源码编译)、核心结构体解析、解码实战代码、推流实战代码以及解码推流一体化实现。重点讲解资源管理、错误处理及时间戳同步等关键问题,并提供常见编译报错与内存泄漏的解决方案。

本文介绍在 Linux 环境下使用 C++ 进行 FFmpeg 音视频解码、视频推流的完整开发流程。内容涵盖从 FFmpeg 开发环境搭建(源码编译)、核心结构体拆解、解码完整流程到推流完整流程,以及解码 + 推流一体化实战。
所有代码均为可运行版本,重点讲解资源管理、错误处理及内存泄漏规避等 C++ 开发核心痛点。详细拆解 FFmpeg 核心概念(时间基、AVPacket/AVFrame、编码器上下文),覆盖 RTMP 推流、RTSP 推流,以及硬解码/软解码、推流卡顿优化等进阶点。
Linux 默认 apt install ffmpeg 只装命令行工具,缺少开发头文件(*.h)和静态/动态库(*.so),必须源码编译安装完整开发版。
# 更新软件源
sudo apt update
# 安装编译依赖(缺一不可)
sudo apt install -y build-essential cmake git libssl-dev libx264-dev libx265-dev libmp3lame-dev libfdk-aac-dev libsdl2-dev libavutil-dev libavformat-dev libavcodec-dev libswscale-dev libavfilter-dev
build-essential:gcc/g++编译工具;libx264/libx265-dev:H.264/H.265 编码器依赖;libmp3lame/libfdk-aac-dev:音频编码器依赖;libssl-dev:RTMP/HTTPS 推流依赖(openssl)。# 下载 FFmpeg 源码(选 5.x/6.x 稳定版,推荐 6.0)
git clone --depth 1 --branch n6.0 https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
# 配置编译选项(关键:启用开发库、编码器、RTMP)
./configure \
--prefix=/usr/local/ffmpeg \
--enable-shared \
--enable-static \
--enable-gpl \
--enable-libx264 \
--enable-libx265 \
--enable-libmp3lame \
--enable-libfdk-aac \
--enable-avformat \
--enable-avcodec \
--enable-avutil \
--enable-network \
--enable-protocol=rtmp \
--enable-protocol=rtsp \
--enable-swscale \
--disable-doc \
--disable-ffplay \
--disable-ffprobe
# 编译(-j 后接 CPU 核心数,比如 4 核写-j4,加快编译)
make -j$(nproc)
# 安装到指定路径
sudo make install
# 编辑环境变量文件
sudo vim /etc/profile
# 在文件末尾添加以下内容
export PATH=/usr/local/ffmpeg/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH
# 生效环境变量
source /etc/profile
# 1. 检查头文件是否存在
ls /usr/local/ffmpeg/include/libavcodec/avcodec.h
# 2. 检查库文件是否存在
ls /usr/local/ffmpeg/lib/libavcodec.so
# 3. 检查 pkg-config(编译时链接用)
pkg-config --libs libavformat libavcodec libavutil
FFmpeg 开发的核心是操作一系列结构体,先拆解最关键的概念和流程,避免代码写了却不懂逻辑。
| 结构体 | 大白话含义 | 核心作用 |
|---|---|---|
AVFormatContext | 格式上下文 | 管理音视频文件/流的全局信息(输入/输出),比如文件名、流数量、时长 |
AVCodecContext | 编解码器上下文 | 管理编解码的核心参数(编码器/解码器类型、码率、分辨率、采样率等) |
AVCodec | 编解码器 | 具体的编解码器实例(比如 H.264 解码器、AAC 编码器) |
AVStream | 流信息 | 描述音频/视频流的属性(时间基、码率、编解码器参数) |
AVPacket | 压缩数据包 | 存储编码后的音视频数据(一帧/多帧压缩数据),解码的输入/推流的输出 |
AVFrame | 原始帧数据 | 存储解码后的原始数据(视频:YUV 像素;音频:PCM 采样),解码的输出/编码的输入 |
AVDictionary | 参数字典 | 传递额外参数(比如推流的超时时间、编码器参数) |
av_register_all / avformat_network_initavformat_open_inputavformat_find_stream_infoAVStream,区分视频/音频avcodec_find_decoderavcodec_open2av_read_frameavcodec_send_packetavcodec_receive_frameav_frame_unref / av_packet_unrefavcodec_close / avformat_close_inputavformat_network_initavformat_alloc_output_context2avformat_new_streamavcodec_open2avformat_write_headeravcodec_send_frame / avcodec_receive_packetav_packet_rescale_tsav_interleaved_write_frameav_write_traileravcodec_close / avformat_free_contextav_strerror(int errnum, char *errbuf, size_t errbuf_size) → 把 FFmpeg 错误码转成可读字符串(Linux 开发必用);av_free / av_unref / av_close → 避免内存泄漏(C++ 中配合智能指针更佳);av_rescale_q → FFmpeg 时间基是分数,需转换为统一单位(比如毫秒)。这是基础中的基础,先实现从 MP4 文件解码出原始 YUV(视频)和 PCM(音频),代码逐行注释,可直接编译运行。
#include <iostream>
#include <cstdio>
#include <cstring>
// FFmpeg 开发头文件(Linux 下必须包含)
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
}
// 错误处理函数(Linux 下封装,方便复用)
void print_ffmpeg_error(int errnum, const char* func_name) {
char errbuf[1024] = {0};
av_strerror(errnum, errbuf, sizeof(errbuf));
std::cerr << "FFmpeg 错误 [" << func_name << "]:" << errbuf << " (错误码:" << errnum << ")" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "用法:./decode_video_audio 输入文件.mp4" << std::endl;
return -1;
}
const char* input_file = argv[1];
// ===================== 1. 初始化 FFmpeg(Linux 下必须) =====================
av_register_all(); // 注册所有封装格式(旧版本需要,新版本可省略,但建议加)
avformat_network_init(); // 初始化网络(解码本地文件可省略,推流必须)
// ===================== 2. 打开输入文件,创建格式上下文 =====================
AVFormatContext* fmt_ctx = nullptr;
int ret = avformat_open_input(&fmt_ctx, input_file, nullptr, nullptr);
if (ret < 0) {
print_ffmpeg_error(ret, "avformat_open_input");
return -1;
}
// ===================== 3. 获取流信息(关键!否则无法找到解码器) =====================
ret = avformat_find_stream_info(fmt_ctx, nullptr);
if (ret < 0) {
print_ffmpeg_error(ret, "avformat_find_stream_info");
avformat_close_input(&fmt_ctx);
return -1;
}
// 打印输入文件信息(Linux 下调试用)
av_dump_format(fmt_ctx, 0, input_file, 0);
// ===================== 4. 查找视频流和音频流索引 =====================
int video_stream_idx = -1, audio_stream_idx = -1;
AVCodecParameters* video_codec_par = nullptr;
AVCodecParameters* audio_codec_par = nullptr;
for (int i = 0; i < fmt_ctx->nb_streams; i++) {
AVStream* stream = fmt_ctx->streams[i];
if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_stream_idx = i;
video_codec_par = stream->codecpar;
std::cout << "找到视频流,索引:" << i << ",分辨率:" << video_codec_par->width << "x" << video_codec_par->height << std::endl;
} else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_idx = i;
audio_codec_par = stream->codecpar;
std::cout << "找到音频流,索引:" << i << ",采样率:" << audio_codec_par->sample_rate << "Hz" << std::endl;
}
}
if (video_stream_idx == -1 && audio_stream_idx == -1) {
std::cerr << "未找到音视频流!" << std::endl;
avformat_close_input(&fmt_ctx);
return -1;
}
// ===================== 5. 初始化视频解码器 =====================
AVCodecContext* video_dec_ctx = nullptr;
if (video_stream_idx != -1) {
const AVCodec* video_dec = avcodec_find_decoder(video_codec_par->codec_id);
if (!video_dec) {
std::cerr << "找不到视频解码器!" << std::endl;
avformat_close_input(&fmt_ctx);
return -1;
}
video_dec_ctx = avcodec_alloc_context3(video_dec);
if (!video_dec_ctx) {
std::cerr << "分配视频解码器上下文失败!" << std::endl;
avformat_close_input(&fmt_ctx);
return -1;
}
ret = avcodec_parameters_to_context(video_dec_ctx, video_codec_par);
if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_parameters_to_context");
avcodec_free_context(&video_dec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
ret = avcodec_open2(video_dec_ctx, video_dec, nullptr);
if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_open2");
avcodec_free_context(&video_dec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
}
// ===================== 6. 初始化音频解码器(和视频类似,简化版) =====================
AVCodecContext* audio_dec_ctx = nullptr;
if (audio_stream_idx != -1) {
const AVCodec* audio_dec = avcodec_find_decoder(audio_codec_par->codec_id);
if (!audio_dec) {
std::cerr << "找不到音频解码器!" << std::endl;
avcodec_free_context(&video_dec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
audio_dec_ctx = avcodec_alloc_context3(audio_dec);
ret = avcodec_parameters_to_context(audio_dec_ctx, audio_codec_par);
ret = avcodec_open2(audio_dec_ctx, audio_dec, nullptr);
if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_open2(audio)");
avcodec_free_context(&audio_dec_ctx);
avcodec_free_context(&video_dec_ctx);
avformat_close_input(&fmt_ctx);
return -1;
}
}
// ===================== 7. 循环解码音视频包 =====================
AVPacket* pkt = av_packet_alloc();
AVFrame* frame = av_frame_alloc();
if (!pkt || !frame) {
std::cerr << "分配 pkt/frame 失败!" << std::endl;
goto clean_up;
}
FILE* video_out = nullptr;
FILE* audio_out = nullptr;
if (video_stream_idx != -1) {
video_out = fopen("output.yuv", "wb");
if (!video_out) {
std::cerr << "打开 output.yuv 失败!" << std::endl;
goto clean_up;
}
}
if (audio_stream_idx != -1) {
audio_out = fopen("output.pcm", "wb");
if (!audio_out) {
std::cerr << "打开 output.pcm 失败!" << std::endl;
goto clean_up;
}
}
while (av_read_frame(fmt_ctx, pkt) >= 0) {
if (pkt->stream_index == video_stream_idx) {
// 解码视频包
ret = avcodec_send_packet(video_dec_ctx, pkt);
if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_send_packet(video)");
av_packet_unref(pkt);
continue;
}
while (ret >= 0) {
ret = avcodec_receive_frame(video_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
else if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_receive_frame(video)");
goto clean_up;
}
// 写入 YUV 数据到文件(Linux 下二进制写入)
int y_size = frame->width * frame->height;
fwrite(frame->data[0], 1, y_size, video_out); // Y 分量
fwrite(frame->data[1], 1, y_size / 4, video_out); // U 分量
fwrite(frame->data[2], 1, y_size / 4, video_out); // V 分量
std::cout << "解码视频帧:pts=" << frame->pts << std::endl;
av_frame_unref(frame); // 释放帧引用(避免内存泄漏)
}
} else if (pkt->stream_index == audio_stream_idx) {
// 解码音频包(简化版)
ret = avcodec_send_packet(audio_dec_ctx, pkt);
if (ret < 0) continue;
while (ret >= 0) {
ret = avcodec_receive_frame(audio_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
// 写入 PCM 数据到文件
int pcm_size = av_samples_get_buffer_size(nullptr, frame->channels, frame->nb_samples, (AVSampleFormat)frame->format, 1);
fwrite(frame->data[0], 1, pcm_size, audio_out);
av_frame_unref(frame);
}
}
av_packet_unref(pkt); // 释放包引用
}
// ===================== 8. 资源清理 =====================
clean_up:
if (video_out) fclose(video_out);
if (audio_out) fclose(audio_out);
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&video_dec_ctx);
avcodec_free_context(&audio_dec_ctx);
avformat_close_input(&fmt_ctx);
avformat_network_deinit();
std::cout << "解码完成!输出文件:output.yuv(视频)、output.pcm(音频)" << std::endl;
return 0;
}
g++ -std=c++11 decode_video_audio.cpp -o decode_video_audio $(pkg-config --cflags --libs libavformat libavcodec libavutil) -lm -lpthread
# Linux 下必须链接数学库和线程库
# 运行解码程序(替换为你的 MP4 文件)
./decode_video_audio test.mp4
# 验证 YUV 文件(用 ffplay 播放,需装 ffplay)
ffplay -pix_fmt yuv420p -s 1920x1080 output.yuv
# 替换为实际分辨率
# 验证 PCM 文件
ffplay -f s16le -ar 44100 -ac 2 output.pcm
# 替换为实际采样率/声道数
以 RTMP 推流为例(比如推到 Nginx-RTMP、抖音/快手推流地址),实现从本地 YUV 文件推流(也可结合上面的解码,推解码后的视频)。
如果没有公网推流地址,先在 Linux 搭 Nginx-RTMP 服务器:
# 安装 Nginx 依赖
sudo apt install -y libpcre3 libpcre3-dev libssl-dev
# 下载 nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
# 下载 Nginx 源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 配置编译选项
./configure --add-module=../nginx-rtmp-module --with-http_ssl_module
# 编译安装
make -j$(nproc)
sudo make install
# 配置 RTMP(编辑 nginx.conf)
sudo vim /usr/local/nginx/conf/nginx.conf
# 在文件末尾添加 RTMP 配置
rtmp {
server {
listen 1935; # RTMP 默认端口
chunk_size 4096;
application live {
live on; # 开启直播
record off; # 不录制
allow publish 127.0.0.1; # 允许本地推流
deny publish all;
allow play all; # 允许所有播放
}
}
}
# 启动 Nginx
sudo /usr/local/nginx/sbin/nginx
# 验证 Nginx 是否运行
ps -ef | grep nginx
# 有进程则 OK
#include <iostream>
#include <cstdio>
#include <cstring>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <libavutil/time.h>
}
// 错误处理函数(复用)
void print_ffmpeg_error(int errnum, const char* func_name) {
char errbuf[1024] = {0};
av_strerror(errnum, errbuf, sizeof(errbuf));
std::cerr << "FFmpeg 错误 [" << func_name << "]:" << errbuf << " (错误码:" << errnum << ")" << std::endl;
}
// 读取 YUV 帧数据(从文件)
int read_yuv_frame(AVFrame* frame, FILE* yuv_file, int width, int height) {
int y_size = width * height;
// 读取 YUV420P 数据
if (fread(frame->data[0], 1, y_size, yuv_file) != y_size) return -1;
if (fread(frame->data[1], 1, y_size / 4, yuv_file) != y_size / 4) return -1;
if (fread(frame->data[2], 1, y_size / 4, yuv_file) != y_size / 4) return -1;
return 0;
}
int main(int argc, char* argv[]) {
if (argc < 4) {
std::cerr << "用法:./push_rtmp YUV 文件 宽度 高度 RTMP 地址" << std::endl;
std::cerr << "示例:./push_rtmp output.yuv 1920 1080 rtmp://127.0.0.1:1935/live/test" << std::endl;
return -1;
}
const char* yuv_file = argv[1];
int width = atoi(argv[2]);
int height = atoi(argv[3]);
const char* rtmp_url = argv[4];
const int fps = 25; // 帧率(可根据需求调整)
// ===================== 1. 初始化 FFmpeg =====================
av_register_all();
avformat_network_init();
// ===================== 2. 创建输出格式上下文(RTMP) =====================
AVFormatContext* fmt_ctx = nullptr;
int ret = avformat_alloc_output_context2(&fmt_ctx, nullptr, "flv", rtmp_url);
if (ret < 0) {
print_ffmpeg_error(ret, "avformat_alloc_output_context2");
return -1;
}
// ===================== 3. 添加视频流 =====================
AVStream* video_stream = avformat_new_stream(fmt_ctx, nullptr);
if (!video_stream) {
std::cerr << "创建视频流失败!" << std::endl;
avformat_free_context(fmt_ctx);
return -1;
}
// ===================== 4. 配置编码器参数 =====================
AVCodecParameters* codec_par = video_stream->codecpar;
codec_par->codec_id = AV_CODEC_ID_H264; // H.264 编码器
codec_par->codec_type = AVMEDIA_TYPE_VIDEO;
codec_par->width = width;
codec_par->height = height;
codec_par->format = AV_PIX_FMT_YUV420P; // YUV420P 输入
codec_par->bit_rate = 2000000; // 码率 2Mbps
video_stream->time_base = av_make_q(1, fps); // 时间基:1/25
codec_par->framerate = av_make_q(fps, 1); // 帧率 25fps
// ===================== 5. 查找并打开编码器 =====================
const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec) {
std::cerr << "找不到 H.264 编码器!" << std::endl;
avformat_free_context(fmt_ctx);
return -1;
}
AVCodecContext* codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
std::cerr << "分配编码器上下文失败!" << std::endl;
avformat_free_context(fmt_ctx);
return -1;
}
ret = avcodec_parameters_to_context(codec_ctx, codec_par);
if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_parameters_to_context");
avcodec_free_context(&codec_ctx);
avformat_free_context(fmt_ctx);
return -1;
}
// 设置编码器额外参数(Linux 下关键)
codec_ctx->time_base = av_make_q(1, fps);
codec_ctx->gop_size = 10; // 每 10 帧一个 I 帧
codec_ctx->max_b_frames = 1; // B 帧数量
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
// 打开编码器(设置 H.264 编码参数,比如预设)
AVDictionary* opts = nullptr;
av_dict_set(&opts, "preset", "fast", 0); // 编码速度:fast(平衡速度和质量)
av_dict_set(&opts, "tune", "zerolatency", 0); // 低延迟(推流必备)
ret = avcodec_open2(codec_ctx, codec, &opts);
if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_open2");
avcodec_free_context(&codec_ctx);
avformat_free_context(fmt_ctx);
return -1;
}
av_dict_free(&opts); // 释放参数字典
// ===================== 6. 打开 RTMP 输出 URL =====================
if (!(fmt_ctx->oformat->flags & AVFMT_NOFILE)) {
ret = avio_open(&fmt_ctx->pb, rtmp_url, AVIO_FLAG_WRITE);
if (ret < 0) {
print_ffmpeg_error(ret, "avio_open");
avcodec_free_context(&codec_ctx);
avformat_free_context(fmt_ctx);
return -1;
}
}
// ===================== 7. 写入头信息 =====================
ret = avformat_write_header(fmt_ctx, nullptr);
if (ret < 0) {
print_ffmpeg_error(ret, "avformat_write_header");
avio_close(fmt_ctx->pb);
avcodec_free_context(&codec_ctx);
avformat_free_context(fmt_ctx);
return -1;
}
// ===================== 8. 准备 YUV 帧和数据包 =====================
AVFrame* frame = av_frame_alloc();
AVPacket* pkt = av_packet_alloc();
FILE* in_file = fopen(yuv_file, "rb");
if (!frame || !pkt || !in_file) {
std::cerr << "分配资源/打开 YUV 文件失败!" << std::endl;
goto clean_up;
}
// 初始化 YUV 帧
frame->width = width;
frame->height = height;
frame->format = AV_PIX_FMT_YUV420P;
ret = av_frame_get_buffer(frame, 0);
if (ret < 0) {
print_ffmpeg_error(ret, "av_frame_get_buffer");
goto clean_up;
}
// ===================== 9. 循环编码并推流 =====================
int frame_index = 0;
int64_t start_time = av_gettime(); // Linux 下获取时间(微秒)
while (1) {
// 读取 YUV 帧
if (read_yuv_frame(frame, in_file, width, height) < 0) {
std::cout << "YUV 文件读取完毕!" << std::endl;
break;
}
// 设置帧时间戳(关键!否则推流卡顿/不同步)
frame->pts = frame_index++;
av_rescale_q(frame->pts, codec_ctx->time_base, video_stream->time_base);
// 发送帧到编码器
ret = avcodec_send_frame(codec_ctx, frame);
if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_send_frame");
break;
}
// 接收编码后的数据包
while (ret >= 0) {
ret = avcodec_receive_packet(codec_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
else if (ret < 0) {
print_ffmpeg_error(ret, "avcodec_receive_packet");
goto clean_up;
}
// 调整数据包时间戳(适配 RTMP)
av_packet_rescale_ts(pkt, codec_ctx->time_base, video_stream->time_base);
pkt->stream_index = video_stream->index;
// 发送数据包到 RTMP 服务器
ret = av_interleaved_write_frame(fmt_ctx, pkt);
if (ret < 0) {
print_ffmpeg_error(ret, "av_interleaved_write_frame");
av_packet_unref(pkt);
break;
}
av_packet_unref(pkt); // 控制推流帧率(Linux 下延时)
int64_t now_time = av_gettime();
int64_t delay = (frame_index * 1000000 / fps) - (now_time - start_time);
if (delay > 0) {
av_usleep(delay); // 微秒级延时
}
}
av_frame_unref(frame);
}
// ===================== 10. 写入尾信息 =====================
av_write_trailer(fmt_ctx);
// ===================== 11. 资源清理 =====================
clean_up:
if (in_file) fclose(in_file);
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&codec_ctx);
if (fmt_ctx && !(fmt_ctx->oformat->flags & AVFMT_NOFILE)) {
avio_close(fmt_ctx->pb);
}
avformat_free_context(fmt_ctx);
avformat_network_deinit();
std::cout << "推流完成!可通过 RTMP 地址播放:" << rtmp_url << std::endl;
return 0;
}
g++ -std=c++11 push_rtmp.cpp -o push_rtmp $(pkg-config --cflags --libs libavformat libavcodec libavutil) -lm -lpthread
# 1. 运行推流程序(用之前解码出的 output.yuv)
./push_rtmp output.yuv 1920 1080 rtmp://127.0.0.1:1935/live/test
# 2. 播放推流(本地/其他机器)
ffplay rtmp://127.0.0.1:1935/live/test
# 能看到视频则推流成功
把前面的解码和推流结合,实现「解码本地 MP4 文件→实时推流到 RTMP 服务器」,核心是把解码后的 AVFrame 直接传给推流的编码器。
// 解码后的视频帧直接推流
if (pkt->stream_index == video_stream_idx) {
ret = avcodec_send_packet(video_dec_ctx, pkt);
if (ret < 0) continue;
while (ret >= 0) {
ret = avcodec_receive_frame(video_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break;
// 直接把解码后的 frame 传给推流编码器
frame->pts = av_rescale_q(frame->pts, video_dec_ctx->time_base, video_enc_ctx->time_base);
avcodec_send_frame(video_enc_ctx, frame);
// 后续编码 + 推流逻辑和之前一致
av_frame_unref(frame);
}
}
undefined reference to avformat_open_input → ✅ 检查 pkg-config 是否正确,或手动指定库路径:-L/usr/local/ffmpeg/lib -lavformat -lavcodec -lavutil;fatal error: libavformat/avformat.h: 没有那个文件或目录 → ✅ 添加头文件路径:-I/usr/local/ffmpeg/include;error: 'AVCodecContext' has no member named 'codec' → ✅ FFmpeg 新版本(5.x+)移除了 codec 成员,改用 codec_par。Connection refused → ✅ 检查 RTMP 服务器是否运行,端口 1935 是否开放(Linux 防火墙:sudo ufw allow 1935);Invalid argument → ✅ 检查编码器参数(比如分辨率、码率)是否合法,或 RTMP 地址格式错误;zerolatency 参数(低延迟)。av_frame_unref / av_packet_unref 释放帧/包引用;av_alloc / av_new 分配的资源,必须对应 av_free / av_close;valgrind 检测内存泄漏:valgrind --leak-check=full ./push_rtmp output.yuv 1920 1080 rtmp://127.0.0.1:1935/live/test。zerolatency 低延迟参数,调整时间戳,控制帧率;av_strerror 打印错误信息,用 valgrind 检测内存泄漏,用 ffplay 验证数据。
微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML 转 Markdown 互为补充。 在线工具,Markdown 转 HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML 转 Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online