Ffmpeg录制

// main.c

#include <libavdevice/avdevice.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libavfilter/avfilter.h> #include <libavfilter/buffersink.h> #include <libavfilter/buffersrc.h> #include <libswscale/swscale.h> #include <libavutil/opt.h> #include <libavutil/time.h> #include <libavutil/timestamp.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <string.h>

#define WIDTH 640 #define HEIGHT 480 #define FPS 25 #define SEGMENT_DURATION 3600 // 1 hour

char *generate_filename() { time_t rawtime; struct tm *timeinfo; static char filename[256];

time(&rawtime);

timeinfo = localtime(&rawtime);

strftime(filename, sizeof(filename), "%Y-%m-%d_%H-%M-%S.mp4", timeinfo);

return filename;

}

AVFormatContext *open_input_device() { AVFormatContext *fmt_ctx = NULL; AVInputFormat *input_fmt = av_find_input_format("v4l2");

AVDictionary *options = NULL;

av_dict_set(&options, "input_format", "mjpeg", 0);

av_dict_set(&options, "framerate", "25", 0);

av_dict_set(&options, "video_size", "640x480", 0);

if (avformat_open_input(&fmt_ctx, "/dev/video0", input_fmt, &options) != 0) {

fprintf(stderr, "Could not open input device\n");

exit(1);

}

av_dict_free(&options);

return fmt_ctx;

}

AVCodecContext *open_decoder(AVStream *stream) { AVCodecContext *codec_ctx = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(codec_ctx, stream->codecpar); AVCodec *decoder = avcodec_find_decoder(codec_ctx->codec_id); avcodec_open2(codec_ctx, decoder, NULL); return codec_ctx; }

AVFormatContext *open_output_context(const char *filename, AVCodecContext *dec_ctx, AVCodecContext **enc_ctx, AVStream **out_stream) { AVFormatContext *out_fmt_ctx; avformat_alloc_output_context2(&out_fmt_ctx, NULL, NULL, filename);

AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);

*enc_ctx = avcodec_alloc_context3(encoder);

(*enc_ctx)->height = dec_ctx->height;

(*enc_ctx)->width = dec_ctx->width;

(*enc_ctx)->pix_fmt = AV_PIX_FMT_YUV420P;

(*enc_ctx)->time_base = (AVRational){1, FPS};

avcodec_open2(*enc_ctx, encoder, NULL);

*out_stream = avformat_new_stream(out_fmt_ctx, encoder);

avcodec_parameters_from_context((*out_stream)->codecpar, *enc_ctx);

if (!(out_fmt_ctx->oformat->flags & AVFMT_NOFILE)) {

avio_open(&out_fmt_ctx->pb, filename, AVIO_FLAG_WRITE);

}

avformat_write_header(out_fmt_ctx, NULL);

return out_fmt_ctx;

}

void encode_and_write(AVFormatContext *out_fmt_ctx, AVCodecContext *enc_ctx, AVFrame *filt_frame) { AVPacket out_pkt; av_init_packet(&out_pkt); out_pkt.data = NULL; out_pkt.size = 0;

avcodec_send_frame(enc_ctx, filt_frame);

while (avcodec_receive_packet(enc_ctx, &out_pkt) == 0) {

out_pkt.stream_index = 0;

av_packet_rescale_ts(&out_pkt, enc_ctx->time_base, out_fmt_ctx->streams[0]->time_base);

av_inter

相关推荐
奔跑吧 android3 小时前
【linux kernel 常用数据结构和设计模式】【数据结构 2】【通过一个案例属性list、hlist、rbtree、xarray数据结构使用】
linux·数据结构·list·kernel·rbtree·hlist·xarray
汉克老师3 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(5、机甲战士)
c++·算法·蓝桥杯·01背包·蓝桥杯c++·c++蓝桥杯
Mr_Xuhhh4 小时前
项目需求分析(2)
c++·算法·leetcode·log4j
NiKo_W5 小时前
Linux 文件系统与基础指令
linux·开发语言·指令
PAK向日葵5 小时前
【C/C++】面试官:手写一个memmove,要求性能尽可能高
c语言·c++·面试
Jared_devin5 小时前
二叉树算法题—— [蓝桥杯 2019 省 AB] 完全二叉树的权值
数据结构·c++·算法·职场和发展·蓝桥杯
搞全栈小苏5 小时前
基于Qt QML和C++的MQTT测试客户端(CMakeLists实现)
xml·c++·qt
啊?啊?6 小时前
18 从对象内存到函数调用:C++ 虚函数表原理(继承覆盖 / 动态绑定)+ 多态实战
开发语言·c++·多态原理
bkspiderx6 小时前
C++标准库:文件流类
开发语言·c++
Darkwanderor6 小时前
Linux 的权限详解
linux