当前位置:网站首页>Ffmpeg video coding
Ffmpeg video coding
2022-07-26 04:29:00 【Mr.codeee】
1. brief introduction
Encoded video data , As shown in the figure below , Will be original YUV The data is encoded as H.264 perhaps H.265.

2. technological process

2.1 In the use of FFmpeg API Before , Need to register first API, And then you can use it API. Of course , The new version of the library does not need to call the following methods .
av_register_all()2.2 Find the encoder , This example demonstrates coding H.264
AVCodec* codec = nullptr;
// Find the encoder
codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec)
{
fprintf(stderr, "Codec not found\n");
exit(1);
}2.3 apply AVCodecContext
// apply AVCodecContext
AVCodecContext* codec_ctx = nullptr;
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx)
{
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
2.4 Set video parameters
For example, set the width of the video 、 high 、fps、 Bit rate and so on
codec_ctx->bit_rate = 2581504; // Bit rate Clarity related
codec_ctx->width = 1920;
codec_ctx->height = 1012;
AVRational time_base = { 1, 25 };
AVRational framerate = { 25, 1 };
codec_ctx->time_base = time_base;
codec_ctx->framerate = framerate;
codec_ctx->gop_size = 10;
codec_ctx->max_b_frames = 1;
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;2.5 Turn on the encoder
// Turn on the encoder
if (avcodec_open2(codec_ctx, codec, NULL) < 0)
{
fprintf(stderr, "Could not open codec\n");
exit(1);
}2.6 Set the parameters of the video frame
Format 、 wide 、 higher .
AVFrame *frame = av_frame_alloc();
if (!frame)
{
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
frame->format = codec_ctx->pix_fmt;
frame->width = codec_ctx->width;
frame->height = codec_ctx->height;2.7 by frame Allocate space
/* allocate the data buffers */
int ret = av_frame_get_buffer(frame, 0);
if (ret < 0)
{
fprintf(stderr, "Could not allocate audio data buffers\n");
exit(1);
}
2.8 obtain frame Of buffer size , Allocate one buffer
int bufferSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P,
frame->width, frame->height, 1);
uint8_t* buffer = (uint8_t*)av_malloc(bufferSize);2.9 take frame The data pointer of points to the applied buffer, Later, you only need to operate buffer That's it .
ret = av_image_fill_arrays(frame->data, frame->linesize,
buffer, AV_PIX_FMT_YUV420P, 1920, 1012, 1);
if (ret < 0)
{
exit(1);
}2.10 Reading data , Start coding
// Open the output file
char fileName[20] = "output.h264";
FILE* f = fopen(fileName, "wb");
if (!f)
{
fprintf(stderr, "Could not open %s\n", fileName);
exit(1);
}
// Open the input file
char inputFile[20] = {0};
for(int i=1;i<256;i++)
{
sprintf(inputFile, "img/%d.yuv", i);
FILE* fp = fopen(inputFile, "rb");
if (!fp)
{
fprintf(stderr, "Could not open %s\n", inputFile);
exit(1);
}
// Read data from file
int count = fread(buffer, sizeof(char), bufferSize, fp);
fclose(fp);
frame->pts = i;
// Start coding
ret = avcodec_send_frame(codec_ctx, frame);
while (ret >= 0)
{
ret = avcodec_receive_packet(codec_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0)
{
break;
}
// The encoded data is written to the local file
fwrite(pkt->data, 1, pkt->size, f);
av_packet_unref(pkt);
}
}
fclose(f);3. The video is decoded as YUV
I will H.264 After decoding the data of , Saved hundreds of frames YUV Data to local .
FFmpeg Video decoding ( Seconds understand )_Mr.codeee The blog of -CSDN Blog
4. Source code
take 3 Hundreds of frames in YUV Data is encoded to get H.264.
#include "pch.h"
#include <iostream>
extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/dict.h"
#include "libavutil/opt.h"
#include "libavutil/timestamp.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/imgutils.h"
};
int main()
{
//av_register_all();
avformat_network_init();
AVCodec* codec = nullptr;
// Find the encoder
codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec)
{
fprintf(stderr, "Codec not found\n");
exit(1);
}
// apply AVCodecContext
AVCodecContext* codec_ctx = nullptr;
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx)
{
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
codec_ctx->bit_rate = 2581504; // Bit rate Clarity related
codec_ctx->width = 1920;
codec_ctx->height = 1012;
AVRational time_base = { 1, 25 };
AVRational framerate = { 25, 1 };
codec_ctx->time_base = time_base;
codec_ctx->framerate = framerate;
codec_ctx->gop_size = 10;
codec_ctx->max_b_frames = 1;
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
// Turn on the encoder
if (avcodec_open2(codec_ctx, codec, NULL) < 0)
{
fprintf(stderr, "Could not open codec\n");
exit(1);
}
AVPacket *pkt = av_packet_alloc();
if (!pkt)
{
fprintf(stderr, "could not allocate the packet\n");
exit(1);
}
AVFrame *frame = av_frame_alloc();
if (!frame)
{
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
frame->format = codec_ctx->pix_fmt;
frame->width = codec_ctx->width;
frame->height = codec_ctx->height;
/* allocate the data buffers */
int ret = av_frame_get_buffer(frame, 0);
if (ret < 0)
{
fprintf(stderr, "Could not allocate audio data buffers\n");
exit(1);
}
// Open the output file
char fileName[20] = "output.h264";
FILE* f = fopen(fileName, "wb");
if (!f)
{
fprintf(stderr, "Could not open %s\n", fileName);
exit(1);
}
int bufferSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, frame->width, frame->height, 1);
uint8_t* buffer = (uint8_t*)av_malloc(bufferSize);
ret = av_image_fill_arrays(frame->data, frame->linesize, buffer, AV_PIX_FMT_YUV420P, 1920, 1012, 1);
if (ret < 0)
{
exit(1);
}
// Open the input file
char inputFile[20] = {0};
for(int i=1;i<256;i++)
{
sprintf(inputFile, "img/%d.yuv", i);
FILE* fp = fopen(inputFile, "rb");
if (!fp)
{
fprintf(stderr, "Could not open %s\n", inputFile);
exit(1);
}
// Read data from file
int count = fread(buffer, sizeof(char), bufferSize, fp);
fclose(fp);
frame->pts = i;
// Start coding
ret = avcodec_send_frame(codec_ctx, frame);
while (ret >= 0)
{
ret = avcodec_receive_packet(codec_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0)
{
break;
}
// The encoded data is written to the local file
fwrite(pkt->data, 1, pkt->size, f);
av_packet_unref(pkt);
}
}
fclose(f);
av_free(buffer);
avcodec_close(codec_ctx);
avcodec_free_context(&codec_ctx);
av_frame_free(&frame);
av_packet_free(&pkt);
return 0;
}
边栏推荐
- AWS Support Plan
- Build a maker Education Laboratory for teenagers
- 数据仓库
- 支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布
- Authentication Encyclopedia (cookies, sessions, tokens, JWT, single sign on), in-depth understanding and understanding of authentication
- dijikstra(先预处理)+dfs,relocation truncated to fit
- Acwing_ 12. Find a specific solution for the knapsack problem_ dp
- When you try to delete all bad code in the program | daily anecdotes
- 旋转数组最小数字
- MySQL log classification: error log, binary log, query log, slow query log
猜你喜欢

人脸数据库收集总结

二、国际知名项目-HelloWorld

Apisex's exploration in the field of API and microservices

Getting started with mongodb Basics

How does win11 22h2 skip networking and Microsoft account login?

A series of problems about the number of DP paths

VM virtual machine has no un bridged host network adapter, unable to restore the default configuration

Array sort 2

机器学习之桑基图(用于用户行为分析)

Yapi installation
随机推荐
View and modify the number of database connections
AWS Support Plan
Acwing_ 12. Find a specific solution for the knapsack problem_ dp
Design and implementation of smart campus applet based on cloud development
UE4 获取玩家控制权的两种方式
Life related - ten years of career experience (turn)
UE4 键盘控制开关灯
青少年创客教育的创意设计原理
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
Offline installation of idea plug-in (continuous update)
1. Excel的IF函数
Swiftui one day crash
Life related - less expectation, happier
11、 Exception handler
远坂凛壁纸
Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200
A series of problems about the number of DP paths
Postman imports curl, exports curl, and exports corresponding language codes
Can literature | relationship research draw causal conclusions
Acwing刷题