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. [oeasy]python0068_ 字体样式_正常_加亮_变暗_控制序列

    字体样式 回忆上次内容 上次了解了一个新的转义模式 \33 逃逸控制字符 esc esc 让输出 退出标准输出流 进行控制信息的设置 可以清屏 也可以设置光标输出的位置 还能做什么呢? 可以设置字符的 ...

  2. VSCode最强插件推荐(持续更新)

    一.通用插件 Codelf 描述:变量命名神器 Bracket Pair Colorizer 描述:成对的彩色括号,让括号拥有独立的颜色,便于区分 Prettier - Code formatter ...

  3. Python 函数中箭头 (->)的用处

    Python 3 -> 是函数注释的一部分,表示函数返回值的类型. def useful_function(x) -> int: # Useful code, using x, here ...

  4. layout文本相关

    Textview t=findViewById(R.id.t); ONE设置文本内容: 在XML中android:text直接写 在java中setText()中修改 注意点1继承appcompata ...

  5. RHCA rh442 006 中断号 缓存命中率 内存概念 大页

    IRQ均衡 硬中断 IRQ是中断号 2003 电脑 拨号 56K Modem USB 打印机 拨号成功,打印机会是乱码,他们会不兼容 因为终端号一样 (类似ip地址冲突) 在bios里面调整设备的中断 ...

  6. Jmeter函数助手2-Random

    Random函数用于获取随机范围内的正整数或负整数. 一个范围内的最小值:必填,且必须填入整数(正负数都可以) 一个范围内允许的最大值:必填,且必须填入整数(正负数都可以).最大值需大于最小值如[-6 ...

  7. 【Zookeeper】Win平台伪集群搭建

    下载稳定版Zookeeper https://downloads.apache.org/zookeeper/stable/ GZ包: apache-zookeeper-3.6.3-bin.tar.gz ...

  8. 【Spring】02 过程分析

    回顾JavaWeb三层架构设计: UserDao接口 public interface UserDao { void getUser(); } 实现类 public class UserDaoImpl ...

  9. 【Tutorial C】02 快速入门

    在信息化.智能化的世界里,可能很早很早 我们就听过许多IT类的名词, C语言也在其中,我们侃侃而谈,到底C程序是什么样子? 让我们先看简单的一个例子: #include<stdio.h> ...

  10. Regardless of the outcome of the Russia-Ukraine war, how can Ukraine avoid paying the weapon fees to the United States after the war?

    According to the agreement between the Ukrainian government and the United States, regardless of the ...