当然可以,这里提供一个简单的 C++ 代码示例,使用 FFmpeg 库来实现推流直播:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <chrono>
#include <thread>
#include <mutex>
#include <atomic>
#include <condition_variable>
extern "C" {
#include <libavutil/opt.h>
#include <libavutil/common.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
#define INBUF_SIZE 4096
#define OUTBUF_SIZE 4096
#define VIDEO_SRC_FILE "your_video_file.mp4" // 视频源文件
#define RTMP_DEST_URL "rtmp://your_rtmp_server_address/live/stream" // RTMP 服务器地址
// 全局变量声明
AVFormatContext *inputFormatCtx = nullptr;
AVCodecContext *inputCodecCtx = nullptr;
AVStream *inputStream = nullptr;
AVFormatContext *outputFormatCtx = nullptr;
AVCodecContext *outputCodecCtx = nullptr;
AVStream *outputStream = nullptr;
int64_t nextPts = 0;
bool stopFlag = false;
std::mutex mutex;
std::condition_variable condVar;
// 命令行参数解析
bool parseArgs(int argc, char **argv)
{
bool result = true;
// TODO: 解析参数或设置默认值
return result;
}
// 初始化 FFmpeg 库
bool ffmpegInit()
{
av_register_all();
avformat_network_init();
// TODO: 根据实际情况初始化 FFmpeg 库的其他部分
return true;
}
// 打开输入文件
bool openInputFile(const char *filename)
{
int ret = avformat_open_input(&inputFormatCtx, filename, nullptr, nullptr);
if (ret < 0) {
std::cerr << "Failed to open input file: " << av_err2str(ret) << std::endl;
return false;
}
ret = avformat_find_stream_info(inputFormatCtx, nullptr);
if (ret < 0) {
std::cerr << "Failed to find input stream info: " << av_err2str(ret) << std::endl;
return false;
}
// 查找视频流
for (unsigned int i = 0; i < inputFormatCtx->nb_streams; i++) {
if (inputFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
inputStream = inputFormatCtx->streams[i];
break;
}
}
if (!inputStream) {
std::cerr << "Failed to find video stream in input file" << std::endl;
return false;
}
// 根据视频流的编解码参数找到对应的解码器
AVCodec *codec = avcodec_find_decoder(inputStream->codecpar->codec_id);
if (!codec) {
std::cerr << "Failed to find codec for input video stream" << std::endl;
return false;
}
inputCodecCtx = avcodec_alloc_context3(codec);
if (!inputCodecCtx) {
std::cerr << "Failed to allocate codec context for input video stream" << std::endl;
return false;
}
ret = avcodec_parameters_to_context(inputCodecCtx, inputStream->codecpar);
if (ret < 0) {
std::cerr << "Failed to copy codec parameters to input codec context: " << av_err2str(ret) << std::endl;
return false;
}
ret = avcodec_open2(inputCodecCtx, codec, nullptr);
if (ret < 0) {
std::cerr << "Failed to open codec for input video stream: " << av_err2str(ret) << std::endl;
return false;
}
return true;
}
// 打开输出文件
bool openOutputFile(const char *filename)
{
int ret;
// 根据输出文件名的后缀名自动选择输出格式
const char *formatName = av_guess_format(nullptr, filename, nullptr);
if (!formatName) {
std::cerr << "Failed to guess output format from file name" << std::endl;
return false;
}
outputFormatCtx = avformat_alloc_context();
if (!outputFormatCtx) {
std::cerr << "Failed to allocate output format context" << std::endl;
return false;
}
// 根据选择的输出格式创建输出流
outputFormatCtx->oformat = av_guess_format(formatName, nullptr, nullptr);
if (!outputFormatCtx->oformat) {
std::cerr << "Failed to guess output format" << std::endl;
return false;
}
// 新建视频流并设置编码参数
AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec) {
std::cerr << "Failed to find encoder for output video stream" << std::endl;
return false;
}
outputStream = avformat_new_stream(outputFormatCtx, codec);
if (!outputStream) {
std::cerr << "Failed to allocate output video stream" << std::endl;
return false;
}
outputCodecCtx = avcodec_alloc_context3(codec);
if (!outputCodecCtx) {
std::cerr << "Failed to allocate codec context for output video stream" << std::endl;
return false;
}
outputCodecCtx->codec_id = codec->id;
outputCodecCtx->width = inputCodecCtx->width;
outputCodecCtx->height = inputCodecCtx->height;
outputCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
outputCodecCtx->time_base = inputStream->time_base;
outputStream->time_base = inputStream->time_base;
ret = avcodec_open2(outputCodecCtx, codec, nullptr);
if (ret < 0) {
std::cerr << "Failed to open codec for output video stream: " << av_err2str(ret) << std::endl;
return false;
}
ret = avcodec_parameters_from_context(outputStream->codecpar, outputCodecCtx);
if (ret < 0) {
std::cerr << "Failed to copy codec parameters to output codec context: " << av_err2str(ret) << std::endl;
return false;
}
// 打开输出文件并写入头部信息
ret = avio_open(&outputFormatCtx->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0) {
std::cerr << "Failed to open output file: " << av_err2str(ret) << std::endl;
return false;
}
ret = avformat_write_header(outputFormatCtx, nullptr);
if (ret < 0) {
std::cerr << "Failed to write output file header: " << av_err2str(ret) << std::endl;
return false;
}
return true;
}
// H.264 编码
bool encodeH264(AVFrame *frame)
{
int ret;
// 编码时间戳
frame->pts = av_rescale_q(nextPts++, inputStream->time_base, outputStream->time_base);
// 送入编码器
ret = avcodec_send_frame(outputCodecCtx, frame);
if (ret < 0) {
std::cerr << "Failed to send frame to H.264 encoder: " << av_err2str(ret) << std::endl;
return false;
}
// 获取编码后的数据,写入输出文件
while (true) {
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = nullptr;
pkt.size = 0;
ret = avcodec_receive_packet(outputCodecCtx, &pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
std::cerr << "Failed to receive packet from H.264 encoder: " << av_err2str(ret) << std::endl;
return false;
}
// 发送到 RTMP 服务器
ret = av_write_frame(outputFormatCtx, &pkt);
if (ret < 0) {
std::cerr << "Failed to write packet to RTMP server: " << av_err2str(ret) << std::endl;
av_packet_unref(&pkt);
return false;
}
av_packet_unref(&pkt);
}
return true;
}
// 从输入文件中读取视频帧
bool readFrameFromInput(AVFrame *frame)
{
int ret;
while (true) {
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = nullptr;
pkt.size = 0;
ret = av_read_frame(inputFormatCtx, &pkt);
if (ret == AVERROR_EOF) {
std::cout << "End of input file" << std::endl;
return false;
} else if (ret < 0) {
std::cerr << "Failed to read frame from input file: " << av_err2str(ret) << std::endl;
return false;
}
if (pkt.stream_index == inputStream->index) {
ret = avcodec_send_packet(inputCodecCtx, &pkt);
if (ret < 0) {
std::cerr << "Failed to send packet to input video decoder: " << av_err2str(ret) << std::endl;
av_packet_unref(&pkt);
return false;
}
while (true) {
ret = avcodec_receive_frame(inputCodecCtx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
std::cerr << "Failed to receive frame from input video decoder: " << av_err2str(ret) << std::endl;
av_packet_unref(&pkt);
return false;
}
// 将解码后的数据送入 H.264 编码器进行编码
encodeH264(frame);
}
}
av_packet_unref(&pkt);
}
return true;
}
// 释放资源
void cleanup()
{
std::unique_lock<std::mutex> lock(mutex);
stopFlag = true;
lock.unlock();
condVar.notify_all();
if (inputCodecCtx) {
avcodec_free_context(&inputCodecCtx);
inputCodecCtx = nullptr;
}
if (inputFormatCtx) {
avformat_close_input(&inputFormatCtx);
inputFormatCtx = nullptr;
}
if (outputCodecCtx) {
avcodec_free_context(&outputCodecCtx);
outputCodecCtx = nullptr;
}
if (outputFormatCtx) {
av_write_trailer(outputFormatCtx);
avio_close(outputFormatCtx->pb);
avformat_free_context(outputFormatCtx);
outputFormatCtx = nullptr;
}
avformat_network_deinit();
}
// 主函数
int main(int argc, char **argv)
{
int ret = EXIT_SUCCESS;
// 解析命令行参数
if (!parseArgs(argc, argv)) {
std::cerr << "Failed to parse command line arguments" << std::endl;
return EXIT_FAILURE;
}
// 初始化 FFmpeg 库
if (!ffmpegInit()) {
std::cerr << "Failed to initialize FFmpeg library" << std::endl;
return EXIT_FAILURE;
}
// 打开输入文件
if (!openInputFile(VIDEO_SRC_FILE)) {
std::cerr << "Failed to open input file" << std::endl;
ret = EXIT_FAILURE;
goto cleanup;
}
// 打开输出文件
if (!open