当前位置: 首页 > news >正文

政务网站的建设原则深圳保障性住房申请

政务网站的建设原则,深圳保障性住房申请,开放大学门户网站建设方案,免费网站推广软件下载大全作者#xff1a;billy 版权声明#xff1a;著作权归作者所有#xff0c;商业转载请联系作者获得授权#xff0c;非商业转载请注明出处 前言 之前做了一个功能#xff0c;从采集卡获取数据然后录制成视频#xff0c;结果发现录制的视频内存占用非常大#xff0c;1分钟的…作者billy 版权声明著作权归作者所有商业转载请联系作者获得授权非商业转载请注明出处 前言 之前做了一个功能从采集卡获取数据然后录制成视频结果发现录制的视频内存占用非常大1分钟的视频大概有 800MB 内存。在帧率和分辨率已确定的情况下只能通过调整比特率来减少内存占用但是设置比特率在不同编码器和平台支持情况都有所不同有些编码器甚至不支持直接设置比特率所以博主想起了 ffmpeg 这个神器。 这里先介绍一下视频的相关参数 在用户视角 清晰度 比特率码率 / 分辨率 流畅度 帧率在开发者视角 影响内存的主要是分辨率 影响 CPU 的码率和编码格式 影响 GPU 的分辨率和编码格式 影响体积大小和带宽码率ffmpeg 库功能测试 首先在网上找到了 ffmpeg 库的 windows 安装包来做下测试 百度网盘下载链接ffmpegwindows安装包 提取码fkmn 下载完成之后可以直接使用 bin\ffmpeg.exe 在命令行做测试把 ffmpeg\bin 路径添加到环境变量中 确认 ffmpeg 版本和配置ffmpeg -version 列举所有设备ffmpeg -list_devices true -f dshow -i dummy 对已录制的内存占用较大的视频进行压缩 设置码率ffmpeg -i input.mp4 -b:v 1000k output.mp4设置分辨率ffmpeg -i input.mp4 -s 640x360 output.mp4设置帧率ffmpeg -i input.mp4 -r 30 output.mp4综合调整ffmpeg -i input.mp4 -b:v 800k -s 640x360 -r 30 output.mp4 也可以用 python 跑脚本 import subprocess def compress_video(input_file, output_file, bitrate1000k):command [ffmpeg,-i, input_file,-b:v, bitrate,output_file]try:subprocess.run(command, checkTrue)print(f视频压缩成功输出文件为: {output_file})except subprocess.CalledProcessError as e:print(f视频压缩失败: {e})# 使用示例 input_file input.mp4 output_file output.mp4 compress_video(input_file, output_file, bitrate800k)再测试一下直接打开设备录制视频 ffmpeg -f dshow -i videodevice_pnp_\\?\usb#vid_2b89pid_5647mi_00#7223c07ce00000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global -c:v libx264 -max_delay 10000 -b:v 1000k -bufsize 200k -r 30 output.mp4 -y命令解释 -c:v libx264: 指定使用libx264编码器进行视频编码。 -max_delay 10000: 设置编码器的最大延迟为10000毫秒10秒。这有助于控制视频编码的缓冲延迟。 -b:v 1000k: 设置视频比特率为1000 kbps千比特每秒。 -bufsize 200k: 设置编码器的缓冲区大小为200 kbps。这个参数用于控制编码器在遇到高负载或低负载时的比特率变化。 -r 30: 设置帧率为30帧每秒。 output.mp4: 输出文件名。 -y: 覆盖输出文件如果文件已存在。测试结果为用 ffmpeg 压缩过的视频内存占用率非常小码率越小内存就越小。但是对于客户来说他不会使用命令行去做压缩所以最终方案还是直接使用 ffmpeg 库来录制视频。 Qt 中集成 ffmpeg 库来录制视频 首先在网上下载编译完成的 ffmpeg 库博主用的是 Qt 5.15.2 和 vs2019 百度网盘下载链接ffmpeg库文件 提取码: cqwx 把库集成到 Qt 中 INCLUDEPATH $$PWD/ffmpeg/includeLIBS -L$$PWD/ffmpeg/lib/ -lavcodec LIBS -L$$PWD/ffmpeg/lib/ -lavdevice LIBS -L$$PWD/ffmpeg/lib/ -lavfilter LIBS -L$$PWD/ffmpeg/lib/ -lavformat LIBS -L$$PWD/ffmpeg/lib/ -lavutil LIBS -L$$PWD/ffmpeg/lib/ -lpostproc LIBS -L$$PWD/ffmpeg/lib/ -lswresample LIBS -L$$PWD/ffmpeg/lib/ -lswscale实现拍照和录屏的功能 extern C { #include libavformat/avformat.h #include libavcodec/avcodec.h #include libavutil/avutil.h #include libavutil/imgutils.h #include libswscale/swscale.h #include libavdevice/avdevice.h }void showErrorInfo(int ret) {static char errbuf[AV_ERROR_MAX_STRING_SIZE];memset(errbuf, 0, AV_ERROR_MAX_STRING_SIZE);av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);qDebug() Error info: errbuf; }void videoRecord() {avformat_network_init();avdevice_register_all();// 遍历所有设备类型// const AVInputFormat *iformat nullptr;// while ((iformat av_input_audio_device_next(iformat))) {// qDebug() Audio input device: iformat-name - iformat-long_name;// }// iformat nullptr;// while ((iformat av_input_video_device_next(iformat))) {// qDebug() Video input device: iformat-name - iformat-long_name;// }const AVInputFormat *iformat_dshow av_find_input_format(dshow);if (!iformat_dshow){qDebug() Could not find input format !;return;}// 用于存储设备列表的上下文AVDeviceInfoList *deviceList nullptr;int ret avdevice_list_input_sources(iformat_dshow, nullptr, nullptr, deviceList);if (ret 0) {qDebug() Could not list input sources !;showErrorInfo(ret);return;}// 设备名称std::string deviceName ;// 遍历设备列表// qDebug() Available DirectShow devices:;for (int i 0; i deviceList-nb_devices; i){AVDeviceInfo *device deviceList-devices[i];// qDebug() Device i : device-device_description ( device-device_name );// 获取绿联采集卡的设备名称QString description QString(device-device_description);if ( description.contains(UGREEN) ) {deviceName video description.toStdString();break;}}// 释放设备列表avdevice_free_list_devices(deviceList);//------------------------------// 创建 AVFormatContextAVFormatContext *inputFormatContext avformat_alloc_context();if (!inputFormatContext) {qDebug() Could not allocate AVFormatContext !;return;}// 设置附加参数AVDictionary *options nullptr;int framerate 30;av_dict_set(options, rtbufsize, 100M, 0); // 设置缓冲区大小av_dict_set(options, framerate, 30, 0); // 设置帧率// 打开输入设备ret avformat_open_input(inputFormatContext, deviceName.c_str(), iformat_dshow, options);if (ret 0) {qDebug() Could not open input device !;showErrorInfo(ret);return;}// 打印输入设备的信息// av_dump_format(inputFormatContext, 0, deviceName.c_str(), 0);// 查找输入流信息ret avformat_find_stream_info(inputFormatContext, nullptr);if (ret 0) {qDebug() Could not find stream information !;showErrorInfo(ret);return;}// 查找视频流int videoStreamIndex -1;for (unsigned int i 0; i inputFormatContext-nb_streams; i){if (inputFormatContext-streams[i]-codecpar-codec_type AVMEDIA_TYPE_VIDEO) {videoStreamIndex i;break;}}if (videoStreamIndex -1) {qDebug() Could not find video stream !;return;}// 获取视频流参数AVCodecParameters *inputCodecParameters inputFormatContext-streams[videoStreamIndex]-codecpar;// 查找输入解码器const AVCodec *inputCodec avcodec_find_decoder(inputCodecParameters-codec_id);if (!inputCodec) {qDebug() Could not find codec !;return ;}// 创建输入解码器上下文AVCodecContext *inputCodecContext avcodec_alloc_context3(inputCodec);if (!inputCodecContext) {qDebug() Could not allocate codec context !;return;}// 将视频流参数复制到输入解码器上下文ret avcodec_parameters_to_context(inputCodecContext, inputCodecParameters);if (ret 0) {qDebug() Could not copy codec parameters to context !;showErrorInfo(ret);return;}// 打开输入解码器ret avcodec_open2(inputCodecContext, inputCodec, nullptr);if (ret 0) {qDebug() Could not open codec !;showErrorInfo(ret);return;}//------------------------------// 分配帧和数据包AVFrame *frame av_frame_alloc();AVPacket *packet av_packet_alloc();if (!frame || !packet) {qDebug() Could not alloc frame and packet !;return;}// 分配图像转换上下文SwsContext *swsContext sws_getContext(inputCodecContext-width, inputCodecContext-height, inputCodecContext-pix_fmt,inputCodecContext-width, inputCodecContext-height, AV_PIX_FMT_RGB24,SWS_BILINEAR, nullptr, nullptr, nullptr);if (!swsContext) {qDebug() Could not initialize SwsContext !;return;}// 分配 RGB 帧AVFrame *rgbFrame av_frame_alloc();int numBytes av_image_get_buffer_size(AV_PIX_FMT_RGB24, inputCodecContext-width, inputCodecContext-height, 1);uint8_t *buffer (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));av_image_fill_arrays(rgbFrame-data, rgbFrame-linesize, buffer, AV_PIX_FMT_RGB24,inputCodecContext-width, inputCodecContext-height, 1);// 读取帧数据保存10张图片int frameCount 0;while (av_read_frame(inputFormatContext, packet) 0 frameCount 11){// 确保帧的数据类型和解码器的数据类型一致if (packet-stream_index videoStreamIndex){// 发送数据包到解码器ret avcodec_send_packet(inputCodecContext, packet);if (ret 0) {qDebug() Error sending packet to decoder !;showErrorInfo(ret);continue;}// 接收解码后的帧while (avcodec_receive_frame(inputCodecContext, frame) 0){// 舍弃第一帧if ( frameCount 0 ) {frameCount;continue;}// 转换图像格式sws_scale(swsContext, frame-data, frame-linesize, 0, inputCodecContext-height,rgbFrame-data, rgbFrame-linesize);// 创建 QImageQImage image(rgbFrame-data[0], inputCodecContext-width, inputCodecContext-height, QImage::Format_RGB888);// 保存图像QString fileName QString(frame_%1.jpg).arg(frameCount);if (!image.save(fileName)) {qDebug() Error saving image: fileName;} else {qDebug() Image saved: fileName;}}}av_packet_unref(packet);}//------------------------------// 输出格式上下文AVFormatContext *outputFormatContext nullptr;const char *outputFileName output.mp4;// 创建输出格式上下文ret avformat_alloc_output_context2(outputFormatContext, nullptr, nullptr, outputFileName);if (!outputFormatContext) {qDebug() Could not create output context !;showErrorInfo(ret);return;}// 查找输出编码器const AVCodec *outputCodec avcodec_find_encoder(AV_CODEC_ID_H264);if (!outputCodec) {qDebug() Could not find output codec !;return;}// 创建输出流AVStream *outputStream avformat_new_stream(outputFormatContext, outputCodec);if (!outputStream) {qDebug() Could not create output stream !;return;}// 打开输出编码器上下文AVCodecContext *outputCodecContext avcodec_alloc_context3(outputCodec);if (!outputCodecContext) {qDebug() Could not allocate output codec context !;return;}// 设置输出编码器参数outputCodecContext-codec_id outputCodec-id;outputCodecContext-codec_type AVMEDIA_TYPE_VIDEO;outputCodecContext-pix_fmt AV_PIX_FMT_YUV420P;outputCodecContext-width 1920; // 设置分辨率outputCodecContext-height 1080;outputCodecContext-time_base {1, framerate}; // 设置帧率outputCodecContext-framerate {framerate, 1};outputCodecContext-bit_rate 2000000; // 设置比特率outputCodecContext-rc_buffer_size 2 * outputCodecContext-bit_rate; // 设置缓冲区大小为码率的两倍outputCodecContext-gop_size 10; // 设置关键帧间隔outputCodecContext-max_b_frames 1;if (outputFormatContext-oformat-flags AVFMT_GLOBALHEADER) {outputCodecContext-flags | AV_CODEC_FLAG_GLOBAL_HEADER;}// 打开输出编码器ret avcodec_open2(outputCodecContext, outputCodec, nullptr);if (ret 0) {qDebug() Could not open output codec !;showErrorInfo(ret);return;}// 复制输出编码器参数到输出流avcodec_parameters_from_context(outputStream-codecpar, outputCodecContext);outputStream-time_base outputCodecContext-time_base;//------------------------------// 打开输出文件if (!(outputFormatContext-oformat-flags AVFMT_NOFILE)) {ret avio_open(outputFormatContext-pb, outputFileName, AVIO_FLAG_WRITE);if (ret 0) {qDebug() Could not open output file !;showErrorInfo(ret);return;}}// 写入文件头ret avformat_write_header(outputFormatContext, nullptr);if (ret 0) {qDebug() Could not write header !;showErrorInfo(ret);return;}// 分配帧和数据包AVFrame *inputFrame av_frame_alloc();AVPacket *inputPacket av_packet_alloc();AVFrame *outputFrame av_frame_alloc();AVPacket *outputPacket av_packet_alloc();if (!inputFrame || !inputPacket || !outputFrame || !outputPacket) {qDebug() Could not alloc frame and packet !;return;}// 分配图像转换上下文SwsContext *swsContext2 sws_getContext(inputCodecContext-width, inputCodecContext-height, inputCodecContext-pix_fmt,outputCodecContext-width, outputCodecContext-height, outputCodecContext-pix_fmt,SWS_BILINEAR, nullptr, nullptr, nullptr);if (!swsContext2) {qDebug() Could not initialize SwsContext !;return;}// 分配输出帧数据outputFrame-format AV_PIX_FMT_YUV420P;outputFrame-width inputCodecContext-width;outputFrame-height inputCodecContext-height;ret av_frame_get_buffer(outputFrame, 0);if (ret 0) {qDebug() Could not get frame buffer !;showErrorInfo(ret);return;}// 录屏60秒帧率30保存1800帧数据int frameCount2 0;while (av_read_frame(inputFormatContext, inputPacket) 0 frameCount2 1800){// 确保帧的数据类型和解码器的数据类型一致if (inputPacket-stream_index videoStreamIndex){// 发送输入数据包到解码器ret avcodec_send_packet(inputCodecContext, inputPacket);if (ret 0) {qDebug() Error sending packet to decoder !;showErrorInfo(ret);continue;}// 接收解码后的帧while (avcodec_receive_frame(inputCodecContext, inputFrame) 0){// 转换图像格式sws_scale(swsContext2, inputFrame-data, inputFrame-linesize, 0, inputCodecContext-height,outputFrame-data, outputFrame-linesize);outputFrame-pts frameCount2;qDebug() frameCount2: frameCount2;// 发送输出帧到输出编码器ret avcodec_send_frame(outputCodecContext, outputFrame);if (ret 0) {qDebug() Error sending frame to encoder !;showErrorInfo(ret);continue;}// 接收编码后的数据包while (avcodec_receive_packet(outputCodecContext, outputPacket) 0){av_packet_rescale_ts(outputPacket, outputCodecContext-time_base, outputStream-time_base);outputPacket-stream_index outputStream-index;// 写入数据包到输出文件ret av_interleaved_write_frame(outputFormatContext, outputPacket);if (ret 0) {qDebug() Error writing packet to output file;showErrorInfo(ret);continue;}av_packet_unref(outputPacket);}}}av_packet_unref(inputPacket);}// 刷新编码器avcodec_send_frame(outputCodecContext, nullptr);while (avcodec_receive_packet(outputCodecContext, outputPacket) 0){av_packet_rescale_ts(outputPacket, outputCodecContext-time_base, outputStream-time_base);outputPacket-stream_index outputStream-index;ret av_interleaved_write_frame(outputFormatContext, outputPacket);if (ret 0) {qDebug() Error writing packet to output file;showErrorInfo(ret);}av_packet_unref(outputPacket);}// 写入文件尾av_write_trailer(outputFormatContext);// 释放资源av_frame_free(inputFrame);av_frame_free(outputFrame);av_packet_free(inputPacket);av_packet_free(outputPacket);sws_freeContext(swsContext2);avcodec_free_context(outputCodecContext);avformat_free_context(outputFormatContext);if (outputFormatContext !(outputFormatContext-oformat-flags AVFMT_NOFILE)) {avio_closep(outputFormatContext-pb);}av_free(buffer);av_frame_free(rgbFrame);av_frame_free(frame);av_packet_free(packet);sws_freeContext(swsContext);avcodec_free_context(inputCodecContext);avformat_close_input(inputFormatContext);avformat_free_context(inputFormatContext);av_dict_free(options);avformat_network_deinit(); }
http://www.w-s-a.com/news/476961/

相关文章:

  • 东莞建设通网站中小企业网站的建设实践报告
  • 合肥网站建设电话wordpress 点击量
  • 公司网站制作注意什么wordpress如何邀请人看
  • 做渲染的网站太原做网站兼职
  • 网站开发实施方案怎么设置wordpress底栏文字
  • 网站建设朝阳学前端有必要找培训机构吗
  • 自适应网站好处wordpress ftp验证
  • 网站建设的时间免费ppt模板的网站
  • 建个人网站一般多少钱ppt下载网站哪个好
  • 网站建设比赛网站建设合同标的怎么写
  • 中国做的儿童编程网站网站建设模板网站
  • 电脑做系统网站微信开店
  • site之后网站在首页说明说明网络舆情分析师怎么考
  • 本溪网站建设兼职wordpress lapa
  • 官网网站设计费用vue大型网站怎么做路由
  • 青海省安建设管理部门网站厦门网站快照优化公司
  • 张家港建网站公司网站开发 认证
  • 网站建设方式优化兰州医院网站制作
  • 怎么创造网站wordpress伪静态规则怎么写
  • 自己怎么做一元购物网站信誉好的合肥网站推广
  • 做网站的骗术有什么好的网站设计思想的博客
  • 网站建设工作 方案企查查企业信息查询在线
  • 上海外贸建站商城定制软件安卓
  • 成都网站建设_创新互联wordpress 相邻文章
  • 电子商务网站制作步骤免费建网站知乎
  • 龙岩有什么招聘本地网站团购网站 方案
  • 服务器运行一段时间网站打不开注册公司名字核名查询系统
  • 企业网站改版的意义响应式网站建设新闻
  • 大连金州新区规划建设局网站金坛市建设局网站
  • 有哪些做排球比赛视频网站wordpress 教师工作坊