FFmpeg

  • 1、编码流程
  • 2、编码

1、编码流程

1、初始化AVFormatContext:avformat_alloc_output_context2(),包含有输出码流(AVStream)和解复用器(AVInputFormat)。
2、打开输出文件:avio_open()。
3、初始化AVCodecContext:avcodec_alloc_context3(),包含编码参数信息。
4、创建视频码流:avformat_new_stream(),该函数生成一个空AVstream 该结构存放编码后的视频码流 。视频码流被拆分为AVPacket新式保存在AVStream中。
5、拷贝编码参数信息:avcodec_parameters_from_context(),从AVCodecContext到输出流中。
6、写头信息:avformat_write_header(),将封装格式的信息写入文件头部位置。
7、查找编码器:avcodec_find_encoder()。
8、设置编码器信息:AVCodecContext
码率:bit_rate
图像格式:pix_fmt
分辨率:width、height
帧率:framerate、time_base
GOP大小:gop_size
B帧:max_b_frames
额外设置一些参数(如 h265 要设置qmax、qmin、qcompress参数才能正常使用h264编码)
9、打开编码器:avcodec_open2(),根据前一步设置的编码器参数信息,来查找初始化一个编码器,并将其打开。
10、分配AVPacket和AVFrame:av_packet_alloc()和av_frame_alloc()
11、编码:avcodec_send_frame()和avcodec_receive_packet()。
12、写入输出文件:av_interleaved_write_frame()。

2、编码

/** Copyright (c) 2001 Fabrice Bellard** Permission is hereby granted, free of charge, to any person obtaining a copy* of this software and associated documentation files (the "Software"), to deal* in the Software without restriction, including without limitation the rights* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell* copies of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in* all copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN* THE SOFTWARE.*//*** @file* video encoding with libavcodec API example** @example encode_video.c*/#include <stdio.h>
#include <stdlib.h>
#include <string.h>#ifdef __cplusplus
extern "C" {#endif // __cplusplus#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>#ifdef __cplusplus
}
#endif // __cplusplusstatic void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,FILE *outfile)
{int ret;/* send the frame to the encoder */if (frame)printf("Send frame %3"PRId64"\n", frame->pts);ret = avcodec_send_frame(enc_ctx, frame);if (ret < 0) {fprintf(stderr, "Error sending a frame for encoding\n");exit(1);}while (ret >= 0) {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);}printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);fwrite(pkt->data, 1, pkt->size, outfile);av_packet_unref(pkt);}
}int main(int argc, char **argv)
{const char *filename, *codec_name;const AVCodec *codec;AVCodecContext *c= NULL;AVCodecID codec_id = AV_CODEC_ID_HEVC;int i, ret, x, y;FILE *f;AVFrame *frame;AVPacket *pkt;uint8_t endcode[] = { 0, 0, 1, 0xb7 };if (argc <= 2) {fprintf(stderr, "Usage: %s <output file> <codec name>\n", argv[0]);exit(0);}filename = argv[1];codec_name = argv[2];/* find the mpeg1video encoder */codec = avcodec_find_encoder(codec_id);if (!codec) {fprintf(stderr, "Codec '%s' not found\n", codec_name);exit(1);}c = avcodec_alloc_context3(codec);if (!c) {fprintf(stderr, "Could not allocate video codec context\n");exit(1);}pkt = av_packet_alloc();if (!pkt)exit(1);/* put sample parameters */c->bit_rate = 400000;/* resolution must be a multiple of two */c->width = 352;c->height = 288;/* frames per second */c->time_base = (AVRational){1, 25};c->framerate = (AVRational){25, 1};/* emit one intra frame every ten frames* check frame pict_type before passing frame* to encoder, if frame->pict_type is AV_PICTURE_TYPE_I* then gop_size is ignored and the output of encoder* will always be I frame irrespective to gop_size*/c->gop_size = 10;c->max_b_frames = 1;c->pix_fmt = AV_PIX_FMT_YUV420P;if (codec->id == AV_CODEC_ID_H264)av_opt_set(c->priv_data, "preset", "slow", 0);/* open it */ret = avcodec_open2(c, codec, NULL);if (ret < 0) {fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));exit(1);}f = fopen(filename, "wb");if (!f) {fprintf(stderr, "Could not open %s\n", filename);exit(1);}frame = av_frame_alloc();if (!frame) {fprintf(stderr, "Could not allocate video frame\n");exit(1);}frame->format = c->pix_fmt;frame->width  = c->width;frame->height = c->height;ret = av_frame_get_buffer(frame, 32);if (ret < 0) {fprintf(stderr, "Could not allocate the video frame data\n");exit(1);}/* encode 1 second of video */for (i = 0; i < 25; i++) {fflush(stdout);/* make sure the frame data is writable */ret = av_frame_make_writable(frame);if (ret < 0)exit(1);/* prepare a dummy image *//* Y */for (y = 0; y < c->height; y++) {for (x = 0; x < c->width; x++) {frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;}}/* Cb and Cr */for (y = 0; y < c->height/2; y++) {for (x = 0; x < c->width/2; x++) {frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;}}frame->pts = i;/* encode the image */encode(c, frame, pkt, f);}/* flush the encoder */encode(c, NULL, pkt, f);/* add sequence end code to have a real MPEG file */fwrite(endcode, 1, sizeof(endcode), f);fclose(f);avcodec_free_context(&c);av_frame_free(&frame);av_packet_free(&pkt);return 0;
}

【FFmpeg4.1.4 编码】h265编码相关推荐

  1. H265编码等级以及图像的基础知识

    1. H265编码等级 H264编码profile & level控制 .H265编码初探 H265 profile H265 Profile & Level & Tier 介 ...

  2. 如何让ffplay或者ffmpeg支持H265编码的rtmp/http-flv 实时直播流

    很多初学者不知道ffplay或者ffmpeg是不支持flv封装的rtmp/http-flv流的,其原因是flv不支持H265编码payload的,因为当时制定flv封装协议的时候,H265还没出来,现 ...

  3. 流媒体播放器播放h264编码视频与h265编码视频哪个更清晰?

    h265编码是h264编码的升级版,h265目前在视频点播方面使用的更加普遍,而在视频直播方面,由于难以达到h265编码的解码速度,运用起来还是有些难度的,还需要看未来我们的流媒体技术的发展.那么既然 ...

  4. x265编码H265

    目录 一.前言 二.x265介绍 三.x265主要编码接口介绍 1.int x265_param_default_preset(x265_param *, const char *preset, co ...

  5. H265编码视频播放器EasyPlayer.JS控制台出现VideoJS:WARN警告信息是什么原因?

    H265编码的压倒性优势致使其不断在音视频行业完善发展,TSINGSEE青犀视频在不断开发H265播放器的不同使用方法,并且期望在未来运用于更多场景当中(h264编码视频与h265编码视频哪个更清晰) ...

  6. H265编码 SPS分析

    学习目标: H265编码分析 学习内容: H265出现的原因: 我们视频的分辨率 从 720p到1080p,而且电视的屏幕也越来越大 视频帧率从30帧 到60帧,再到120帧 这就会导致我们cpu在编 ...

  7. 视频在html不能播放器,网页无插件直播H265编码视频播放器EasyPlayer网页播放器不能播放怎么处理?...

    原标题:网页无插件直播H265编码视频播放器EasyPlayer网页播放器不能播放怎么处理? EasyPlayer播放器系列项目提供了非常简单易用的SDK及API接口,用户通过API调用就可以非常快速 ...

  8. FFmpeg4入门13:h264编码为mp4

    上一篇将yuv源视频文件编码为*.h264的由libx264实现压缩的文件,将源文件从55M编码为620KB,但是h264文件只有视频数据,而且使用范围不太广.那么就需要进一步的封装,在此选用最常用的 ...

  9. H264和h265编码

    未压缩的码流:一秒钟码流大小:640x480x1.5x15x8=55296000 (是55MB)其中 1.5是yuv占用1.5倍,rgb是3倍,8是一个字节是八位bit H264的建议码流是500kp ...

  10. php h.265解码视频,用狸窝转换H265编码视频步骤

    认识众多玩家高手/拆客/DIYer,查阅更多资源,一起学习技术知识 您需要 登录 才可以下载或查看,没有帐号?立即注册 x 网上下了些电视剧,可惜是H265编码的,机顶盒和电脑都播放不了 只能转码了 ...

最新文章

  1. python爬虫能干什么-总算发现python爬虫能够干什么
  2. pytorch 查看当前学习率_pytorch调整模型训练的学习率
  3. 白领丽人:这六行盛产“钻石王老五”
  4. SpringSecurity集中式整合之授权操作
  5. 【招聘(北京)】北森测评招聘 .NET 架构师、高级工程师
  6. 行添加DataGridView导出Excel的数据表格
  7. Http请求处理流程
  8. linux脚本硬盘,Linux mount挂载和卸载硬盘脚本分享
  9. 数据的四大特征_大数据
  10. MySQL 备份 nb3 和 psc的区别
  11. spring-boot-starter家族成员简介
  12. 学习vim: vim cheat sheet
  13. 房产纠纷官司费用是多少
  14. 【亲测】2022最新H5手机微商城运营源码/简约轻量版/对接支付个人免签接口/带搭建教程
  15. mysql 5.7.25 中文全文检索(多个字段联合索引)
  16. 《珞珈山原色植物图谱》高清文字版pdf 附下载链接
  17. android和夜神模拟器哪个好,天天模拟器和夜神安卓模拟器哪个好 两者功能对比...
  18. LINUX下浮点运算
  19. birt什么意思中文翻译_有含义的英文网名带中文翻译
  20. [复变函数]第24堂课 6.3 辐角原理

热门文章

  1. cmd net 命令
  2. 2.交互-对鼠标及键盘的使用
  3. windows系统上删除顽固文件
  4. 如何缩小jpg图片大小?jpg格式怎么压缩?
  5. windows10强制删除文件_Windows10 初装必设置
  6. Vuex 命名空间 namespaced 介绍
  7. 个人辅助带后台纯HTML网站源码
  8. gstreamer插件指南
  9. 批处理访问域服务器文件夹,批处理如何访问域共享文件夹
  10. 交换机入门书籍推荐_网络工程学习方法/路线/专业书籍推荐