相关文章:

v4l2采集图像并转换格式

视频编码(yuv编码h264)

目标:使用v4l2采集摄像头图像数据,并实时编码成h264视频文件

代码:

#include <stdio.h>
#include <sys/time.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"#define CAMERA_DEV "/dev/video0"
#define CAMERA_FMT AV_PIX_FMT_YUYV422
#define ENCODE_FMT AV_PIX_FMT_YUV420Pfloat get_diff_time(struct timeval *tim, int update) {float dt = 0.0;struct timeval now;gettimeofday(&now, NULL);dt = (float)(now.tv_sec - tim->tv_sec);dt += (float)(now.tv_usec - tim->tv_usec)*1e-6;if (update == 1) {tim->tv_sec = now.tv_sec;tim->tv_usec = now.tv_usec;}return dt;
}void get_video(const char *out_file, int w, int h, int nb_frames) {// 初始化格式上下文AVInputFormat *in_fmt = av_find_input_format("video4linux2");if (in_fmt == NULL) {printf("can't find_input_format\n");return ;}// 设置摄像头的分辨率AVDictionary *option = NULL;char video_size[10];sprintf(video_size, "%dx%d", w, h);av_dict_set(&option, "video_size", video_size, 0);AVFormatContext *fmt_ctx = NULL;if (avformat_open_input(&fmt_ctx, CAMERA_DEV, in_fmt, &option) < 0) {printf("can't open_input_file\n");return ;}// printf device infoav_dump_format(fmt_ctx, 0, CAMERA_DEV, 0);// 初始化编码器AVCodec *cod = avcodec_find_encoder(AV_CODEC_ID_H264);if (cod == NULL) {printf("failed to find encoder\n");return ;}AVCodecContext *cod_ctx = avcodec_alloc_context3(cod);cod_ctx->pix_fmt = ENCODE_FMT;cod_ctx->width = w;cod_ctx->height = h;cod_ctx->time_base.num = 1;cod_ctx->time_base.den = 30;cod_ctx->bit_rate = 400000;cod_ctx->qmin = 10;cod_ctx->qmax = 51;cod_ctx->max_b_frames = 0;cod_ctx->thread_count = 4;cod_ctx->gop_size = 15;if (avcodec_open2(cod_ctx, cod, NULL) < 0) {printf("failed to open encoder\n");return ;}struct SwsContext *sws_ctx;// 图像格式转换:CAMERA_FMT --> ENCODE_FMTsws_ctx = sws_getContext(w, h, CAMERA_FMT, \w, h, ENCODE_FMT, 0, NULL, NULL, NULL);uint8_t *yuy2buf[4];int yuy2_linesize[4];int yuy2_size = av_image_alloc(yuy2buf, yuy2_linesize, w, h, CAMERA_FMT, 1);uint8_t *yuv420pbuf[4];int yuv420p_linesize[4];int yuv420p_size = av_image_alloc(yuv420pbuf, yuv420p_linesize, w, h, ENCODE_FMT, 1);FILE *fp = fopen(out_file, "w+");// 初始化packet,存放编码数据AVPacket *camera_packet = av_packet_alloc();AVPacket *packet = av_packet_alloc();// 初始化frame,存放原始数据AVFrame *frame = av_frame_alloc();frame->width = w;frame->height = h;frame->format = ENCODE_FMT;av_frame_get_buffer(frame, 0);int y_size = w*h;struct timeval timer;float tim_start = get_diff_time(&timer, 1);for (int i = 0; i < nb_frames; i++) {// 摄像头获取图像数据av_read_frame(fmt_ctx, camera_packet);  memcpy(yuy2buf[0], camera_packet->data, camera_packet->size);// 图像格式转化sws_scale(sws_ctx, (const uint8_t **)yuy2buf, yuy2_linesize, \0, h, yuv420pbuf, yuv420p_linesize);av_packet_unref(camera_packet); // 清理数据frame->data[0] = yuv420pbuf[0];frame->data[1] = yuv420pbuf[1];frame->data[2] = yuv420pbuf[2];frame->pts = i;// 图像数据发送到编码器int send_result = avcodec_send_frame(cod_ctx, frame);if (send_result < 0) {printf("send failed:%d.\n", send_result);}while (send_result >= 0) {// 编码器对图像数据进行编码int receive_result = avcodec_receive_packet(cod_ctx, packet);if (receive_result == AVERROR(EAGAIN) || receive_result == AVERROR_EOF)break;fwrite(packet->data, 1, packet->size, fp);// printf("get %d frames\n", i);av_packet_unref(packet);}}printf("recording time:%f\n", get_diff_time(&timer, 0));fclose(fp);av_packet_free(&camera_packet);av_packet_free(&packet);av_frame_free(&frame);avformat_close_input(&fmt_ctx);sws_freeContext(sws_ctx);av_freep(yuy2buf);av_freep(yuv420pbuf);
}int main(int argc, char const* argv[])
{avdevice_register_all();get_video("output.h264", 640, 480, 500);return 0;
}

运行效果:500帧图像,录制时间为17s。

播放:ffplay output.h264

其它:可以使用一个alarm时钟信号,设置录制时间。

ffmpeg —— v4l2录制h264视频文件(边采集边转码)相关推荐

  1. FFmpeg 4.0.2编码YUV序列为H264视频文件

    /****************************** 功能:编码YUV序列为h264视频文件 FFmpeg:4.0.2 ******************************/ #in ...

  2. C#基于FFMPEG进行录制音视频(一)

    当前有需求在window系统录制视频(带声音的),需要支持USB摄像头 或者 支持RTSP的网络摄像头  录制的视频文件需要带压缩的,减少文件储存体积 Fmpeg 是领先的多媒体框架,能够解码.编码. ...

  3. H264 视频文件如何缩放分辨率?

    前几天在知识星球里面有位朋友请教问题:如何将 H264 视频缩放分辨率? 具体的问题详情如下: 将 800x600 的 H264 文件缩放成 400x300 的,大概的流程是先解码,得到 AVFram ...

  4. H264 视频文件 帧格式 传输封装等 杂碎

    H264 视频文件 帧格式 传输封装等 杂碎 rfc3984 Standards Track [Page 2] RFC 3984 RTP Payload Format for H.264 Video ...

  5. 手机录制的视频文件被删除如何恢复

    手机录制的视频文件被删除如何恢复 前段时刻和女友一起出国去了一次印尼,这仍是咱长这么大首次出国,呵呵,印尼举世闻名的巴厘岛,也许许多人还仅仅晓得巴厘岛,却不晓得印尼吧?本来印尼和泰国相同,是热带景色, ...

  6. windows使用ffmpeg将mp4文件转变成h264视频文件

    我是直接使用ffmpeg的安装包,win10下 首先下载ffmpeg http://www.ffmpeg.org/download.htmlhttp://www.ffmpeg.org/download ...

  7. JavaCV音视频开发宝典:JavaCV混合屏幕录屏和系统声音录制mp4视频文件(windows桌面屏幕和系统声音混合录制)

    <JavaCV音视频开发宝典>专栏目录导航 <JavaCV音视频开发宝典>专栏介绍和目录 前言 之前已经写过dshow方式采集摄像头画面.麦克风.系统声音和桌面屏幕画面. 之前 ...

  8. FFMPEG学习(4)-使用ffmpeg读取基本音视频文件信息,熟释AVFormatContext结构

    前段时间把环境整了下,闲时学习下ffmpeg. 最近在看雷神的创作,边看,边学,感谢雷神! 头文件: // // ffmpeg_read_av_info.hpp // ffmpegDemo // // ...

  9. FFmpeg[13] - 如何实现视频文件的倍速播放和慢速播放

    目录 前言 正文 <FFmpeg实用技巧>系列-总览_liuzhen007的专栏-CSDN博客前言FFmpeg是一个完整的跨平台解决方案,可用于音视频的录制.转码.流化处理等应用,号称音视 ...

最新文章

  1. iOS设备中垂直同步开启后的帧率计数
  2. C++编程语言之赋值运算符
  3. “12306” 的架构到底有多牛逼?
  4. 为什么建议你使用枚举?
  5. 计组之数据运算:11、浮点数的运算
  6. Delphi XE7的安卓程序如何调用JAVA的JAR,使用JAVA的类?
  7. SpannableString 给TextView添加不同的显示样式
  8. De Casteljau算法
  9. 精密电阻阻值表丝印大全
  10. 简单说明什么是网络的DMZ区域
  11. 疫情下跨越一万公里的友情:熊超与飒特电子哨兵的故事
  12. 华为首次自曝“天才少年”成果:入职不到一年算法研究用于千万台 Mate/P 系列手机,网友:值 200 万年薪
  13. 小程序遵循的语法_2020年遵循的最佳应用程序开发实践
  14. Python-Django毕业设计“小世界”私人空间(程序+Lw)
  15. java蚂蚁智力题,智力题大全_附答案
  16. his系统服务器选择,我院完成HIS服务器及数据库的升级改造
  17. 【GIS】PostGIS使用案例
  18. Windows设备管理器中的错误代码
  19. linux 门罗cli钱包操作记录
  20. 求两个文件的交集、并集和差集 sort和uniq

热门文章

  1. gmap mysql cachet_MySQL中binlog cache使用流程解惑
  2. 谁你的财神 谁是你的穷神
  3. 3D Dirichlet Free-Form Deformation(三维Dirichlet自由变形)
  4. 兔子c语言程序,可爱的C语言程序。....兔子问题...斐波那契数列
  5. [论文阅读] (26) 基于Excel可视化分析的论文实验图表绘制总结——以电影市场为例
  6. ubuntu安装水星MW150US无线网卡8188eu驱动
  7. 如何使用python在一个图片内显示多个函数图像及其数学公式
  8. 用HTML+CSS+JS做一个漂亮的个人网页
  9. 3、如何理解超平面?
  10. #芯片# N25Q128A21BSF40F