C++怎么将海康的视频流数据 转码为flv视频流格式
时间: 2024-05-04 16:19:54 浏览: 139
LINUX版用QT C++调用海康SDK取流和控制云台
5星 · 资源好评率100%
要将海康的视频流数据转码为FLV视频流格式,可以使用FFmpeg库进行实现。以下是一个简单的示例代码,可以将海康、大华等品牌的IPC、NVR等设备的RTSP、RTMP等协议的视频流转换成FLV格式的视频流。
```c++
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/opt.h>
}
using namespace std;
#define STREAM_DURATION 10.0
#define STREAM_FRAME_RATE 25 /* 25 images/s */
#define STREAM_WIDTH 640
#define STREAM_HEIGHT 480
#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
/* exit a program after printing its usage */
static void usage(const char *progname)
{
fprintf(stderr, "Usage: %s [options]\n", progname);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -i | --input <filename> input file name\n");
fprintf(stderr, " -o | --output <filename> output file name\n");
fprintf(stderr, " -h | --help print this message\n");
exit(1);
}
static AVFormatContext *ifmt_ctx;
static AVFormatContext *ofmt_ctx;
static int video_stream_index = -1;
static AVStream *video_stream;
static AVCodecContext *video_dec_ctx;
static AVCodecContext *video_enc_ctx;
static AVFrame *frame;
static AVPacket pkt;
static int64_t start_time = 0;
static int64_t last_pts = 0;
static int open_input_file(const char *filename)
{
int ret;
AVCodec *dec;
ifmt_ctx = NULL;
ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open input file '%s'", filename);
return ret;
}
ret = avformat_find_stream_info(ifmt_ctx, NULL);
if (ret < 0) {
fprintf(stderr, "Could not find stream information");
return ret;
}
/* select the video stream */
ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
if (ret < 0) {
fprintf(stderr, "Could not find video stream in the input file");
return ret;
}
video_stream_index = ret;
video_stream = ifmt_ctx->streams[video_stream_index];
/* allocate a codec context for the decoder */
video_dec_ctx = avcodec_alloc_context3(dec);
if (!video_dec_ctx) {
fprintf(stderr, "Failed to allocate codec context");
return AVERROR(EINVAL);
}
/* copy codec parameters from input stream to output codec context */
ret = avcodec_parameters_to_context(video_dec_ctx, video_stream->codecpar);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parameters to decoder context");
return ret;
}
/* init the video decoder */
ret = avcodec_open2(video_dec_ctx, dec, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to open codec");
return ret;
}
/* allocate a frame for the decoded video */
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Failed to allocate frame");
return AVERROR(ENOMEM);
}
return 0;
}
static int open_output_file(const char *filename)
{
AVCodec *codec;
int ret;
ofmt_ctx = NULL;
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
if (!ofmt_ctx) {
fprintf(stderr, "Could not create output context");
return AVERROR_UNKNOWN;
}
/* find the encoder */
codec = avcodec_find_encoder(AV_CODEC_ID_FLV1);
if (!codec) {
fprintf(stderr, "Could not find encoder");
return AVERROR_UNKNOWN;
}
/* create a new video stream in the output file */
video_stream = avformat_new_stream(ofmt_ctx, codec);
if (!video_stream) {
fprintf(stderr, "Could not create stream");
return AVERROR_UNKNOWN;
}
video_enc_ctx = avcodec_alloc_context3(codec);
if (!video_enc_ctx) {
fprintf(stderr, "Could not allocate codec context");
return AVERROR(ENOMEM);
}
/* set the output codec context parameters */
video_enc_ctx->bit_rate = 400000;
video_enc_ctx->width = STREAM_WIDTH;
video_enc_ctx->height = STREAM_HEIGHT;
video_enc_ctx->time_base = (AVRational){1, STREAM_FRAME_RATE};
video_enc_ctx->gop_size = 10;
video_enc_ctx->max_b_frames = 1;
video_enc_ctx->pix_fmt = STREAM_PIX_FMT;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
video_enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
/* init the video encoder */
ret = avcodec_open2(video_enc_ctx, codec, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to open encoder");
return ret;
}
/* copy the codec context parameters to the output stream */
ret = avcodec_parameters_from_context(video_stream->codecpar, video_enc_ctx);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parameters to output stream");
return ret;
}
video_stream->time_base = video_enc_ctx->time_base;
/* set the output file format */
ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open output file '%s'", filename);
return ret;
}
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to write output file header");
return ret;
}
return 0;
}
static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *stream, AVPacket *pkt)
{
pkt->stream_index = stream->index;
av_packet_rescale_ts(pkt, *time_base, stream->time_base);
return av_interleaved_write_frame(fmt_ctx, pkt);
}
static int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index)
{
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codecpar->codec->capabilities &
AV_CODEC_CAP_DELAY))
return 0;
while (1) {
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = avcodec_send_frame(fmt_ctx->streams[stream_index]->codec, NULL);
if (ret < 0)
break;
while (1) {
ret = avcodec_receive_packet(fmt_ctx->streams[stream_index]->codec, &enc_pkt);
if (ret < 0)
break;
got_frame = 1;
ret = write_frame(fmt_ctx, &fmt_ctx->streams[stream_index]->time_base, fmt_ctx->streams[stream_index], &enc_pkt);
if (ret < 0)
break;
}
if (ret == AVERROR_EOF)
return 0;
}
return ret;
}
int main(int argc, char **argv)
{
int ret;
AVPacket packet = {0};
int64_t end_pts;
int64_t pts = 0;
int64_t duration;
int64_t cur_time;
int64_t last_time;
int video_frame_count = 0;
const char *input_filename = NULL;
const char *output_filename = NULL;
/* parse the command-line options */
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
usage(argv[0]);
}
else if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--input")) {
if (i + 1 < argc) {
input_filename = argv[++i];
}
else {
usage(argv[0]);
}
}
else if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--output")) {
if (i + 1 < argc) {
output_filename = argv[++i];
}
else {
usage(argv[0]);
}
}
else {
usage(argv[0]);
}
}
/* check that input and output filenames have been provided */
if (!input_filename || !output_filename) {
usage(argv[0]);
}
/* register all codecs and formats */
av_register_all();
/* open the input file */
ret = open_input_file(input_filename);
if (ret < 0) {
fprintf(stderr, "Failed to open input file '%s'", input_filename);
exit(1);
}
/* open the output file */
ret = open_output_file(output_filename);
if (ret < 0) {
fprintf(stderr, "Failed to open output file '%s'", output_filename);
exit(1);
}
/* set the start time */
start_time = av_gettime();
/* encode video frames */
while (1) {
/* get the next video frame */
ret = av_read_frame(ifmt_ctx, &packet);
if (ret < 0) {
/* end of stream */
break;
}
/* check if this packet belongs to the video stream */
if (packet.stream_index == video_stream_index) {
/* decode the video frame */
ret = avcodec_send_packet(video_dec_ctx, &packet);
if (ret < 0) {
fprintf(stderr, "Error submitting a packet for decoding");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(video_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < 0) {
fprintf(stderr, "Error during decoding");
exit(1);
}
/* encode the video frame */
frame->pts = pts++;
av_init_packet(&pkt);
pkt.data = NULL;
pkt.size = 0;
ret = avcodec_send_frame(video_enc_ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error submitting a frame for encoding");
break;
}
while (ret >= 0) {
ret = avcodec_receive_packet(video_enc_ctx, &pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < 0) {
fprintf(stderr, "Error during encoding");
exit(1);
}
/* write the encoded video frame to the output file */
duration = pkt.duration;
cur_time = av_gettime() - start_time;
if (last_pts) {
duration = pkt.pts - last_pts;
}
else {
duration = 0;
}
last_time = duration * av_q2d(video_stream->time_base) * 1000;
if (cur_time < last_time) {
usleep((last_time - cur_time) * 1000);
}
ret = write_frame(ofmt_ctx, &video_stream->time_base, video_stream, &pkt);
if (ret < 0) {
fprintf(stderr, "Error writing video frame");
break;
}
av_packet_unref(&pkt);
last_pts = pkt.pts;
end_pts = pkt.pts + pkt.duration;
video_frame_count++;
}
}
}
av_packet_unref(&packet);
}
/* flush the video encoder */
ret = flush_encoder(ofmt_ctx, video_stream_index);
if (ret < 0) {
fprintf(stderr, "Error flushing video encoder");
exit(1);
}
/* write the trailer */
av_write_trailer(ofmt_ctx);
/* print the summary */
printf("Video frames: %d\n", video_frame_count);
/* free the resources */
avcodec_free_context(&video_dec_ctx);
avcodec_free_context(&video_enc_ctx);
avformat_close_input(&ifmt_ctx);
avformat_free_context(ofmt_ctx);
av_frame_free(&frame);
return 0;
}
```
注:上述代码仅为示例代码,需要根据具体情况进行修改,比如调整视频尺寸等参数。
阅读全文