当前位置:网站首页>Ffmpeg first learning (only for coding)
Ffmpeg first learning (only for coding)
2022-07-25 23:10:00 【Zhuge hammer who loves learning】
Learning resources
Official documents : Documentation
Thor blog
It can be moved [ summary ]FFMPEG Video and audio codec zero basic learning method
Structure learning :FFMPEG Structural analysis
Basic knowledge of
Video coding
The main function of video coding is to encode video pixel data (RGB,YUV etc. ) Compressed into a video stream , So as to reduce the amount of video data . The video is too big without compression .
Main video coding
| name | Launch mechanism |
|---|---|
| HEVC(H.265) | MPEG/ITU-T |
| H.264 | MPEG/ITU-T |
| MPEG4 | MPEG |
| MPEG2 | MPEG |
| VP9 | |
| VP8 | |
| VC-1 | Microsoft Inc. |
Pros and cons
HEVC(H.265) > VP9 > H.264 > VP8 > MPEG4 > H.263 > MPEG2
Structure learning
AVFrame Structures are generally used to store raw data ( That is, uncompressed data , For example, for video YUV,RGB, For audio, it's PCM), In addition, it contains some related information
AVCodecContext yes FFmpeg The structure of the codec context , and AVCodec Is the structure of encoding and decoding parameters
AVPacket It's a structure that stores information about compressed encoded data
AVCodec Is a structure that stores codec information
Structure relation
Every AVStream Store a video / Relevant data of audio stream ; Every AVStream Corresponding to one AVCodecContext, Store the video / The audio stream uses decoded data ;
Every AVCodecContext Corresponding to one in AVCodec, Including the video / The decoder for the audio . Each decoder corresponds to one AVCodec structure .
official demo And encode.c Learn to understand
Learn official demo:encode_video.c
I'm personally responsible for the official demo Modified and annotated , The modified file can be run directly , Has been uploaded to the Resource download address
Process analysis :
- The file path of the given output after the program runs
- call
avcodec_find_encoderFind the encoder AVCodec - call
avcodec_alloc_context3Assign encoder context AVCodecContext - call
av_packet_allocDistribute AVPacket, The encoder will put each AVFrame Encoded as AVPacket - to AVCodecContext Configuration parameters , Such as bit rate 、 Video width and height 、 Frame format and so on
- call
avcodec_open2Turn on the encoder - call
av_frame_allocDistribute AVFrame Memory and set the format and width of the frame - call
av_frame_get_bufferAllocate space for frame data - Start for loop , First call
av_frame_make_writableEnsure frame data AVFrame Can write , Then prepare a frame of virtual image data to writeframe->data, Finally, for each frame pts assignment , Tell the player when to display this frame data - Start coding , call
avcodec_send_framePut frame data AVFrame Send to encoder - call
avcodec_receive_packetPut the encoded frame AVPacket Take it out of the encoder , Then write each encoded frame into the output file File , Call after writingav_packet_unrefRelease right AVPacket References to - If the encoder ID yes MPEG, You need to output files to File Add a fixed sequence at the end of to get a real MPEG file
- Finally close the file File, And release AVCodecContext、AVFrame、AVPacket Memory footprint , call
avcodec_free_context、av_frame_free、av_packet_free
API Study
avcodec_find_encoder
According to a AVCodecID Find a registered encoder
const AVCodec *avcodec_find_encoder(enum AVCodecID id);
common AVCodecID Casually list some
enum AVCodecID {
AV_CODEC_ID_NONE,
/* video codecs */
AV_CODEC_ID_H263,
AV_CODEC_ID_MPEG4,
AV_CODEC_ID_H264,
AV_CODEC_ID_HEVC,
}
example :
const AVCodec *codec;
enum AVCodecID codec_id;
codec_id = AV_CODEC_ID_HEVC;
codec = avcodec_find_encoder(codec_id);
if (!codec) {
printf("Codec '%d' not found\n", codec_id);
exit(1);
}
avcodec_alloc_context3
by AVCodecContext Structure allocates memory , And set each field as the default value , The structure returned by the result needs to use avcodec_free_context() Release memory
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);
example :
AVCodecContext *c= NULL;
const AVCodec *codec;
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
...
...
...
avcodec_free_context(&c);
av_packet_alloc
by AVPacket Structure allocates memory , And set each field as the default value , The structure returned by the result needs to use av_packet_free() Release memory
AVPacket *av_packet_alloc(void);
example :
AVPacket *pkt;
pkt = av_packet_alloc();
if (!pkt)
exit(1);
...
...
...
av_packet_free(&pkt);
avcodec_open2
initialization AVCodecContext To use given AVCodec , Before using this function, you need to use avcodec_alloc_context3() Allocate memory
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
av_frame_get_buffer
Assign a new buffers Give video data
Before calling this function, you need to set frame Some parameters of format、width、height
int av_frame_get_buffer(AVFrame *frame, int align);
Parameters align Setting to 0, The system will be based on the current CPU Automatic setting alignment
example :
AVFrame *frame;
frame = av_frame_alloc();
frame->format = c->pix_fmt;
frame->width = c->width;
frame->height = c->height;
av_frame_get_buffer(frame, 32);
avcodec_send_frame
Send data frame to encoder . Use avcodec_receive_packet() Function to retrieve buffered output packets
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
avcodec_receive_packet
Read the encoded data from the encoder
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
Return type :
0 It means success
AVERROR(EAGAIN) Output is currently unavailable , The user must give input
AVERROR_EOF The encoder is completely flushed , There will be no output packets
AVERROR(EINVAL) codec It's not turned on , Or it is a decoder rather than an encoder
example :
ret = avcodec_receive_packet(enc_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
}
Conclusion
Just started learning ffmpeg, Feel the stone to cross the river , Little by little demo And the source code is quite interesting
English reading needs improving , Otherwise, you won't see the comments given by the developer
边栏推荐
- AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario
- Recommend short videos every week: more and more smart devices need collaboration, posing a greater challenge to the development of the Internet of things?
- anaconda安装教程环境变量(如何配置环境变量)
- Stack and stack class
- Day 3 experiment
- Ma Tiji Wanmin hall Chef
- [paper notes] a meta reinforcement learning algorithm for causal discovery
- 四旋翼飞行器的飞控实现「建议收藏」
- Analysis of the influence of ESM direction finding error on positioning error
- 7-1 understand everything
猜你喜欢

Custom MVC principle

Memory paging and tuning, kernel and user space

Secure code warrior learning record (IV)

Enterprise level inventory management system of code audit

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

How does PHP remove an element from an array based on the key value

【接口性能优化】索引失效的原因以及如何进行SQL优化

What has Amazon cloud technology done right to become the leader of cloud AI services for three consecutive years?

Network Security Learning (XV) ARP

Stack and stack class
随机推荐
Secure code warrior learning record (III)
技术美术百人计划学习笔记(1)--基础渲染管线
Anaconda installation tutorial environment variables (how to configure environment variables)
MathType安装和解决不能Crtl+V的问题
栈与Stack类
Shanghai Second Polytechnic University - Health Daily autocheck
What has Amazon cloud technology done right to become the leader of cloud AI services for three consecutive years?
CTS test method "suggestions collection"
Design of Butterworth filter and drawing of amplitude frequency characteristic curve
Circle detection and line detection of PCL
[opencv] edge detection [API and source code implementation]
Take root downward, grow upward, and explore the "root" power of Huawei cloud AI
js正则表达式匹配ip地址(ip地址正则表达式验证)
Deep recursion, deep search DFS, backtracking, paper cutting learning.
Hj9 extract non duplicate integers
Summary of my 2020 online summer camp
HJ9 提取不重复的整数
Summary of common PHP functions
Panzer_ Jack's personal blog founding day
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.