1. getpic_transform_encode2h264.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "avformat.h"
#include "avcodec.h"
#include "avdevice.h"
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h> /* 知识点: ffmpeg新版本中(封装流)AVStream的codec参数被codecpar参数所替代
该知识点的讲解博客
https://blog.csdn.net/weixin_34419326/article/details/91775446?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
*/ #define LOOP_NUM 300
#define OUT_WIDTH 320
#define OUT_HEIGHT 240 char* input_name= "video4linux2";
char* file_name = "/dev/video0"; struct timeval time_val;
float time_start;
float time_end; float get_diff_time(struct timeval* start , int update)
{
float dt;
struct timeval now;
gettimeofday(&now, NULL);
dt = (float)(now.tv_sec - start->tv_sec);
dt += (float)(now.tv_usec - start->tv_usec) * 1e-6; if (update == 1) {
start->tv_sec = now.tv_sec;
start->tv_usec = now.tv_usec;
} return dt;
} int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->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_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
NULL, &got_frame);
av_frame_free(NULL);
if (ret < 0)
break;
if (!got_frame){
ret=0;
break;
}
printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size);
ret = av_write_frame(fmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
return ret;
} void captureFrame(void){
AVFormatContext *fmtCtx = NULL;
AVInputFormat *inputFmt;
AVPacket *packet;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
struct SwsContext *sws_ctx;
FILE *fp;
int i;
int ret;
int videoindex; enum AVPixelFormat dst_pix_fmt = AV_PIX_FMT_YUV420P;
const char *dst_size = NULL;
const char *src_size = NULL;
uint8_t *src_data[4];
uint8_t *dst_data[4];
int src_linesize[4];
int dst_linesize[4];
int src_bufsize;
int dst_bufsize;
int src_w ;
int src_h ;
int dst_w = OUT_WIDTH;
int dst_h = OUT_HEIGHT; inputFmt = av_find_input_format (input_name); if (inputFmt == NULL) {
printf("can not find_input_format\n");
return;
} if (avformat_open_input ( &fmtCtx, file_name, inputFmt, NULL) < 0){
printf("can not open_input_file\n"); return;
} av_dump_format(fmtCtx, 0, file_name, 0); videoindex= -1;
for(i=0; i<fmtCtx->nb_streams; i++)
if(fmtCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
videoindex=i;
break;
}
if(videoindex==-1){
printf("Didn't find a video stream.\n");
return -1;
} pCodecCtx = fmtCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id); printf("picture width = %d \n", pCodecCtx->width);
printf("picture height = %d \n", pCodecCtx->height);
printf("Pixel Format = %d \n", pCodecCtx->pix_fmt); sws_ctx = sws_getContext( pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, dst_w, dst_h, dst_pix_fmt,
SWS_BILINEAR, NULL, NULL, NULL); src_bufsize = av_image_alloc(src_data, src_linesize, pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, 16);
dst_bufsize = av_image_alloc(dst_data, dst_linesize, dst_w, dst_h, dst_pix_fmt, 1); packet = (AVPacket *)av_malloc(sizeof(AVPacket)); /* set out format */
AVFormatContext *outFormatCtx;
AVOutputFormat *outfmt;
AVCodecContext *outCodecCtx;
AVStream *video_st;
AVDictionary *param = 0;
AVCodec *outCodec;
AVFrame *outFrame;
AVPacket outpkt; uint8_t *picture_buf;
char *out_file = "ds.h264";
int picture_size;
int y_size;
int got_picture;
int loop = 0; outFormatCtx = avformat_alloc_context();
outfmt = av_guess_format(NULL, out_file, NULL);
outFormatCtx->oformat = outfmt;
if (avio_open(&outFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0){
printf("Failed to open output file! \n");
return -1;
}
video_st = avformat_new_stream(outFormatCtx, 0);
if (video_st==NULL){
printf(" creat new stream err \n ");
return -1;
} outCodecCtx = video_st->codec;
outCodecCtx->codec_id = outfmt->video_codec;
outCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
outCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
outCodecCtx->width = dst_w;
outCodecCtx->height = dst_h;
outCodecCtx->bit_rate = 2000000;
outCodecCtx->gop_size=10; outCodecCtx->time_base.num = 1;
outCodecCtx->time_base.den = 25;
outCodecCtx->qmin = 10;
outCodecCtx->qmax = 51;
outCodecCtx->max_b_frames=3; if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {
av_dict_set(&param,"preset", "faster", 0);
//av_dict_set(&param,"preset", "slow", 0);
av_dict_set(&param,"tune", "zerolatency", 0);
} av_dump_format(outFormatCtx, 0, out_file, 1); outCodec = avcodec_find_encoder(outCodecCtx->codec_id);
if (!outCodec){
printf("Can not find encoder! \n");
return -1;
} if (avcodec_open2(outCodecCtx, outCodec, &param) < 0){
printf("Failed to open encoder! \n");
return -1;
} outFrame = av_frame_alloc();
picture_size = avpicture_get_size(outCodecCtx->pix_fmt, outCodecCtx->width, outCodecCtx->height);
picture_buf = (uint8_t *)av_malloc(picture_size);
avpicture_fill((AVPicture *)outFrame, picture_buf, outCodecCtx->pix_fmt, outCodecCtx->width, outCodecCtx->height);
outFrame->format = outCodecCtx->pix_fmt;
outFrame->width = outCodecCtx->width;
outFrame->height = outCodecCtx->height;
avformat_write_header(outFormatCtx,NULL);
av_new_packet(&outpkt,picture_size);
y_size = outCodecCtx->width * outCodecCtx->height; time_start = get_diff_time(&time_val, 1);
while(loop++ < LOOP_NUM){
av_read_frame(fmtCtx, packet);
memcpy(src_data[0], packet->data, packet->size);
sws_scale(sws_ctx, src_data, src_linesize, 0, pCodecCtx->height, dst_data, dst_linesize); outFrame->data[0] = dst_data[0];
outFrame->data[1] = dst_data[0] + y_size;
outFrame->data[2] = dst_data[0] + y_size*5/4; outFrame->pts=(loop -1)*(video_st->time_base.den)/((video_st->time_base.num)*25);
ret = avcodec_encode_video2(outCodecCtx, &outpkt, outFrame, &got_picture);
if(ret < 0)
{
printf("Failed to encode! \n");
return -1;
} if(got_picture==1){
outpkt.stream_index = video_st->index;
ret = av_write_frame(outFormatCtx, &outpkt);
av_free_packet(&outpkt);
}
} time_end = get_diff_time(&time_val, 0);
printf("\n\nencoder %d frame spend time = %f \n\n",loop, time_end); ret = flush_encoder(outFormatCtx,0);
if(ret < 0){
printf("Flushing encoder failed\n");
return -1;
} av_write_trailer(outFormatCtx);
if (video_st){
avcodec_close(video_st->codec);
av_free(outFrame);
av_free(picture_buf);
}
avio_close(outFormatCtx->pb);
avformat_free_context(outFormatCtx); av_free_packet(packet);
av_freep(&dst_data[0]);
sws_freeContext(sws_ctx);
avformat_close_input(&fmtCtx);
} int main(void){
av_register_all();
avcodec_register_all();
avdevice_register_all();
captureFrame();
return 0;
}

2. makefile

OUT_APP         = test
INCLUDE_PATH = /usr/local/ffmpeg/include/
INCLUDE = -I$(INCLUDE_PATH) -I$(INCLUDE_PATH)libavutil/ -I$(INCLUDE_PATH)libavdevice/ \
-I$(INCLUDE_PATH)libavcodec/ -I$(INCLUDE_PATH)libswresample \
-I$(INCLUDE_PATH)libavfilter/ -I$(INCLUDE_PATH)libavformat \
-I$(INCLUDE_PATH)libswscale/ LIB_PATH = /usr/local/ffmpeg/lib/
FFMPEG_LIBS = -L$(LIB_PATH) -lavformat -lavutil -lavdevice -lavcodec -lswresample -lavfilter -lswscale
SDL_LIBS =
LIBS = $(FFMPEG_LIBS)$(SDL_LIBS) COMPILE_OPTS = $(INCLUDE)
C = c
OBJ = o
C_COMPILER = cc
C_FLAGS = $(COMPILE_OPTS) $(CPPFLAGS) $(CFLAGS) LINK = cc -o
LINK_OPTS = -lz -lm -lpthread
LINK_OBJ = getpic_transform_encode2h264.o .$(C).$(OBJ):
$(C_COMPILER) -c -g $(C_FLAGS) $< $(OUT_APP): $(LINK_OBJ)
$(LINK)$@ $(LINK_OBJ) $(LIBS) $(LINK_OPTS) clean:
-rm -rf *.$(OBJ) $(OUT_APP) core *.core *~ picture

实测可能是编码的时间过长,视频分辨率又高,所以导致播放出来跟快进一样了。 以后再分析吧。

.

音视频入门-8-ffmpeg小实验-v4l2 ubuntu取图、格式转换、编码成H264,ffplay观看的更多相关文章

  1. 音视频入门-12-手动生成一张PNG图片

    * 音视频入门文章目录 * 预热 上一篇 [PNG文件格式详解]详细介绍了 PNG 文件的格式. PNG 图像格式文件由一个 8 字节的 PNG 文件署名域和 3 个以上的后续数据块(IHDR.IDA ...

  2. 音视频入门-10-使用libyuv对YUV数据进行缩放、旋转、镜像、裁剪、混合

    * 音视频入门文章目录 * libyuv libyuv 是 Google 开源的实现各种 YUV 与 RGB 之间相互转换.旋转.缩放等的库.它是跨平台的,可在 Windows.Linux.Mac.A ...

  3. 音视频入门-09-RGB&YUV互转-使用开源库

    * 音视频入门文章目录 * 介绍开源库 使用第三方开源库来简化开发,屏蔽一些底层的复杂度,节省大量编写代码的时间. libyuv: Google 开源的实现各种 YUV 与 RGB 之间相互转换.旋转 ...

  4. 音视频入门-08-RGB&YUV

    * 音视频入门文章目录 * YUV & RGB 相互转换公式 YCbCr 的 Y 与 YUV 中的 Y 含义一致,Cb 和 Cr 与 UV 同样都指色彩,Cb 指蓝色色度,Cr 指红色色度,在 ...

  5. 音视频入门-02-RGB拼图

    * 音视频入门文章目录 * 图片 & 像素点 & RGB 平时浏览的图片看不出像素点: 图片放大时,可以看出图片是一个个像素点组成的: 每个像素点的颜色可以用 RGB 表示: RGB ...

  6. 堪称教科书级别的Android音视频入门进阶学习手册,开源分享!

    概述 随着整个互联网的崛起,数据传递的形式也在不断升级变化,总的流行趋势如下: 纯文本的短信,QQ -> 空间,微博,朋友圈的图片文字结合 -> 微信语音 -> 各大直播软件 -&g ...

  7. 音视频入门-11-PNG文件格式详解

    * 音视频入门文章目录 * PNG 文件格式解析 PNG 图像格式文件由一个 8 字节的 PNG 文件署名域和 3 个以上的后续数据块(IHDR.IDAT.IEND)组成. PNG 文件包括 8 字节 ...

  8. 音视频入门-14-JPEG文件格式详解

    * 音视频入门文章目录 * JPEG 文件格式解析 JPEG 文件使用的数据存储方式有多种.最常用的格式称为 JPEG 文件交换格式(JPEG File Interchange Format,JFIF ...

  9. 音视频入门-13-使用开源库生成PNG图片

    * 音视频入门文章目录 * RGB-to-PNG 回顾 上一篇 [手动生成一张PNG图片] 根据 [PNG文件格式详解] 一步一步地手动实现了将 RGB 数据生成了一张 PNG 图片. 有许多开源的 ...

  10. 音视频入门-07-认识YUV

    * 音视频入门文章目录 * YUV & YCbCr 简介 YUV,是一种颜色编码方法.常使用在各个视频处理组件中. YUV 在对照片或视频编码时,考虑到人类的感知能力,允许降低色度的带宽. Y ...

随机推荐

  1. 【JavaScript高级01】JavaScript基础深入

    1,数据类型 JavaScript将数据分为六大类型,分别为数值类型(number).字符串类型(string).布尔类型(boolean).undefined(定义未赋值).null(赋值为空值). ...

  2. Vue 打包后自定义样式无法覆盖elementUI组件原有样式问题

    Vue 打包后自定义样式无法覆盖elementUI组件原有样式问题   by:授客 QQ:1033553122   开发环境   Win 10   node-v10.15.3-x64.msi 下载地址 ...

  3. SpringBoot2.7 霸王硬上弓 Logback1.3 → 不甜但解渴

    开心一刻 一大早,她就发消息质问我 她:你给我老实交代,昨晚去哪鬼混了? 我:没有,就哥几个喝了点酒 她:那我给你打了那么多视频,为什么不接? 我:不太方便呀 她:我不信,和你哥们儿喝酒有啥不方便接视 ...

  4. Linux下查看压缩文件内容的 10 种方法【转】

    转载地址:https://zhuanlan.zhihu.com/p/91593509 1.使用 Vim 编辑器 Vim 不仅仅是编辑器,它还包含其他许多强大的功能.下面的命令将直接显示压缩归档文件的内 ...

  5. vue3源码学习api-vue-sfc文件编译

    vue 最有代表性质的就是.VUE 的文件,每一个vue文件都是一个组件,那么vue 组件的编译过程是什么样的呢 Vue 单文件组件 (SFC)和指令 ast 语法树 一个 Vue 单文件组件 (SF ...

  6. 【Vue】Re19 Promise

    一.概述 Promise是异步编程的解决方案 异步事件的处理: 封装的异步请求函数不能立即获取结果, 通常会传入另外一个函数,在请求成功的时候将数据通过传入的函数回调出去 如果只是一个简单的请求,那么 ...

  7. Longley数据集——强共线性的宏观经济数据,包含GNP deflator(GNP平减指数)、GNP(国民生产总值)、Unemployed(失业率)、ArmedForces(武装力量)、Population(人口)、year(年份),Emlpoyed(就业率)。LongLey数据集因存在严重的多重共线性问题,在早期经常用来检验各种算法或计算机的计算精度

    Longley数据集来自J.W.Longley(1967)发表在JASA上的一篇论文,是强共线性的宏观经济数据,包含GNP deflator(GNP平减指数).GNP(国民生产总值).Unemploy ...

  8. gym.wrappers.Monitor报错,无法使用

    使用gym中的录制功能,报错,具体: >>> import gym >>> gym.wrappers.MonitorTraceback (most recent c ...

  9. Ubuntu系统中CUDA套件nvvp启动后报错Failed to load module "canberra-gtk-module"

    最近在看cuda方面的内容,需要对cuda代码做一些性能分析,于是需要使用nvvp,但是启动nvvp后报错:Failed to load module "canberra-gtk-modul ...

  10. 【牛客刷题】HJ6 质数因子

    题目链接 这道题本身更多的是考察如何计算一个数的质数因子,更像是一道数学题,用到了循环的方法: package main import ( "fmt" "math&quo ...